-2

我有很多功能,并且想控制标签列表,以防止拼写错误和其他命名问题。有没有办法在我的所有功能中获取标签列表?

所以有

@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

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!

我会得到标签列表['opponent', 'wip', 'dummy']

4

2 回答 2

2

你可以通过before_scenario在你的钩子中environment.py迭代场景的标签来做到这一点:

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

您需要一个类似的钩子来迭代功能的标签。

或者,如果您不关心标签是否附加到场景或功能,您可以使用before_tag挂钩而不是上述两个挂钩:

def before_tag(context, tag):
    if tag not in (... tuple of valid values...):
        raise ValueError("unexpected tag: " + tag)
于 2015-10-19T16:37:30.337 回答
0

我使用以下 shell 命令来获取我在所有功能文件中使用的所有标签的列表:

grep -r --include="*.feature" -h "^\s*@[[:alnum:]._=:,;()-]\+" | tr "[ \r]" "\n" | sed '/^$/d' | sort | uniq -c

解释:

  • 递归grep匹配特征文件中以标签开头的所有行,而不打印文件名 ( -h)。正则表达式是根据有效标签的定义,here
  • 将空格转换为换行符使用tr (对我来说,回车也换行符,因为我在 WSL 中并且我的功能文件有 CRLF 结尾)
  • 用 . 删除空行sed
  • 种类。
  • 使用 删除重复项uniq,并打印遇到每个标签的次数 ( -c)。

输出看起来像这样:

     13 @bar
      5 @baz
      2 @disabled
      1 @foo
      1 @wip
于 2020-11-12T12:31:53.553 回答