Skip to content

Commit dec056e

Browse files
committed
pytestplugin: make "target" a parametrized fixture
This allows users to run their pytest test scripts with an LG_ENV/--lg-env that has multiple targets instead of forcing the user to define one named "main" in order to run the tests. One of the downsides of this implementation is that autogenerated doc wouldn't define the "target" fixture anymore as it is entirely programmatically defined. Signed-off-by: Quentin Schulz <quentin.schulz@cherry.de>
1 parent b67bfc1 commit dec056e

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

labgrid/pytestplugin/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from .fixtures import pytest_addoption, env, target, strategy
1+
from .fixtures import pytest_addoption, env, pytest_generate_tests, strategy
22
from .hooks import pytest_configure, pytest_collection_modifyitems, pytest_cmdline_main

labgrid/pytestplugin/fixtures.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,38 @@ def pytest_addoption(parser):
5656
parser.addini("log_format", default=DEFAULT_FORMAT, help="Default value for log_format (overwritten by labgrid)")
5757

5858

59+
def pytest_generate_tests(metafunc):
60+
# Support running all tests from passed pytest files/directories for all
61+
# targets by generating a parametrized "target" fixture based on the name
62+
# of all targets in the passed lg_env file.
63+
if "target" in metafunc.fixturenames:
64+
env = metafunc.config.stash[LABGRID_ENV_KEY]
65+
66+
if not env:
67+
pytest.skip("missing environment config (use --lg-env)")
68+
69+
env_targets = list(env.config.get_targets().keys())
70+
71+
targets = []
72+
73+
for target_name in env_targets:
74+
target = env.get_target(target_name)
75+
if target is None:
76+
raise UserError(f"Using target fixture without '{target_name}' target in config")
77+
78+
targets.append(target)
79+
80+
metafunc.parametrize("target", targets, scope="session", ids=env_targets)
81+
82+
# This session fixture is programmatically defined within pytest_generate_tests
83+
# just above.
84+
#
85+
# @pytest.fixture(scope="session")
86+
# def target():
87+
# """Return the Target to run the test against
88+
# """
89+
# pass
90+
5991
@pytest.fixture(scope="session")
6092
def env(request, record_testsuite_property):
6193
"""Return the environment configured in the supplied configuration file.
@@ -116,17 +148,6 @@ def env(request, record_testsuite_property):
116148
sshmanager.close_all()
117149

118150

119-
@pytest.fixture(scope="session")
120-
def target(env):
121-
"""Return the default target `main` configured in the supplied
122-
configuration file."""
123-
target = env.get_target()
124-
if target is None:
125-
raise UserError("Using target fixture without 'main' target in config")
126-
127-
return target
128-
129-
130151
@pytest.fixture(scope="session")
131152
def strategy(request, target):
132153
"""Return the Strategy of the default target `main` configured in the

0 commit comments

Comments
 (0)