Skip to content

Commit

Permalink
Convert Django test tags to Pytest markers
Browse files Browse the repository at this point in the history
Django TestCase supports tags:
https://docs.djangoproject.com/en/4.2/topics/testing/tools/#topics-tagging-tests
These are basically similar to (basic) Pytest tags, so let's interpret
them to allow using the native pytest-native markers functionality.
This helps projects which are unable to convert tags to markers.

This may cause breakage for projects using `strict-markers`. Such
projects would need to add the tags to their `markers` config, or deal
with it some other way.

Fix #818.
  • Loading branch information
bluetech committed Oct 29, 2023
1 parent d8be0ef commit 9d2df82
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ addopts = [
]
DJANGO_SETTINGS_MODULE = "pytest_django_test.settings_sqlite_file"
testpaths = ["tests"]
markers = ["tag1", "tag2", "tag3", "tag4", "tag5"]

[tool.mypy]
strict = true
Expand Down
42 changes: 42 additions & 0 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,48 @@ def pytest_report_header(config: pytest.Config) -> Optional[List[str]]:
return None


# Convert Django test tags on test classes to pytest marks.
def pytest_collectstart(collector: pytest.Collector) -> None:
if "django" not in sys.modules:
return

if not isinstance(collector, pytest.Class):
return

tags = getattr(collector.obj, "tags", ())
if not tags:
return

from django.test import TransactionTestCase

if not issubclass(collector.obj, TransactionTestCase):
return

Check warning on line 395 in pytest_django/plugin.py

View check run for this annotation

Codecov / codecov/patch

pytest_django/plugin.py#L395

Added line #L395 was not covered by tests

for tag in tags:
collector.add_marker(tag)


# Convert Django test tags on test methods to pytest marks.
def pytest_itemcollected(item: pytest.Item) -> None:
if "django" not in sys.modules:
return

if not isinstance(item, pytest.Function):
return

tags = getattr(item.obj, "tags", ())
if not tags:
return

from django.test import TransactionTestCase

if not issubclass(item.cls, TransactionTestCase):
return

Check warning on line 416 in pytest_django/plugin.py

View check run for this annotation

Codecov / codecov/patch

pytest_django/plugin.py#L416

Added line #L416 was not covered by tests

for tag in tags:
item.add_marker(tag)


@pytest.hookimpl(tryfirst=True)
def pytest_collection_modifyitems(items: List[pytest.Item]) -> None:
# If Django is not configured we don't need to bother
Expand Down
20 changes: 19 additions & 1 deletion tests/test_unittest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from django.test import TestCase
from django.test import TestCase, tag

from .helpers import DjangoPytester

Expand Down Expand Up @@ -57,6 +57,24 @@ def tearDown(self) -> None:
assert Item.objects.count() == 3


@tag("tag1", "tag2")
class TestDjangoTagsToPytestMarkers(TestCase):
@pytest.fixture(autouse=True)
def set_markers(self, request: pytest.FixtureRequest) -> None:
self.markers = {m.name for m in request.node.iter_markers()}

@tag("tag3", "tag4") # type: ignore[misc]
def test_1(self) -> None:
assert self.markers == {"tag1", "tag2", "tag3", "tag4"}

def test_2(self) -> None:
assert self.markers == {"tag1", "tag2"}

@tag("tag5") # type: ignore[misc]
def test_3(self) -> None:
assert self.markers == {"tag1", "tag2", "tag5"}


def test_sole_test(django_pytester: DjangoPytester) -> None:
"""
Make sure the database is configured when only Django TestCase classes
Expand Down

0 comments on commit 9d2df82

Please sign in to comment.