问题描述
@scenario('../features/config.feature','Loading a valid config')
def test_config():
pass
@given("I have provided a valid pylon config",target_fixture="pylon_config")
def pylon_config():
input_data = {
}
return input_data
@when("The configuration is loaded")
def excute_pylon_config(pylon_config):
createFilterPattern(pylon_config)
@then("It should be enriched with the expected FilterPatterns")
def no_error_message(pylon_config):
test_data1= {
}
test_data_2 = {
}
result = pylon_config
@scenario('../features/config.feature','Missing usagePlan section')
def test_missing_usageplan():
pass
@given("I have provided a pylon config with a missing key",target_fixture="pylon_config_missing_usageplan")
def pylon_config_missing_usageplan():
input_data = {
'metricFilters': {
'defaults': {
'xyz': []
}
}
}
return input_data
@when("The configuration is loaded")
def excute_pylon_config_missing_usageplan(pylon_config_missing_usageplan):
try:
createFilterPattern(pylon_config_missing_usageplan)
except KeyError:
pass
@then("I should receive an exception")
def error_message_pylon_config_missing_usageplan(pylon_config_missing_usageplan):
print(pylon_config_missing_usageplan)
我编写了多个测试用例,并在两个@given 场景中指定了 target_fixture。
运行测试用例时抛出错误
未找到夹具“pylon_config_missing_usageplan”
可用设备:cache、capfd、capfdbinary、caplog、capsys、capsysbinary、doctest_namespace、monkeypatch、pylon_config、pytestbdd_given_I 提供了一个缺少密钥的pylon 配置,pytestbdd_given_I 提供了一个有效的pylon 配置,pytestbdd_given_I 提供了一个有效的pylon 配置,pytestbdd_gven_接收异常,pytestbdd_then_应该丰富了预期的FilterPatterns,pytestbdd_then_trace,pytestbdd_when_配置被加载,pytestbdd_when_trace,pytestconfig,record_property,record_testsuite_property,record_xml_attribute,recwarn,tmpymt_pathfactor,pytestbdd_when_trace 使用 'pytest --fixtures [testpath]' 获得帮助。
有人可以帮我吗?
解决方法
问题是,步骤 When The configuration is loaded
在代码中有两种不同的实现:
@when("The configuration is loaded")
def excute_pylon_config(pylon_config):
createFilterPattern(pylon_config)
@when("The configuration is loaded")
def excute_pylon_config_missing_usageplan(pylon_config_missing_usageplan):
try:
createFilterPattern(pylon_config_missing_usageplan)
except KeyError:
pass
函数 excute_pylon_config_missing_usageplan
覆盖了 excute_pylon_config
的步骤实现,因此如果您尝试在场景 Loading a valid config
中加载 pylon 配置,pytest-bdd 实际上会尝试执行函数 {{1 }},它期待灯具 excute_pylon_config_missing_usageplan
(在这种情况下不可用......)
解决方案
- 加载有效/无效配置有两个不同的步骤,例如
pylon_config_missing_usageplan
和When The configuration is loaded
(我推荐这种方法,因为它比解决方案 2 更简单易读) - 在加载配置的步骤中添加一个变量,其中包含配置的类型
配置加载步骤中的变量示例:
When The invalid configuration is loaded