Open
Description
Hi @smarie,
I found myself coming up with the following class decorator that allows applying a bunch of case tags to all the cases defined in a case class. It has the advantage to reduce code repetition if all cases in a class are to be tagged similarly or have a common set of tags.
Question 1: I did not find anything obvious to do the following already in pytest-cases. Did I overlook something?
Question 2: (if answer to previous is: "no it's not currently possible") Would it be something interesting for pytest-cases? If yes, I'm open to drafting a PR.
from pytest_cases import case as case_decorator
from pytest_cases.case_funcs import CASE_FIELD
from pytest_cases.case_funcs import is_case_class
from pytest_cases.case_funcs import is_case_function
def with_case_tags(*tags):
"""Attach `tags` to all cases defined in the decorated class."""
def _decorator(cls):
if is_case_function(cls):
raise ValueError(
'Cannot use with_case_tags on a case '
'function. Use the @case decorator instead'
)
if not is_case_class(cls):
raise ValueError('with_case_tags can only be applied to classes '
'defining a collection of cases')
for case_name in dir(cls):
case_ = getattr(cls, case_name)
if not is_case_function(case_): # Not a case
continue
try:
case_info = getattr(case_, CASE_FIELD)
except AttributeError:
# Not explicitly decorated with @case. Do so now.
case_ = case_decorator(case_)
case_info = getattr(case_, CASE_FIELD)
tags_to_add = tuple(t for t in tags if t not in case_info.tags)
case_info.add_tags(tags_to_add)
return cls
return _decorator
Metadata
Metadata
Assignees
Labels
No labels