I have another pants+django mystery I could use so...
# general
l
I have another pants+django mystery I could use some help with– I can't figure out why my test database is not being created on test runs. With our non-pants stack we get
Creating test database for alias 'default'...
and a working database. When running the same tests with pants, the django testing framework does not appear to create the test database. Am I missing something obvious here?
w
have you tried debugging inside of the sandbox to see if you get any interesting info? https://www.pantsbuild.org/docs/troubleshooting#debug-tip-inspect-the-sandbox-with---no-process-execution-local-cleanup
h
Hmm that does work for us internally
How are you running things in your non-Pants stack?
I think you'll need to use the pytest-django pytest plugin , if you're not already
l
yeah I just noticed that plugin is not getting loaded either
our current approach is, how do you say, complicated
i am considering trying to just get it working with plain pytest first before trying to move to pants
1
c
We use pytest-django plugin. We put this in pants.toml:
Copy code
[pytest]
execution_slot_var = "PANTS_PROCESS_EXECUTION_SLOT"
extra_requirements = [
  "pytest-django==4.4.0",
]
The other important piece is conftest.py
Copy code
import importlib.util
import os

import pytest


def _set_suffix_to_test_databases(suffix):
    from django.conf import settings

    for db_settings in settings.DATABASES.values():
        db_settings.setdefault("TEST", {})
        db_settings["TEST"]["NAME"] = "test_dealroom_{}".format(suffix)


def pytest_cmdline_preparse(args):
    if importlib.util.find_spec("clofo.dealroom.dealroom") is not None:
        args[:] = args + ["--reuse-db"]


def pytest_configure(config):
    if importlib.util.find_spec("clofo.dealroom.dealroom") is not None:
        from django.conf import settings

        os.environ["DJANGO_SETTINGS_MODULE"] = "clofo.dealroom.dealroom.pytest_settings"
        settings.DATABASES
This is using
fixtures
that are provided by
pytest-django
package to bootstrap one database per process execution slot.
The
find_spec
thing is to avoid running this code if we aren't actually testing anything related to our django app
h
For context - the "process execution slot" is to allow you to run tests concurrently without them trying to hit the same db.
h
i am considering trying to just get it working with plain pytest first before trying to move to pants
@loud-laptop-17949 you can use
./pants peek
+
skip_tests
to incrementally port your test suite to Pants https://www.pantsbuild.org/docs/existing-repositories#3-set-up-tests