Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: apply case tags to all cases in class #351

Open
michele-riva opened this issue Sep 19, 2024 · 0 comments
Open

Proposal: apply case tags to all cases in class #351

michele-riva opened this issue Sep 19, 2024 · 0 comments

Comments

@michele-riva
Copy link
Contributor

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
michele-riva added a commit to viperleed/viperleed that referenced this issue Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant