0
    @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”

可用的固定装置:缓存、capfd、capfdbinary、caplog、capsys、capsysbinary、doctest_namespace、monkeypatch、pylon_config、pytestbdd_given_我提供了一个缺少密钥的 pylon 配置,pytestbdd_given_我提供了一个有效的 pylon 配置,pytestbdd_given_trace,pytestbdd_then_我应该收到一个异常,pytestbdd_then_它应该使用预期的 FilterPatterns、pytestbdd_then_trace、pytestbdd_when_加载配置、pytestbdd_when_trace、pytestconfig、record_property、record_testsuite_property、record_xml_attribute、recwarn、tmp_path、tmp_path_factory、tmpdir、tmpdir_factory 使用“pytest --fixtures [testpath]”获取帮助。

有人可以帮我吗?

4

1 回答 1

0

问题是,该步骤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覆盖了 step 的实现,excute_pylon_config因此如果您尝试在场景中加载 pylon 配置Loading a valid config,pytest-bdd 实际上会尝试执行该函数excute_pylon_config_missing_usageplan,该函数期待夹具pylon_config_missing_usageplan(在此场景中不可用......)

解决方案

  1. 有两个不同的步骤来加载有效/无效的配置,例如When The configuration is loadedWhen The invalid configuration is loaded(我会推荐这种方法,因为它比解决方案 2 更简单、更容易阅读)
  2. 在加载配置的步骤中添加一个变量,其中包含配置的类型

配置加载步骤中的变量示例:

@when(parsers.parse("The {config_type} configuration is loaded"))
def excute_pylon_config(request, config_type):
    if config_type == 'valid':
        # Retrieve fixture dynamically by name
        pylon_config = request.getfixturevalue('pylon_config')
        createFilterPattern(pylon_config)
    else:
        # Retrieve fixture dynamically by name
        pylon_config_missing_usageplan = request.getfixturevalue('pylon_config_missing_usageplan')
        try: 
            createFilterPattern(pylon_config_missing_usageplan)
        except KeyError:
            pass

于 2021-07-12T10:40:49.903 回答