<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Arpit Solanki on Medium]]></title>
        <description><![CDATA[Stories by Arpit Solanki on Medium]]></description>
        <link>https://medium.com/@solankiarpit1997?source=rss-6c112fe6bb24------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*UsSq1nbKciaDrH3UOTUXog.jpeg</url>
            <title>Stories by Arpit Solanki on Medium</title>
            <link>https://medium.com/@solankiarpit1997?source=rss-6c112fe6bb24------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 19:31:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@solankiarpit1997/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Testing Celery Tasks]]></title>
            <link>https://medium.com/@solankiarpit1997/testing-celery-tasks-a8a3568d0213?source=rss-6c112fe6bb24------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8a3568d0213</guid>
            <category><![CDATA[celery]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[asynchronous]]></category>
            <dc:creator><![CDATA[Arpit Solanki]]></dc:creator>
            <pubDate>Mon, 19 Feb 2018 15:03:05 GMT</pubDate>
            <atom:updated>2018-02-19T15:03:05.435Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I came across the problem of writing unit tests for <a href="http://docs.celeryproject.org/">Celery</a> tasks. Writing unit tests for celery tasks can be painful since they are asynchronous and long. I will be explaining how to write unit tests for Celery tasks and will try to address different cases for this. For this tutorial we will be using python’s native <a href="https://docs.python.org/2/library/unittest.html">unittest</a> library.</p><h4>Prerequisites</h4><ul><li>You should know the basics of Celery to follow the tutorial.</li><li>If you know basics of writing tests then its awesome but not mandatory for this tutorial.</li></ul><h4>Environment Setup</h4><ul><li>This tutorial is written for python 2.x and should work or python 3.x also.</li><li>You must have working installation of Celery, <a href="https://www.rabbitmq.com/">RabbitMQ</a> and <a href="https://redis.io/">Redis</a> (redis because we want to store the results)</li></ul><p>That was all let’s dive into the tutorial</p><p>We will be creating a very simple celery task which adds two numbers</p><p>Open your favourite text editor and paste the following code and save it as tasks.py</p><pre>from celery import Celery<br>from time import sleep</pre><pre>BROKER_URL = ‘amqp://guest@localhost:5672//’<br>REDIS = ‘redis://localhost/0’<br>app = Celery(‘test’, broker=BROKER_URL, backend=REDIS)</pre><pre><a href="http://twitter.com/app">@app</a>.task(name=”add”)<br>def add(x, y):<br> return x+y</pre><p>Now let’s write some tests for this tasks.</p><p>We have to test whether our task returns the correct value for given two numbers or not. So we will write one test case class to test all the possible tests for this tasks.</p><p>Now create a file named tests.py in the same directory as tasks.py and paste the following code.</p><pre>from tasks import add, app<br>import unittest<br>class TestAddTask(unittest.TestCase):<br>  def setUp(self):<br>    self.task = add.apply_async(args=[3, 5])<br>    self.results = self.task.get()</pre><p>Let’s see what we did back up there. We inherited the unittest.TestCase class and defined a setUp method. This method is used to initialize the tests and set necessary variables for testing. Here we called our add task and with arguments 3, 5 and we called get method on it to get the result. This get method call wait till the task finishes. When the tasks finishes we got the results and test initialization is completed. Now let’s test the output of this task.</p><p>Let’s check our task’s state whether it returned SUCCESS or FAILURE</p><p>In the above mentioned TestAddTask class we now define a method to check state of our task</p><pre>def test_task_state(self):<br>  self.assertEqual(self.task.state, ‘SUCCESS’)</pre><p>In the above mentioned TestAddTask class now we define a method to test our output.</p><pre>def test_addition(self):<br>  self.assertEqual(self.results, 8)</pre><p>Complete test case class</p><pre>from tasks import add, app<br>import unittest</pre><pre>class TestAddTask(unittest.TestCase):<br>  def setUp(self):<br>    self.task = add.apply_async(args=[3, 5])<br>    self.results = self.task.get()</pre><pre>  def test_task_state(self):<br>    self.assertEqual(self.task.state, ‘SUCCESS’)</pre><pre>  def test_addition(self):<br>    self.assertEqual(self.results, 8)</pre><p>Now let’s run the tests.</p><p>Start celery worker: Inside the directory where tasks.py is run the following command.</p><p>celery -A tasks worker — loglevel=INFO</p><p>Run the tests: Inside the directory where tests.py is run the following command</p><p>python -m unittest discover</p><p>You should see an output something like below</p><pre>➜ python -m unittest discover<br>..<br> — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — <br>Ran 2 tests in 5.353s</pre><pre>OK</pre><p>Voila we successfully ran the tests on celery tasks.</p><p><strong>What if my tasks are on remote machine and I can’t import them?</strong></p><p>Don’t worry just replace apply_async function with the following statement</p><pre>self.task = app.send_task(‘add’, args=[3, 5])</pre><p><strong>Now let’s take an example where the task does not return any results</strong></p><p>Let’s write a task which downloads an image and writes it to a file. Update tasks.py with following task</p><pre>import requests<a href="http://twitter.com/app"><br>@app</a>.task(name=’download-image’, ignore_results=True)<br>def download_image(url):<br>  r = requests.get(url)<br>  with open(‘image.jpg’, ‘wb’) as f:<br>    f.write(r.content)</pre><p>Let’s see how should we test this task. Write the following code in the tests.py</p><pre>import os<br>class TestDownloadTask(unittest.TestCase):<br>  def setUp(self):<br>    self.task = app.send_task(‘download-image’, args=    [‘<a href="https://www.math.ust.hk/~masyleung/Teaching/CAS/MATLAB/image/images/cameraman.jpg&#39;">https://www.math.ust.hk/~masyleung/Teaching/CAS/MATLAB/image/images/cameraman.jpg&#39;</a>])<br>    self.results = self.task.get()</pre><pre>  def test_task_state(self):<br>    self.assertEqual(self.task.state, ‘SUCCESS’)</pre><pre>  def test_download(self):<br>    self.assertEqual(os.path.exists(‘image.jpg’), True)</pre><p>Here we check that our task has wrote the image successfully or not by using os library.</p><p>Check out more about Testing Celery Tasks at <a href="http://docs.celeryproject.org/en/latest/userguide/testing.html">official docs</a>. Please comment if you find something wrong.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8a3568d0213" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>