https://pantsbuild.org/ logo
#general
Title
# general
p

plain-summer-72727

11/01/2022, 8:39 PM
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.
h

happy-kitchen-89482

11/01/2022, 10:32 PM
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
g

gentle-sugar-52379

11/02/2022, 12:13 AM
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:"
    else:
        name = os.path.join(
            mkdtemp(),
            f"test{os.environ.get('PANTS_EXECUTION_SLOT', '')}.sqlite3",
        )
    c.DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.sqlite3",
            "NAME": name,
        }
    }
    return c
i

incalculable-hydrogen-44003

11/02/2022, 1:48 AM
Thanks. We will look into this tomorrow. We have been trying to get Django to work with pants for many months now unfortunetly
p

polite-garden-50641

11/02/2022, 2:24 PM
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.
i

incalculable-hydrogen-44003

11/02/2022, 8:23 PM
Thank @polite-garden-50641 We are giving
pytest-django
a shot now
p

plain-summer-72727

11/02/2022, 9:05 PM
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
.
h

happy-kitchen-89482

11/02/2022, 9:11 PM
I think you have to use
conftest.py
to initialize django (take a look at examples in that example-repo)
p

plain-summer-72727

11/02/2022, 9:15 PM
Ah I see
settings_for_tests
- will check it out. Thank you!
6 Views