|
1 | 1 | import pytest |
2 | | -from django.test import TestCase |
| 2 | +from django.test import TestCase, tag |
3 | 3 |
|
4 | 4 | from .helpers import DjangoPytester |
5 | 5 |
|
@@ -57,6 +57,41 @@ def tearDown(self) -> None: |
57 | 57 | assert Item.objects.count() == 3 |
58 | 58 |
|
59 | 59 |
|
| 60 | +@tag("tag1", "tag2") |
| 61 | +class TestDjangoTagsToPytestMarkers(TestCase): |
| 62 | + """Django test tags are converted to Pytest markers, at the class & method |
| 63 | + levels.""" |
| 64 | + |
| 65 | + @pytest.fixture(autouse=True) |
| 66 | + def gimme_my_markers(self, request: pytest.FixtureRequest) -> None: |
| 67 | + self.markers = {m.name for m in request.node.iter_markers()} |
| 68 | + |
| 69 | + @tag("tag3", "tag4") # type: ignore[misc] |
| 70 | + def test_1(self) -> None: |
| 71 | + assert self.markers == {"tag1", "tag2", "tag3", "tag4"} |
| 72 | + |
| 73 | + def test_2(self) -> None: |
| 74 | + assert self.markers == {"tag1", "tag2"} |
| 75 | + |
| 76 | + @tag("tag5") # type: ignore[misc] |
| 77 | + def test_3(self) -> None: |
| 78 | + assert self.markers == {"tag1", "tag2", "tag5"} |
| 79 | + |
| 80 | + |
| 81 | +@tag("tag1") |
| 82 | +class TestNonDjangoClassWithTags: |
| 83 | + """Django test tags are only converted to Pytest markers if actually |
| 84 | + Django tests. Use pytest markers directly for pytest tests.""" |
| 85 | + |
| 86 | + @pytest.fixture(autouse=True) |
| 87 | + def gimme_my_markers(self, request: pytest.FixtureRequest) -> None: |
| 88 | + self.markers = {m.name for m in request.node.iter_markers()} |
| 89 | + |
| 90 | + @tag("tag2") # type: ignore[misc] |
| 91 | + def test_1(self) -> None: |
| 92 | + assert not self.markers |
| 93 | + |
| 94 | + |
60 | 95 | def test_sole_test(django_pytester: DjangoPytester) -> None: |
61 | 96 | """ |
62 | 97 | Make sure the database is configured when only Django TestCase classes |
|
0 commit comments