Skip to content
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

Pytest scope='module' fixture not delete model instance after testing module #873

Closed
MaximMukhametov opened this issue Oct 5, 2020 · 1 comment

Comments

@MaximMukhametov
Copy link

MaximMukhametov commented Oct 5, 2020

I create the message instance in a fixture with scope='module', right in the test file. But when the test reaches another module, this message instance still exists in the database.

in .../apps/dialogs/test/api/test_message.py

@pytest.fixture(scope='module')
def message_by_auth_user(django_db_setup, django_db_blocker,
                         message_factory: type,
                         user_factory: type,
                         user_with_auth: User) -> Message:
    """Return message by auth user."""
    with django_db_blocker.unblock():
        message = message_factory(written_by=user_with_auth)  # Message object (1)
        message_text = message.message  # 'text_message_№_1'
        return message

in .../apps/users/test/api/test_users.py

@pytest.mark.django_db
def test_get_users_view_with_filter(bool_value: bool,
                                    user_count_change: int,
                                    filter_pattern: str,
                                    api_auth_client: APIClient,
                                    user_with_auth: User,
                                    user_factory: type):
    message_count = Message.objects.all().count()  # 1
    message = Message.objects.first()  # Message object (1)
    message_text = message.message # 'text_message_№_1'

After I replaced the 'return' with a 'yield', and after the 'yield' I manually deleted the object, everything works correctly. But shouldn't the test do it automatically, as it does in my other fixtures? For example, if scope = 'function' then the test automatically deletes the object (after each test), without any 'yield'

If the message instance is not manually deleted, it will exist throughout the entire session, even if scope='module'. Why is this happening???

@pytest.fixture(scope='module')
def message_by_auth_user(django_db_setup, django_db_blocker,
                         message_factory: type,
                         user_factory: type,
                         user_with_auth: User) -> Message:
    """Return message by auth user."""
    with django_db_blocker.unblock():
        message = message_factory(written_by=user_with_auth)
        yield message
        message.delete()  # This code is executed when fixture run teardown, after testing current module

why does function scope fixtures delete it automatically after each test? I expect module scope fixture to have the same behavior after each test module.

@bluetech
Copy link
Member

Doing DB things in non function-scopes fixtures is currently not reset automatically by pytest-django. Unfortunately it is not so simple to achieve. There are several issues about this, e.g. #514, #105 so I'll close this one.

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

2 participants