Unit Testing Celery Tasks

How to unit test Celery tasks

While you might get away with not writing unit tests for very simple Rest API endpoints,
doing the same for celery tasks is recipe for frustration (and disaster). Celery tasks are asynchronous
by design and therefore a lot harder to get a grip on using a “development driven development” approach.

Test Driven Development (TDD) might not have taken us to the promised land we had hoped for, but when it comes to
celery tasks, it most definitely is essential to a sane, effective and efficient development process -
and having that peace of mind when releasing your code into production.

How to apply this

Invoking your Celery tasks inside your tests with the apply() method executes
the task synchronously and locally. This allows you to write tests that look and feel very similar
to the ones for your API endpoints.