Skip to content

pytest.mark.django_db does not isolate tests #461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
qwiglydee opened this issue Feb 13, 2017 · 4 comments
Closed

pytest.mark.django_db does not isolate tests #461

qwiglydee opened this issue Feb 13, 2017 · 4 comments

Comments

@qwiglydee
Copy link

pytest version 3.0.6
pytest-django-3.1.2
django-1.10

settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR + '/var/db.sqlite'
    },
    'world': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'test',
        'HOST': '',
        'USER': 'postgres',  # need superuser to create db
        'TEST': {
            'NAME': 'test'
        }
    }
}

tests:

@pytest.mark.django_db
def test_1():
    models.Feature.objects.create(id=99999999, **dumb_attrs)


@pytest.mark.django_db
def test_2():
    models.Feature.objects.create(id=99999999, **dumb_attrs)

Running tests fails with IntegrityError.
After running created objects remain in database.

@blueyed
Copy link
Contributor

blueyed commented Feb 13, 2017

Is the Feature model being routed through the world DB?

@qwiglydee
Copy link
Author

Yes, it is routed.
But this indeed seems to be the problem. The same problem arises when using native django testrunner.
Also, documentation says: "To have the GeoDjango tests executed when running the Django test suite with runtests.py all of the databases in the settings file must be using one of the spatial database backends."

I changed the testing settings to use one and only 'default' database, removed routing, and isolation started to work.

It seems like both for django testrunner and pytest runner problem is at using multiple databases.
Or maybe it is even deeper than test runner.

@blueyed
Copy link
Contributor

blueyed commented Feb 14, 2017

You need to enable multi_db.
pytest-django does not support it easily currently though - see #431 and the issues referenced there.

@pelme
Copy link
Member

pelme commented Jul 4, 2017

Closing this since it is a multi_db problem, see the referenced issue above.

Feel free to comment/reopen this issue if I misunderstood it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants