随着项目的推进,QA 设计的测试用例也会越来越多,一个 Step 可能会应用到多个 Scenarios 中,当这个 Step 的描述或实现发生修改,很有可能会影响到其它 Scenarios,那么有什么办法可以快速检查受影响的 Scenarios 是否有异常呢?众所周知,Python 是解释性语言,只有在运行的时候才能发现问题,不像 Java,C# 编译性语言编译过程中能发现问题了。把所有的 Scenarios 运行一遍太耗时,人工查看一遍更是不可靠,别着急,Behave 框架的 --dry-run 可以很轻松的解决这个问题。
想了解更多 Behave 相关的文章,欢迎阅读《Python BDD Behave 系列》,持续更新中。
dry-run 是 Behave 测试框架的一个选项,用于模拟运行测试而无需实际执行步骤中的代码。执行 Behave 命令时添加 --dry-run 选项,将会输出测试执行的计划,但不会执行实际的测试步骤代码。这个选项对于以下一些场景非常有用:
测试计划验证:
通过 --dry-run 可以查看 Behave 在执行测试时计划执行的具体步骤,以确保测试覆盖了预期的场景和步骤。
步骤匹配检查:
Behave 在执行时会尝试匹配测试步骤与定义的步骤实现(Step Definitions)。使用 --dry-run 可以检查 Behave 是否能够正确匹配所有的测试步骤,以防止由于步骤匹配问题而导致的测试失败。
快速检查场景:
在大型测试套件中,通过 --dry-run 可以快速查看所有场景的执行计划,而无需实际运行测试。这对于快速检查测试计划的正确性很有帮助。
以下是一个使用 --dry-run 的示例:
behave --dry-run
在 --dry-run 模式下,Behave 将输出执行计划,但不会运行测试步骤中的代码。这使得你可以在执行实际测试之前进行一些验证和检查。
项目结构如下,还是用简单的计算器例子吧
创建 calculator.feature 文件
# calculator.feature
Feature: Calculator Addition
In order to verify that the calculator can perform addition
As a user
I want to ensure that the addition operation is correct
@test_hooks_tag
Scenario: Add two numbers
Given the calculator is turned on
When I add 5 and 7
Then the result should be 12
创建 calculator_steps.py 文件
# calculator_steps.py
from behave import given, when, then
@given('the calculator is turned on')
def step_calculator_turned_on(context):
context.calculator_on = True
@when('I add {num1:d} and {num2:d}')
def step_add_numbers(context, num1, num2):
context.result = num1 + num2
@then('the result should be {expected_result:d}')
def step_check_result(context, expected_result):
assert context.result == expected_result, f"Actual result: {context.result}, Expected result: {expected_result}"
上面的 feature 文件中的 Gherkin steps 和 steps 实现是 match 的,执行命令:behave Features/Calculator --dry-run
终端输出没有看到异常,整个测试用例的场景设计一目了然,features/scenarios/steps 级别都是 untested, 没有真正执行测试用例。
PS C:\Automation\Test\bdd> behave Features/Calculator --dry-run
Feature: Calculator Addition # Features/Calculator/calculator.feature:3
In order to verify that the calculator can perform addition
As a user
I want to ensure that the addition operation is correct
@test_hooks_tag
Scenario: Add two numbers # Features/Calculator/calculator.feature:9
Given the calculator is turned on # steps/calculator_steps.py:5
When I add 5 and 7 # steps/calculator_steps.py:9
Then the result should be 12 # steps/calculator_steps.py:13
0 features passed, 0 failed, 0 skipped, 1 untested
0 scenarios passed, 0 failed, 0 skipped, 1 untested
0 steps passed, 0 failed, 0 skipped, 0 undefined, 3 untested
Took 0m0.000s
为了制造点异常,feature 文件中的 Gherkin step 修改一下,但是 step 文件中的具体实现不做任何改动。
修改:When I add 5 and 7 --》When I added
5 and 7
新增一个 step:And the calculator is turned off
可以看到界面上 Behave 已经识别到了异常了,用红色字体标注了,这个时候我们执行命令:behave Features/Calculator --dry-run
终端可以看到 steps 级别有 2 undefined,而且会输出哪 2 个 steps 是有异常的。
PS C:\Automation\Test\bdd> behave Features/Calculator --dry-run
Feature: Calculator Addition # Features/Calculator/calculator.feature:3
In order to verify that the calculator can perform addition
As a user
I want to ensure that the addition operation is correct
@test_hooks_tag
Scenario: Add two numbers # Features/Calculator/calculator.feature:9
Given the calculator is turned on # steps/calculator_steps.py:5
When I added 5 and 712 # steps/calculator_steps.py:13
Then the result should be 12 # None
And the calculator is turned off # None
0 features passed, 0 failed, 0 skipped, 1 untested
0 scenarios passed, 0 failed, 0 skipped, 1 untested
0 steps passed, 0 failed, 0 skipped, 2 undefined, 2 untested
Took 0m0.000s
You can implement step definitions for undefined steps with these snippets:
@when(u'I added 5 and 7')
def step_impl(context):
raise NotImplementedError(u'STEP: When I added 5 and 7')
@then(u'the calculator is turned off')
def step_impl(context):
raise NotImplementedError(u'STEP: Then the calculator is turned off')