Jishnunand P K
Jishnunand P K

Reputation: 290

fixture 'pylon_config_missing_usageplan' not found while running pytest-bdd

    @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)
    

I have written multiple test case with specifying target_fixture in both @given scenario.

While running the test case it's throwing an error with

fixture 'pylon_config_missing_usageplan' not found

available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pylon_config, pytestbdd_given_I have provided a pylon config with a missing key, pytestbdd_given_I have provided a valid pylon config, pytestbdd_given_trace, pytestbdd_then_I should receive an exception, pytestbdd_then_It should be enriched with the expected FilterPatterns, pytestbdd_then_trace, pytestbdd_when_The configuration is loaded, pytestbdd_when_trace, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory use 'pytest --fixtures [testpath]' for help on them.

Can anyone help me over here?

Upvotes: 0

Views: 55

Answers (1)

mattefrank
mattefrank

Reputation: 261

The issue is, that the step When The configuration is loaded has two different implementations in code:

@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

The function excute_pylon_config_missing_usageplan overrides the step implementation of excute_pylon_config and therefore if you try to load the pylon config in scenario Loading a valid config, pytest-bdd actually tries to execute the function excute_pylon_config_missing_usageplan, which is expecting the fixture pylon_config_missing_usageplan (which is not available in this scenario ...)

Solutions

  1. Have two distinct steps for loading a valid/invalid config, e.g. When The configuration is loaded and When The invalid configuration is loaded (I would recommend this approach, since it is simpler and easier to read than solution 2)
  2. Add a variable to the step for loading the configuration which contains the type of the configuration

Example of variable in configuration loading step:

@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

Upvotes: 0

Related Questions