Force app to use test database.

For an integration test (using Playwright) I start a celery worker in another process using a pytest fixture. This worker fetches its configuration from the Django settings. Unfortunately, it doesn’t use the test database (it just uses the database with name “postgres” instead of “test_postgres”).

  • How does Django decide to use the “test_postgres” database? Some environment variable?
  • Is there some way to force the Django celery worker to use the test database (in a nice way)?
  • Where is this documented? The documentation talks about that there is a test database created, but not when and how exactly.

Hey there!
Can you tell more about your environment?

Are you using any pytest plugin like pytest-django?
Are these fixtures started from pytest? Do you have a running instance of django server?

Hi @leandrodesouzadev.

Yes, I am using pytest-django and something similar as live_server to start a Django server.

Starting the celery worker is just a fixture (full code):

@pytest.fixture
def adit_celery_worker():
    def start_worker():
        # here it tells me that NAME ist "test_postgres"
        print(settings.DATABASES["default"])

        # But inside the worker it tells me NAME is "postgres"
        call_command("celery_worker", "-Q", "default_queue,dicom_task_queue")

    p = Process(target=start_worker)
    p.start()
    yield
    p.terminate()

I am just looking for a way to force the worker to use the test database. Of course, I could add another flag to the worker (maybe “–test”) and prepend “test_” manually. But I wonder how Django itself decides that it is in a testing environment.

I think that is due to the settings being patched on the process A by pytest-django, and on process B that gets started for the worker, this is not being patched.

Yes, and this is exactly what I am looking for. How is it “patched”?

Maybe you can wrap your celery start program, with a python script that changes the settings.DATABASES before actually start the worker. I think that you can run celery programatically as well aside from the CLI.

Yes, it seems to be the easiest way. I took a look at the Django test runner, and it seems to directly alter the database names (I guess pytest does this also or maybe wraps the Django runner). Thanks for your input!

1 Like