All jobs

Write pytest Tests for Celery Tasks

Yes. An agent writes pytest tests for Celery tasks and the routes that queue them, with no broker for unit tests and an in-memory worker for one real round trip.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent sorts the code into three kinds of thing to test, because each needs a different technique. In an app like miguelgrinberg/flask-celery-example everything is in app.py and there is no test directory, no pytest in requirements.txt and no CI workflow. The file holds two task bodies, send_async_email and the bound task long_task; three routes that queue or inspect tasks, /, /longtask and /status/<task_id>; and the wiring, a module-level Celery object whose broker is redis://localhost:6379/0. Importing the module creates the Flask app, the Mail extension and the Celery app, so every test has to work with those objects as they are.

Task bodies are tested by calling them in process, with no broker. long_task loops random.randint(10, 50) times and sleeps one second per pass, so an unpatched test would take up to fifty seconds and assert on random text. The agent patches time.sleep in the module, fixes the random total, and replaces update_state on the task with a recorder. The test then asserts that each call carries state='PROGRESS', that current counts up from 0 to one below total, that status is never empty, and that the return value is the dictionary with result 42. For send_async_email it checks where Flask-Mail reads MAIL_SUPPRESS_SEND, at import or at send time, sets it where it takes effect, and captures the outgoing message with mail.record_messages() to assert the subject, the recipient, the body and the default sender.

Routes are tested with Flask's test client and the queue calls replaced. A POST to / with submit=Send must call send_async_email.delay with the three-key email_data, and any other submit value must call apply_async with countdown=60; both must store the address in the session, flash the right message and redirect. /longtask must answer 202 with a Location header that points at the status route for the new task id. /status/<task_id> has three branches, and the agent feeds each one a stand-in for long_task.AsyncResult: PENDING gives the placeholder numbers, any other state but FAILURE reads current, total and status from task.info and adds result when present, and FAILURE returns the exception as text.

One test goes through a real worker so the suite is not only patches. The agent points the app's own celery object at Celery's in-memory broker and cache backend, starts a worker thread with celery.contrib.testing.worker.start_worker, queues long_task with the sleep patched out, and waits on the result. It puts pytest in a requirements-dev.txt beside the pinned file, runs the suite, measures coverage of app.py before and after, then removes the countdown argument once to show a red test and restores it.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Write pytest tests for this app's Celery tasks and the routes that use them. Read every Python file, requirements.txt and the README first, and list the tasks, how each is queued (delay, apply_async, countdown, eta), and every place a task's state or result is read. Unit-test each task body in process with no broker: patch time.sleep and random where a task uses them so no test waits or depends on chance, record update_state calls on bound tasks and assert their order and contents, and capture outgoing mail with the mail extension's own test recorder instead of an SMTP server. Test each route with the framework's test client: replace delay and apply_async with recorders and assert the exact arguments and options, the response status, headers, flashes and session; cover every branch of any status route by substituting AsyncResult with objects in PENDING, a custom progress state, SUCCESS and FAILURE. Add one integration test that runs a task through a real worker using Celery's in-memory broker and backend and celery.contrib.testing.worker.start_worker on the app's own Celery object. Add pytest in a separate dev requirements file pinned to a version that supports the pinned Celery; change no application code, and list anything that made testing harder instead. Run the suite twice to show it is stable, report coverage of the application module before and after, break one queueing option to show a failing test, restore it, and open a pull request with the output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

The pull request adds a tests/ directory with a conftest.py and one file each for task bodies, routes and the worker round trip, plus requirements-dev.txt. app.py is not in the diff. The description lists every test with the branch it covers, both runs of the suite, the coverage figure for app.py before and after, and the failing output from the deliberate break followed by the passing run.

A separate section holds what the agent noticed and left alone. In this app that would include the status route calling .get on task.info for every state that is neither PENDING nor FAILURE, which assumes the info is always a dictionary, and the fact that objects created at import make it awkward to give each test a fresh app. A reviewer checks that no test opens a connection to Redis or SMTP, that the patches target the names app.py actually looks up, and that the worker test would fail if the task were never delivered and not merely pass when it is.

Run it on every merged pull request

To keep new tasks from landing untested, create an automation in the Project that holds the repository with a github trigger on pull_request.closed.merged and main in its branch_patterns. The saved prompt says: read the merged pull request that follows, and if it added or changed a Celery task or a route that queues one and no test names it, write tests in the style of the existing suite and open a pull request; otherwise end with one line. The trigger puts the event after the prompt and fills in nothing, and it fires only for work by an owner, member or collaborator.

A pull request merged by a bot does not fire it. For those, or to cover a whole set of worker services in one pass, a script on your own server sends this body once per repository with a request_id that is a UUID v5 of the repository's full name and the commit it was sent for, so a retry returns the run already started.

The use case this job follows, with its automation and trigger: Write the missing tests.

Why run it on Rush?

Task code is where test suites go slow or go fake. A sleep that nobody patched makes every run of the suite wait on it; a test that mocks the task it is meant to test passes forever. The agent runs the suite in its own sandbox as it writes it, so a test that waits, reaches for Redis or depends on a random draw shows up immediately in the output, and that output is kept with the run. The deliberate break at the end is the check that the assertions are attached to the code.

It also reads how each task is queued before choosing what to assert. A countdown, a bound task that reports progress and a route that reads task state are three different contracts, and they fail in production in three different ways. Writing a test for each takes patience more than insight, which makes it a good job to hand over: twenty task modules across several services are twenty runs, each working from that module's own code.

Frequently asked questions

Do the tests need Redis or RabbitMQ running?

No. Task bodies run in process, routes run with the queue calls replaced, and the single worker test uses Celery's in-memory broker and cache backend. The suite runs on a laptop or in CI with nothing but Python installed.

Why not just set task_always_eager?

Eager mode runs the task inside the call that queues it, so it never proves a message was serialized, delivered and picked up. The agent tests the body directly, tests the queueing call by its arguments, and keeps one test that goes through a worker. That covers what eager mode covers and the part it hides.

How does it test a task that reports progress?

It replaces update_state on the task object with a function that records each call, runs the body, and asserts on the recorded sequence. For long_task that means the state name, a current that rises by one per pass and a total that stays fixed.

Will it restructure my app into a factory to make it testable?

No. The prompt says to change no application code, so the tests work with the objects the module creates at import. If a factory would make the tests simpler, the pull request says so and leaves the decision to you.

Does it add a CI workflow?

Not unless you ask. This app has none, and adding one is a separate decision about Python versions and services. The pull request gives the one command that runs the suite, which is what a workflow would call.

More Celery jobs

The same job on other stacks

Keep reading