假设我有两组用于BDD场景的测试数据。
dataX = A、B、C
dataY = 1,2,3,4
如何在没有在场景大纲中显式定义这些组合的情况下,针对dataY的所有组合运行一个场景?
目前,BDD场景如下所示,正如您所看到的,我正在显式定义所有可能的组合。如果值计数和/或变量计数增加,事情就会变得非常不切实际。我正在用python-behave library来做这个。
Scenario Outline: Test all data combinations - <dataX> <dataY>
Given combination <dataX> <dataY>
When do stuff with given combination <dataX> <dataY>
Then check result for <dataX> <dataY>
Examples: Input
| dataX | dataY |
| A | 1 |
| A | 2 |
| A | 3 |
| A | 4 |
| B | 1 |
| B | 2 |
| B | 3 |
| B | 4 |
| C | 1 |
| C | 2 |
| C | 3 |
| C | 4 | 注意,这需要通过场景大纲和示例来完成,以确保所有的测试数据覆盖。使用一个简单的场景并潜在地测试单个测试下的所有组合是不够的,因为它通常意味着测试在遇到的第一个失败时停止,跳过任何剩余的测试数据。
发布于 2021-10-04 14:32:53
在研究了behave库实现的核心之后,我找到了一些方法,人们可以使用这些方法来实现:编程构建Examples表。我的用例是需要测试的大量数据组合。
这是在environment.py中
from behave.model import Scenario, Examples, Table
from itertools import product
def before_feature(context, feature):
...
# data for each column for the Examples table; the way you get this is based on your specific case;
# you can read it from a file or generate it etc.; this here is merely an example
data_x = [data for test column dataX here]
data_y = [data for test column dataY here]
# create data tuples aka table rows; we will add these a bit later to the Examples table
# the way you build this is also specific to your case; I am using itertools.product for a quick way to get all possible pairs
all_data_pairs = [(dx, dy) for dx, dy in product(data_x, data_y)]
# identify the behave.model.Examples object we will populate
example: Examples = next(sc.examples[0] for sc in feature.scenarios if
sc.name == 'Test all data combinations - <dataX> <dataY>')
# Examples consists, amongst other things, of an object of type behave.model.Table
test_table: Table = example.table
# add test data to the Examples table, row by row
for row in all_data_pairs:
test_table.add_row(row)这将确保场景"Test all data combinations - <dataX> <dataY>"断言所有测试行。它不会在第一次失败之后停止。
发布于 2021-09-30 19:47:52
我不知道这是不是你想要的,但是你可以做这样的一般场景:
Feature: Test
Scenario: Test all data combinations
Given combination data is loaded
When combinations are processed
Then display results这将允许您在第一步(例如)加载数据。并在When步骤中执行组合过程(可能调用其他步骤)。
步骤实现可以如下所示:
from behave import *
from steps import data
@given('combination data is loaded')
def step_impl(context):
context.data_x = getattr(data, "dataX")
context.data_y = getattr(data, "dataY")
print(context.data_x)
print(context.data_y)
@when('combinations are processed')
def step_impl(context):
for x in context.data_x:
for y in context.data_y:
context.execute_steps(
"""
When process combination {x} and {y}
Then check result for given combination
""".format(x=x, y=y)
)
@then('display results')
def step_impl(context):
print(context.results)
@when('process combination {x} and {y}')
def step_impl(context, x, y):
context.x = x
context.y = y
@then('check result for given combination')
def step_impl(context):
context.results.append('.'.join([context.x, context.y]))运行此操作将提供:
>>behave --no-capture
Feature: Test # test.feature:1
Scenario: Test all data combinations # test.feature:2
Given combination data is loaded # steps/step_implementation.py:4
['A', 'B', 'C']
[1, 2, 3, 4]
When combinations are processed # steps/step_implementation.py:20
Then display results # steps/step_implementation.py:31
['A.1', 'A.2', 'A.3', 'A.4', 'B.1', 'B.2', 'B.3', 'B.4', 'C.1', 'C.2', 'C.3', 'C.4']
1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.016s发布于 2021-10-01 15:41:39
我会以不同的方式对待这件事。BDD的目的是检查应用程序的行为,这可以通过使用少量测试不同业务需求的特定数据集来实现。例如,如果要求添加2个数字,您将使用不同的数据集(如正数、负数、十进制数)测试该数字。你不会用所有可能的数字组合来测试。
但是,如果应用程序处理每个数据的方式与“是”不同,则必须测试所有数据组合。在这种情况下,最好定义大型数据集,而不是编写代码来生成数据集。原因是,最好明确说明应用程序将作为BDD步骤的一部分来处理什么,而不是隐藏在代码中,这只有团队的技术成员才能理解。这违背了使用BDD的目的。
对于非常大的数据集,您可以考虑维护excel中的数据。我已经展示了如何在NoCodeBDD (免责声明:我是NoCodeBDD的创始人) https://nocodebdd.live/examples-using-excel中做到这一点。在Cucumber中也可以这样做,但是您必须编写代码才能读取excel。
https://stackoverflow.com/questions/65630524
复制相似问题