How not to make async task tests

Before moving to Dramatiq I used to use Celery. And when it came to unit testing the first thing I added to my code was:

:::python
CELERY_ALWAYS_EAGER = True
CELERY_EAGER_PROPAGATES_EXCEPTIONS = True

At the time I thought this was the best way. But after a lot of trial and error (and mainly because of Dramatiq, which I couldn’t find an eager mode) I found out that the eager mindset is wrong when it comes to unit testing.

By using eager mode I was treating my async tasks as sync and the views or methods that call those tasks need to run successfully without the async task.

So, in my opinion the best way to test this is to do something like (django with rest framework):

:::python
url = reverse("user-list")
response = self.client.post(url, data=sample_data)

# assert that nothing breaks and is working as expected
self.assert()

# call task as a normal function
task()

# assert that the side effect of the task were applied
self.assert()

Dramatiq not having an eager mode was a blessing in disguise.