繁体   English   中英

行为模拟场景大纲 output 与 execute_steps

[英]Behave mimic scenario outline output with execute_steps

简短的上下文:

在我公司,我们使用脚本来生成一系列 PDF 文件。 此脚本从 JSON 文件中获取它应该构建的 PDF。 我的任务是创建一个自动化测试,以便在 Jenkins 中运行构建(和脚本)时捕获丢失的文件。 我创建了一个加载 JSON 文件并使用该信息调用检查文件是否存在的步骤的测试。 我的问题是我想实际重现使用场景大纲时得到的行为,这不仅是为了测试一个值,而且是打印到 output 哪些文件已经过测试。 简而言之:我想要一个带有动态示例的场景大纲(不幸的是,链接的问题没有提供答案)。

假设我有以下功能文件:

Feature: Verify squared numbers

  Scenario Outline: Verify square for <number>
    Then the <number> squared is <result>

Examples:
  | number | result |
  |   1    |    1   | 
  |   2    |    4   |
  |   3    |    9   |
  |   4    |   16   |

和步骤文件:

from behave import step

@step('the {number:d} squared is {result:d}')
def step_impl(context, number, result):
    assert number*number == result

我明白了

Feature: Verify squared numbers # x.feature:1

  Scenario Outline: Verify square for 1 -- @1.1   # x.feature:8
    Then the 1 squared is 1                       # steps/x.py:10

  Scenario Outline: Verify square for 2 -- @1.2   # x.feature:9
    Then the 2 squared is 4                       # steps/x.py:10

  Scenario Outline: Verify square for 3 -- @1.3   # x.feature:10
    Then the 3 squared is 9                       # steps/x.py:10

  Scenario Outline: Verify square for 4 -- @1.4   # x.feature:11
    Then the 4 squared is 16                      # steps/x.py:10

  Scenario: Verify something              # x.feature:13
    Given I use the data from "data.json" # steps/x.py:5
    Then everything is allright           # steps/x.py:14

1 feature passed, 0 failed, 0 skipped
5 scenarios passed, 0 failed, 0 skipped
6 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.010s

这非常好,我可以看到测试了哪些值(更重要的是,这些值是在我正在导出的测试报告中捕获的)。

现在我已将我的功能更改为:

Feature: Verify squared numbers

  Scenario: Verify something 
    Given I use the data from "data.json"
     Then everything is alright

还有我的步骤文件:

from behave import step
import json

@step('I use the data from "{file}"')
def step_impl(context, file):
    with open(file) as json_file:
        context.json_data = json.load(json_file)

@step('the {number:d} squared is {result:d}')
def step_impl(context, number, result):
    assert number*number == result

@step('everything is alright')
def step_impl(context):
    for number, result in context.json_data.items():
        context.execute_steps(f'Then the {number} squared is {result}')

data.json

{"1": 1, "2": 4, "3": 9}

我得到

Feature: Verify squared numbers # x.feature:1

  Scenario: Verify something              # x.feature:3
    Given I use the data from "data.json" # steps/x.py:5
    Then everything is alright            # steps/x.py:14

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
2 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.006s

这只是一项测试,与执行的步骤数无关。

我怎样才能获得与我在场景大纲中获得的类似的 output?

非常感谢!!

我认为目前不支持此功能。 有一些方法可以使用 jinja 模板自动生成场景大纲。 但是,我做了一些不同的事情来获得所有结果,而不是在第一次断言时失败 - 收集所有不正确的值,然后断言所有这些:

@step('everything is alright')
def step_impl(context):
    results = []
    for number, result in context.json_data.items():
        if int(number)*int(number) != result:
            results.append({"expected": int(number)*int(number), "actual": result})

    assert not results, f'Wrongly calculated values: {results}'

结果将如下所示:

    Then everything is alright            # features/steps/all_steps.py:17 0.000s
      Assertion Failed: Wrongly calculated values: [{'expected': 9, 'actual': 8}, {'expected': 16, 'actual': 11}]

我知道打印输出不是您希望看到的,但至少是您可以扩展/更改的一些解决方案。

如果您只想在控制台中查看正在发生的事情 - 也许只是简单的日志记录:

logging.info(f'Then the {number} squared is {result}')

然后需要使用--no-logcapture参数运行行为。 也许,你已经尝试过了。 Output 将类似于:

  Scenario: Verify something              # features/square_numbers.feature:3
    Given I use the data from "data.json" # features/steps/all_steps.py:6 0.000s
    Then everything is alright            # features/steps/all_steps.py:17
INFO:root:Then the 1 squared is 1
INFO:root:Then the 2 squared is 4
INFO:root:Then the 3 squared is 8
    Then everything is alright     

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM