Getting a "Circular Dependency in TEST[DEPENDENCIES]" error when running pytest over unittests

81 Views Asked by At

I currently have some tests written with the built in unittest framework, that works fine when I do python manage.py tests location of tests.

However, I decided I want to switch to pytest (eventually). I installed pytest-playwright (because I also want to create some functional tests), along with pytest-django. It seems playwright recommends using the pytest plugin, and since from what I've read...pytest's testrunner is totally compatible with unittest, tests, I did that.

But...when I try to run tests with pytest location of tests. I get the below error:

django.core.exceptions.ImproperlyConfigured: Circular dependency in TEST[DEPENDENCIES]

I've tried a bunch of things, but I can't get it to work.

Some additional notes:

  • My current unittests are not using a test database (or real one). The tests are just checking if a request can hit the view essentially.
  • I tried uninstalling pytest-django, and when I run the tests I get this error:

.

django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.co...

I'm 99% sure I'm correctly pointing to my test environment variable. I have a pyproject.toml file and pytest.ini file, both with the DJANGO_SETTINGS_MODULE pointing to my config.settings.test env.

I noticed that when I have pytest-django installed and I'm getting the circular error, the code is looping through my databases, and it seems to error out there (in the django\test\utils.py file). Below is the code where it bombs:

def dependency_ordered(test_databases, dependencies):
"""
Reorder test_databases into an order that honors the dependencies
described in TEST[DEPENDENCIES].
"""
ordered_test_databases = []
resolved_databases = set()

# Maps db signature to dependencies of all its aliases
dependencies_map = {}

# Check that no database depends on its own alias
for sig, (_, aliases) in test_databases:
    all_deps = set()
    for alias in aliases:
        all_deps.update(dependencies.get(alias, []))
    if not all_deps.isdisjoint(aliases):
        raise ImproperlyConfigured(
            "Circular dependency: databases %r depend on each other, "
            "but are aliases." % aliases
        )
    dependencies_map[sig] = all_deps

while test_databases:
    changed = False
    deferred = []

    # Try to find a DB that has all its dependencies met
    for signature, (db_name, aliases) in test_databases:
        print("db", signature)

        if dependencies_map[signature].issubset(resolved_databases):
            resolved_databases.update(aliases)
            ordered_test_databases.append((signature, (db_name, aliases)))
            changed = True
        else:
            deferred.append((signature, (db_name, aliases)))

    if not changed:
        raise ImproperlyConfigured("Circular dependency in TEST[DEPENDENCIES]")
    test_databases = deferred
return ordered_test_databases

Oddly...it doesn't seem to loop through my 'default' database...I don't know why...but in any case I don't even need a database for my tests, so not sure why it would even get stuck here...

i've also tried add the command flag --no-migrations at the end to not spin up a test database, but that didn't change anything :(

Appreciate any help!

Thank You!

1

There are 1 best solutions below

2
Matt S On

I found the reason!

I had multiple databases in my config.settings.base file. Only 1 of them was local, the others were remote (that wouldn't be necessary for my tests).

my config.settings.test file inherited the .base file. Those databases persisted through. That error I mentioned above was getting snagged on all those remote databases (but not my local one).

What I ended up doing was moving the local database info that I needed into the test file, essentially overriding all those other ones that were not needed, and it worked!