【问题标题】:fixture 'pylon_config_missing_usageplan' not found while running pytest-bdd运行 pytest-bdd 时找不到夹具“pylon_config_missing_usageplan”
【发布时间】:2021-08-30 09:04:34
【问题描述】:
    @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]' 寻求帮助。

谁能帮帮我?

【问题讨论】:

    标签: pytest pytest-bdd


    【解决方案1】:

    问题是,步骤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 实际上会尝试执行函数excute_pylon_config_missing_usageplan,它期待着fixture 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
    
    

    【讨论】:

      猜你喜欢
      • 2020-12-17
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2021-03-13
      • 1970-01-01
      • 2019-07-17
      相关资源
      最近更新 更多