Skip to content

Run class cleanups when the database setup or teardown of a test fails - #1328

Open
patrick-immometrica wants to merge 1 commit into
pytest-dev:mainfrom
patrick-immometrica:db-helper-teardown-cleanup
Open

patrick-immometrica wants to merge 1 commit into
pytest-dev:mainfrom
patrick-immometrica:db-helper-teardown-cleanup

Conversation

@patrick-immometrica

Copy link
Copy Markdown

When _pre_setup() or _post_teardown() raises in _django_db_helper, tearDownClass() and doClassCleanups() are skipped. Django removes its database access guards in a class cleanup, so they stay on the connections and every later test that may use another alias fails:

@pytest.mark.django_db
def test_a():
    # leaves a dangling foreign key, check_constraints() raises in teardown
    Permission.objects.create(content_type_id=12345, codename="x", name="x")

@pytest.mark.django_db(databases=["default", "other"])
def test_b():
    Group.objects.using("other").create(name="b")

test_b fails with DatabaseOperationForbidden: Database queries to 'other' are not allowed in this test, although the only broken test is test_a.

The cleanups now run in finally blocks, in the order unittest runs them.

If _pre_setup() or _post_teardown() raised, tearDownClass() and
doClassCleanups() were skipped. Django removes its database access
guards in a class cleanup, so they stayed on the connections and later
tests using another alias failed with DatabaseOperationForbidden.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant