[Answered ]-How to reuse database in some tests using `pytest-django`?

1๐Ÿ‘

โœ…

@pytest.mark.django_db marker runs the function-scoped _django_db_helper fixture.

Implement @pytest.mark.django_db_class_scope marker that runs a class-scoped fixture:

import pytest
from pytest_django.fixtures import _django_db_helper

_django_db_function_scope_helper = pytest.fixture(_django_db_helper.__wrapped__, scope='function')
_django_db_class_scope_helper = pytest.fixture(_django_db_helper.__wrapped__, scope='class')


@pytest.fixture()
def _django_db_helper(request) -> None:
    marker = request.node.get_closest_marker('django_db_class_scope')
    if not marker:
        request.getfixturevalue('_django_db_function_scope_helper')


@pytest.fixture(autouse=True)
def django_db_class_scope_marker(request) -> None:
    marker = request.node.get_closest_marker('django_db_class_scope')
    if marker:
        request.getfixturevalue('_django_db_class_scope_helper')

Usage:

# @pytest.mark.django_db
@pytest.mark.django_db_class_scope
class TestSignIn:
    def test_valid_user(self, django_user_model):
        django_user_model.objects.create()
        assert django_user_model.objects.count() == 1

    def test_sign_in(self, django_user_model):
        assert django_user_model.objects.count() == 1
๐Ÿ‘คaaron

0๐Ÿ‘

The problem is that the django_db_keepdb fixture is used to indicate that the database should not be destroyed after the test has completed. It does not prevent the database from being reset between tests. In order to reuse the same user between tests, you will need to use the django_db_reset_sequences fixture. This fixture will reset the database and keep the data, allowing you to reuse the same data in subsequent tests.

django_db_reset_sequences fixture in action:

@pytest.mark.django_db
class TestSignIn:
    def test_valid_user(self, client, valid_user_data, django_user_model, django_db_reset_sequences):
        client_signup(client, valid_user_data)
        response = activate_user(client)
        assert 'sign out' in get_text(response)

def test_sign_in(self, client, valid_user_data, django_user_model, django_db_reset_sequences):
    client_signup(client, valid_user_data)
    activate_user(client)
    response = client.post(reverse('signout'), follow=True)
    assert 'sign in' in get_text(response)
    response = client_sign_in(client, valid_user_data)
    assert 'sign out' in get_text(response)

You will also need to include the django_db_reset_sequences fixture in the list of arguments for each test function. This will reset the database and keep the data between tests, allowing you to reuse the same user in both tests.

๐Ÿ‘คSergio A. S.

Leave a comment