Mocking Celery
If you are building tests for Celery workers, the simplest way to run your tests is to use a real Redis server. A Redis server is straightforward to run on any platform. Even Travis-CI will let you run one. So, instead of adding a lot of work to mock all the interactions, your Flask code will have with Redis to send some jobs to workers, and post them for real.
Using a real broker means that you can run your Celery worker in your test, just to validate that the app sent the proper job payloads. Celery provides a pytest test fixture that will run for you in a separate thread and shut it down once the test is over.
This is done by implementing a few fixtures to configure Celery to use Redis and to point your tests's tasks. The ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access