Search code examples
pythonbddpython-behave

How can I run checks on all the tags used in my features?


I have many features, and want to control the list of tags, to prevent typos and other naming issues. Is there any way to get the list of tags in in all my features?

So having

@opponent
Feature: Fight or flight
  In order to increase the ninja survival rate,
  As a ninja commander
  I want my ninjas to decide whether to take on an
  opponent based on their skill levels

  Scenario: Weaker opponent
    Given the ninja has a third level black-belt
    When attacked by a samurai
    Then the ninja should engage the opponent

  @wip
  Scenario: Stronger opponent
    Given the ninja has a third level black-belt
    When attacked by Chuck Norris
    Then the ninja should run for his life

And

Feature: showing off behave

  @dummy
  Scenario: run a simple test
    Given we have behave installed
     when we implement a test
     then behave will test it for us!

I'd get the list of tags ['opponent', 'wip', 'dummy'].


Solution

  • You could do it by having a before_scenario hook in your environment.py which would iterate over the tags of the scenario:

    def before_scenario(context, scenario):
        for tag in scenario.tags:
            if tag not in (... tuple of valid values ...):
                raise ValueError("unexpected tag: " + tag)
    

    You'd want a similar hook for iterating over the tags of a feature.

    Or if you don't care whether the tags are attached to scenarios or features, you could use a before_tag hook instead of the above two hooks:

    def before_tag(context, tag):
        if tag not in (... tuple of valid values...):
            raise ValueError("unexpected tag: " + tag)