pytest: how to use a fixture to parametrize

时间:2018-09-18 20:03:54

标签: python python-3.x pytest

in conftest.py there is a fixture defined that returns a list:

@pytest.fixture(scope="session")
def targets_to_try():
    with open(
        "{}/xxx/cicd/xxx/manifest.yaml".format(os.environ["APPFOLDER"]), "rt"
    ) as f:
        manifest = yaml.safe_load(f.read())
        schema = Manifest.ManifestSchema()
        appobj = schema.load(manifest)
        return appobj.data["targets_to_try"]

In test file test_interfaces_cfg.py I would like to use the above fixture in order to parametrize the test function

import pytest

@pytest.mark.parametrize("target", targets_to_try)
def test_interfaces_cfg(manifest, target):
    pass

This causes the following error:

E   NameError: name 'targets_to_try' is not defined

Could you please advise how to achieve the desired behaviour ?

EDIT: if targets_to_try() is not a fixture in conftest.py then I get the following error:

    @pytest.mark.parametrize("target", targets_to_try())
E   NameError: name 'targets_to_try' is not defined

EDIT: directory layout is as follows:

$ tree
.
|-- attributes
|   `-- __init__.py
|-- bgp
|   `-- __init__.py
|-- Blueprint.py
|-- communities
|   `-- __init__.py
|-- conftest.py
|-- customize
|   `-- __init__.py
|-- Head.py
|-- interfaces
|   |-- __init__.py
|   `-- test_interfaces_configlet.py
|-- InterfacesIx.py
|-- InterfacesLacp.py
|-- manifest
|   |-- __init__.py
|   `-- test_schema.py
|-- Manifest.py
|-- NeighborIx.py
|-- policies
|   `-- __init__.py
|-- prefixes
|   `-- __init__.py
|-- pytest.ini
|-- Stack.py
`-- Tail.py

0 个答案:

没有答案