You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
frompytest_casesimportcaseascase_decoratorfrompytest_cases.case_funcsimportCASE_FIELDfrompytest_cases.case_funcsimportis_case_classfrompytest_cases.case_funcsimportis_case_functiondefwith_case_tags(*tags):
"""Attach `tags` to all cases defined in the decorated class."""def_decorator(cls):
ifis_case_function(cls):
raiseValueError(
'Cannot use with_case_tags on a case ''function. Use the @case decorator instead'
)
ifnotis_case_class(cls):
raiseValueError('with_case_tags can only be applied to classes ''defining a collection of cases')
forcase_nameindir(cls):
case_=getattr(cls, case_name)
ifnotis_case_function(case_): # Not a casecontinuetry:
case_info=getattr(case_, CASE_FIELD)
exceptAttributeError:
# Not explicitly decorated with @case. Do so now.case_=case_decorator(case_)
case_info=getattr(case_, CASE_FIELD)
tags_to_add=tuple(tfortintagsiftnotincase_info.tags)
case_info.add_tags(tags_to_add)
returnclsreturn_decorator
The text was updated successfully, but these errors were encountered:
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.
The text was updated successfully, but these errors were encountered: