[BangPypers] Django - Testing Celery Tasks!

kracekumar ramaraju kracethekingmaker at gmail.com
Wed Aug 6 10:01:42 CEST 2014


Hi

This is how I solve it.

tasks.py

@app.task
def some_task(*args, **kwargs):
      return do_something(*args, **kwargs)

somefile.py
def do_something(*args, **kwargs):

   # All my logic is here

test_somefile.py

def test_do_something_when_user_isinactive():
     #test logic, this works in py.test

By the above approach my function which does real work is testable/callable
without celery. Tomorrow if I want to move away from celery it is easy.




On Wed, Aug 6, 2014 at 12:17 PM, Anand Reddy Pandikunta <
anand21nanda at gmail.com> wrote:

> Hi,
>
> Here is one simple model and a celery task.
> Can some one tell me how to write a test for this task?
> Thank you!
>
> *my_app/models.py*
> *class MyModel(models.Model):*
> *    x = models.IntegerField()*
> *    y = models.IntegerField()*
> *    sum = models.IntegerField()*
>
> *my_app/tests/test_tasks.py*
> *@celery.task()*
> *def add(id):*
> *    m = MyModel.objects.get(pk=id)*
> *    m.sum = m.x + m.y*
> *    m.save()*
> *    return True*
>
> --
> - Anand Reddy Pandikunta
> www.avilpage.com
> www.quotes160.com
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>



-- 

*Thanks & Regardskracekumar"Talk is cheap, show me the code" -- Linus
Torvaldshttp://kracekumar.com <http://kracekumar.com>*


More information about the BangPypers mailing list