Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7582651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:29:15+00:00 2026-05-30T18:29:15+00:00

I am trying to test whether a celery task has been started after a

  • 0

I am trying to test whether a celery task has been started after a request to my django server. I have something like this:

# tasks.py
def add(x, y):
    return x + y

# views.py
def home(request): # respond to request at root url
    tasks.add.delay(1,2)
    return HttpResponse('hello world')

# tests.py
class MyTest(TestCase):
    def test_task_triggered(self):
        self.client.get('/')

        # XXXX HOW TO TEST THAT TASK HAS BEEN TRIGGERED?

How can I test whether or not the task has been started in my unit tests? Obviously, I don’t have direct access to the task id otherwise something like this would work.

More generally, how can you detect celery tasks being triggered from across functions, classes, or modules?

Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:29:16+00:00Added an answer on May 30, 2026 at 6:29 pm

    You can use Mock for this (when unit testing). Patch out the delay attribute on your task, and then check the called attribute of the mock. In your example, try something like:

    # tests.py
    from mock import Mock, patch
    
    class MyTest(TestCase):
        def test_task_triggered(self):
            with patch('views.tasks.add.delay') as patch_mock:
                self.client.get('/')
            self.assertTrue(patch_mock.called)
    

    The caveat is that once you’ve done this, tasks.add won’t actually be called in this test case, as we’ve replaced it with a mock. This test would simply assert that it’s being called (and you could check patch_mock.call_args_list to assert it’s being called with the correct arguments)

    If that’s a deal-breaker – you want to assert in one test case that a task is being called, as well as the impact/side effects of the task – you could experiment with the wraps attribute of mock, which might work:

    # tests.py
    from mock import Mock, patch
    
    class MyTest(TestCase):
        def test_task_triggered(self):
            add_mock = Mock(wraps=tasks.add)
            with patch('views.tasks.add.delay', new=add_mock):
                self.client.get('/')
            self.assertTrue(add_mock.called)
    

    But the other way is probably better, since it better isolates what’s being tested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ColdFusion 9.0.1. I am trying to test whether a user has
I am trying to test whether certain time has elapsed. I use javascript. Both
I was trying to test whether the Alt key was pressed. I had a
Easy question this time. I'm trying to test whether or not a string does
I am trying to test SharePoint's sending of emails - whether basic alerts or
Im trying to test my successfully creates a new user after login (using authlogic).
I am trying to test the likelihood that a particular clustering of data has
I'm trying to test whether a user is registered on FreeNode. nick_info() doesn't seem
I am trying to test to see whether a Javascript variable is undefined. You
I am trying to test whether a string can be converted into a number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.