Search code examples
pythonpytestbddpytest-bdd

Get scenario name in pytest_bdd_apply_tag method


In case of @todo tag I need to send a request to Zephyr Scale that the test is skipped.

def pytest_bdd_apply_tag(tag, function):
    env = os.environ.get(ENV_VARIABLE)
    if tag == 'todo':
        marker = pytest.mark.skip(reason="Not implemented yet")
        marker(function)
        ## Get issue number
        # match = re.search(r'\[([A-Z]+-[^\]]+)\]', scenario.name)
        ## Send request to Zephyr Scale
        # ZephyrScale(match.group(1)).set_skipped()
        return True
    else:
        # Fall back to the default behavior of pytest-bdd
        return None

Is there any options to get Scenario name?


Solution

  • Resolved by extending todo-tag

    https://pytest-bdd.readthedocs.io/en/latest/

    Allow spaces in tag names. This can be useful when using the pytest_bdd_apply_tag hook with tags like @xfail: Some reason.

        @todo: /{TEST CYCLE}/ [TEST-CASE] CSRF Token invalid
        Scenario: [TEST-CASE] ....
            When ...
    
    def pytest_bdd_apply_tag(tag, function):
        env = os.environ.get(ENV_VARIABLE)
        if tag.startswith("todo"):
            marker = pytest.mark.skip(reason="Not implemented yet")
            marker(function)
            match = re.search(r'/(.*?)/ \[([A-Z]+-[^\]]+)\] (.*?)', tag)
            if match:
                ZephyrScale(match.group(1)).set_skipped(match.group(2), match.group(3))
            return True
        else:
            # Fall back to the default behavior of pytest-bdd
            return None