Hi everyone - has anyone tried to run tests for a ...
# general
Hi everyone - has anyone tried to run tests for a Django app inside of a Pants monorepo? Curious about other folks' experience with it - if they had to make any BUILD file changes, set dependencies on targets, set up a local database, etc.
Hi! We have an example repo to demonstrate some Django-isms: https://github.com/pantsbuild/example-django
And a talk I gave on the subject is here: https://www.pantsbuild.org/docs/media#djangocon-europe-2021
And @polite-garden-50641 has a lot of experience with this in our own repo
Regarding dependencies, Django does rely a lot on runtime loading via symbol names in settings.py, so you may want to turn on “string imports”: https://www.pantsbuild.org/docs/reference-python-infer#string_imports
And you may need to add some manual deps
At some point I’d like to write a Django plugin that understands settings.py natively, but that’ll have to wait
For local databases, you can use “execution slots” https://www.pantsbuild.org/docs/reference-pytest#execution_slot_var in your test code to point each running test file at its own database, so you don’t have multiple concurrent tests trying to use the same db at the same time
for the tests im using this and its works fine so far
Copy code
def db_sqlite_test_settings(c, in_memory=True):
    if in_memory:
        name = ":memory:"
        name = os.path.join(
            f"test{os.environ.get('PANTS_EXECUTION_SLOT', '')}.sqlite3",
    c.DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.sqlite3",
            "NAME": name,
    return c
Thanks. We will look into this tomorrow. We have been trying to get Django to work with pants for many months now unfortunetly
If you have specific questions or issues you are seeing I am happy to try and help out.
As Benjy mention, our internal monorepo is using django extensively and it works very well for us. We deploy multiple microservices (by composing django apps into services) and use pytest and pytest-django extensively to test our code. for our DB stuff we use postgresql (both when running tests locally or in CI) and for production. I personally feel that using a different DB for testing and for prod is dangerous because it creates a blindspot for tests.
Thank @polite-garden-50641 We are giving
a shot now
Hi @polite-garden-50641 - when we run a test in our Django application we're getting the following error:
Copy code
raise AppRegistryNotReady("Apps aren't loaded yet.")
E   django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Do you happen to know why that might be the case? This happens when we run
./pants test path/to/my_test.py
I think you have to use
to initialize django (take a look at examples in that example-repo)
Ah I see
- will check it out. Thank you!