-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
Inconsistent behaviour with data migration #330
Comments
I think that using Can you run two tests commands? One with |
That might be possible. Or somehow excluding those tests via In either case, do you know which part of this plugin could cause this? It seems to happen as soon as the |
No, I don't know. But, if you can figure that out, PR is totally welcome! |
I tracked this down to the use of the Removing it does not work. The tests then fail with:
|
I looked into It ran fine with See: https://pytest-django.readthedocs.io/en/latest/helpers.html#std-fixture-django_db_serialized_rollback and https://docs.djangoproject.com/en/stable/topics/testing/overview/#test-case-serialized-rollback |
@mschoettle so, is this working as you expect? |
One of our developers ran into this issue again despite the change to always use I realized today that the execution order of tests can be modified. In combination with from _pytest.config import Config
from _pytest.main import Session
from _pytest.python import Function, Module
def pytest_collection_modifyitems(session: Session, config: Config, items: list[Function]) -> None:
"""
Change the execution order of tests.
Ensure that migration tests run last.
This is to avoid the migrator from `django-test-migrations` to cause data migrations to be flushed.
See: https://github.com/wemake-services/django-test-migrations/issues/330
See pytest docs: https://docs.pytest.org/en/latest/reference/reference.html#pytest.hookspec.pytest_collection_modifyitems
Args:
session: the pytest session
config: the pytest configuration
items: the items to test (test functions and test classes)
"""
migration_tests = []
original_items_order = []
for item in items:
# some test functions are wrapped within a class
module = item.getparent(Module)
if module and module.name == 'test_migrations.py':
migration_tests.append(item)
else:
original_items_order.append(item)
# modify items in place with migration tests moved to the end
items[:] = original_items_order + migration_tests |
@mschoettle have you tried to run migrations tests separately from other tests? pytest -m migration_test and to run all other tests: pytest -m not migration_test
# or
pytest --no-migrations I know it's a bit more problematic, because of 2 separate processes (runs) of pytest, but it's pretty hard to maintain the DB state when running migrations tests. Reference: https://github.com/wemake-services/django-test-migrations#choosing-only-migrations-tests BTW I will experiment a bit with serialized rollback, maybe it will help somehow 👍 |
We added a data migration and are testing it using django-test-migrations. When running pytest with
--create-db
everything works fine. But when running it with--reuse-db
the data added by the data migration is gone. Since this only happens when migration tests are part of the suite it seems that this is something that happens in this plugin.Is there anything that could trigger this? Maybe: https://github.com/wemake-services/django-test-migrations/blob/master/django_test_migrations/migrator.py#L47
Probably unrelated but I noticed that
Migrator.reset()
causes the last migration to be re-applied (after a migration test that starts with the second last and applies the last one):The call to the
migrate
command will cause0002
to be re-applied. If 0002 is a data migration it will attempt to add the same data again.The text was updated successfully, but these errors were encountered: