-
-
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
add markers/tags for migration tests #138
Changes from 3 commits
24b050c
bb1f2fd
0230b47
413a236
b9b6fb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,6 +221,34 @@ class TestDirectMigration(MigratorTestCase): | |
assert SomeItem.objects.filter(is_clean=True).count() == 1 | ||
``` | ||
|
||
### Choosing only migrations tests | ||
|
||
In CI systems it is important to get instant feedback. Running tests that | ||
apply database migration can slow down tests execution, so it is often a good | ||
idea to run standard, fast, regular unit tests without migrations in parallel | ||
with slower migrations tests. | ||
|
||
#### pytest | ||
|
||
`django_test_migrations` adds `migration_test` marker to each test using | ||
`migrator_factory` or `migrator` fixture. | ||
To run only migrations test, use `-m` option: | ||
|
||
```bash | ||
pytest -m migration_test | ||
``` | ||
|
||
#### unittest | ||
|
||
`django_test_migrations` adds `migration_test` | ||
[tag](https://docs.djangoproject.com/en/3.0/topics/testing/tools/#tagging-tests) | ||
to every `MigratorTestCase` subclass. | ||
To run only migrations tests, use `--tag` option: | ||
|
||
```bash | ||
python mange.py test --tag=migration_test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have something similar to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, there is a CLI option |
||
``` | ||
|
||
|
||
## Django Checks | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from typing_extensions import Final | ||
|
||
#: marker/tag indicating that marked test is Django's migrations test | ||
MIGRATION_TEST_MARKER: Final = 'migration_test' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import re | ||
import subprocess | ||
|
||
from django_test_migrations.constants import MIGRATION_TEST_MARKER | ||
|
||
|
||
def test_call_pytest_setup_plan(): | ||
"""Checks that module is registered and visible in the meta data.""" | ||
|
@@ -19,3 +22,44 @@ def test_call_pytest_setup_plan(): | |
|
||
assert 'migrator' in output_text | ||
assert 'migrator_factory' in output_text | ||
|
||
|
||
def test_pytest_registers_marker(): | ||
"""Ensure ``MIGRATION_TEST_MARKER`` marker is registered.""" | ||
output_text = subprocess.check_output( | ||
['pytest', '--markers'], | ||
stderr=subprocess.STDOUT, | ||
universal_newlines=True, | ||
encoding='utf8', | ||
) | ||
|
||
assert MIGRATION_TEST_MARKER in output_text | ||
|
||
|
||
def test_pytest_markers(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sobolevn what do you think about using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, sure! Let's open a new issue for it! 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added issue #140 |
||
"""Ensure ``MIGRATION_TEST_MARKER`` markers are properly added.""" | ||
output_text = subprocess.check_output( | ||
[ | ||
'pytest', | ||
'--collect-only', | ||
|
||
# Collect only tests marked with ``MIGRATION_TEST_MARKER`` marker | ||
'-m', | ||
MIGRATION_TEST_MARKER, | ||
|
||
# We need this part because otherwise check fails with `1` code: | ||
'--cov-fail-under', | ||
'0', | ||
], | ||
stderr=subprocess.STDOUT, | ||
universal_newlines=True, | ||
encoding='utf8', | ||
) | ||
|
||
search_result = re.search( | ||
r'(?P<selected_number>\d+)\s+selected', | ||
output_text, | ||
) | ||
assert search_result | ||
assert int(search_result.group('selected_number') or 0) > 0 | ||
assert 'test_pytest_plugin' in output_text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also give an example here how to ignore migration tests: