1

We use py.test to test our Django system. We use unmanaged DBs in our system:

class Example(models.Model):

    class Meta:
        managed = False

When we run tests that need access to the existing DBs with existing data, they fail with

ConnectionDoesNotExist: The connection my_connection_name doesn't exist

This is how our tests look like:

@pytest.mark.django_db
def test_example_access():
    from example.models import Example
    Example.objects.count()

We've tried to add the following to conftest.py as described in the top answer on Django test tables are not being created, but it did not change the matter (despite being executed).

@pytest.fixture(autouse=True, scope='session')
def __make_unmanaged_managed():
    from django.apps import apps
    unmanaged_models = [m for m in apps.get_models() if not m._meta.managed]
    for m in unmanaged_models:
        m._meta.managed = True

Thanks a lot for your ideas!

ps: Testing django application with several legacy databases provides a workaround for unit tests, however I would not know how to apply this when using py.test specifically.

elke
  • 1,220
  • 2
  • 12
  • 24

1 Answers1

0

I had the same problem here, and as far as I can tell, this is the snippet that worked for me.

I had to put this in the conftest.py file, in the root of my django project, to override the normal django_test_environment fixture in pytest-django.

@pytest.fixture(autouse=True, scope='session')
def django_test_environment(request):
    """
    This is an override of the django_test_environment function in pytest-django,
    to run through the unmanaged models, and just for the test, set them to be managed,
    so we can create models in our tests, even if we don't manage directly in Django.
    """

    from pytest_django.plugin import django_settings_is_configured
    from pytest_django.plugin import _setup_django

    from django.apps import apps
    unmanaged_models = [m for m in apps.get_models() if not m._meta.managed]
    for m in unmanaged_models:
        m._meta.managed = True

    if django_settings_is_configured():
        _setup_django()
        from django.conf import settings as dj_settings
        from django.test.utils import (setup_test_environment,
                                      teardown_test_environment)
        dj_settings.DEBUG = False
        setup_test_environment()
        request.addfinalizer(teardown_test_environment)

What this does

When Pytest sets up a django database, it does so with the django_db_setup fixture. This fixture itself takes django_test_environment, which we override in conftest.py, to update the managed state on the unmanaged models.

When setup_test_environment is called as part of our overriden setup_test_environment fixture, it creates the necessary tables for us to reference models in our tests

I'd love to hear if this works, as it seems to have resolved the issue at my end, when I ended up having to work with many, many unmanaged models, and manually creating them with SQL seemed like a terrible idea.

Chris Adams
  • 2,721
  • 5
  • 33
  • 45