For a very long time now I've been using task queues on AppEngine to schedule tasks, just the way I'm supposed to.
But what I've always been wondering is how does one write tests for that? Until now I've simply made tests to make sure an error doesn't occur on the API that queues a task and then wrote the more proper tests for the API executing the task.
However lately I've started feeling a bit unsatisfied by this and I'm searching for a way to actually test that the correct task has been added to the correct queue. Hopefully this can be done better than simply by deploying the code and hoping for the best.
I'm using django-nonrel, if that has any bearing on the answer.
To recap: How can a unit test be written to confirm tasks have been queued?
Using GAE Test Bed will allow you to stub out a task queue.
If you inherit from
FunctionalTestCase
orTaskQueueTestCase
, you'll get methods such asget_tasks
andassertTasksInQueue
.You can actually run the tasks, too. How to do it differs depending on whether you use tasks or deferred.
For deferreds, I have some code like this:
Running tasks is similar, but after you fetch the task, you use WebTest (which GAE Test Bed is built on top of) to submit as POST request to the task's URL with its parameters.