pytest-django icon indicating copy to clipboard operation
pytest-django copied to clipboard

Is Django Testing Tags Supported?

Open erayerdin opened this issue 5 years ago • 6 comments
trafficstars

This is a question rather than a real issue. The documentation does not have anything about this and I couldn't find any regarding issue.

One can use tags for Django tests, is this supported?

erayerdin avatar Feb 25 '20 11:02 erayerdin

This seems related https://github.com/pytest-dev/pytest-django/issues/516

tolomea avatar Apr 17 '20 08:04 tolomea

@erayerdin you can use @pytest.mark.some_tag_here instead

graingert avatar Apr 17 '20 08:04 graingert

It would be nice if pytest-django translated one to the other. There are usecases where it's convenient that pytest is a no code change drop in replacement.

tolomea avatar Apr 17 '20 08:04 tolomea

looks like a tiny little collect hook to upgrade it to a mark

https://github.com/django/django/blob/8bcca47e8356521f52f0738d9633befd53007cae/django/test/utils.py#L844-L853

def pytest_collection_modifyitems(items):
    for item in items:
        for tag in getattr(item.obj, "tags", ()):
            item.add_marker(pytest.mark.django_tag(tag))

graingert avatar Apr 17 '20 08:04 graingert

I was able to get this to work (somewhat) by setting the following in conftest.py based on @graingert 's suggestion:

def pytest_collection_modifyitems(items):
    for item in items:
        for tag in getattr(item.obj, "tags", ()):
            item.add_marker(tag)

However, this only works for @tag applied to functions - it will not work with tags on an entire TestCase class. I suspect this is because pytest markers don't work on an entire class?

YPCrumble avatar Jul 28 '21 18:07 YPCrumble

we can add the following block of code to pytest_collection_modifyitems to add @tag valued as pytest markers but it raises an error if a tag is not registered in markers. Is it a good way to go? @bluetech do you have any idea?

hramezani avatar Nov 19 '21 14:11 hramezani