Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] pytestplugin: make "target" a parametrized fixture #1554

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion labgrid/pytestplugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .fixtures import pytest_addoption, env, target, strategy
from .fixtures import pytest_addoption, env, pytest_generate_tests, strategy
from .hooks import pytest_configure, pytest_collection_modifyitems, pytest_cmdline_main
43 changes: 32 additions & 11 deletions labgrid/pytestplugin/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,38 @@ def pytest_addoption(parser):
parser.addini("log_format", default=DEFAULT_FORMAT, help="Default value for log_format (overwritten by labgrid)")


def pytest_generate_tests(metafunc):
# Support running all tests from passed pytest files/directories for all
# targets by generating a parametrized "target" fixture based on the name
# of all targets in the passed lg_env file.
if "target" in metafunc.fixturenames:
env = metafunc.config.stash[LABGRID_ENV_KEY]

if not env:
pytest.skip("missing environment config (use --lg-env)")

env_targets = list(env.config.get_targets().keys())

targets = []

for target_name in env_targets:
target = env.get_target(target_name)
if target is None:
raise UserError(f"Using target fixture without '{target_name}' target in config")

targets.append(target)

metafunc.parametrize("target", targets, scope="session", ids=env_targets)

# This session fixture is programmatically defined within pytest_generate_tests
# just above.
#
# @pytest.fixture(scope="session")
# def target():
# """Return the Target to run the test against
# """
# pass

@pytest.fixture(scope="session")
def env(request, record_testsuite_property):
"""Return the environment configured in the supplied configuration file.
Expand Down Expand Up @@ -116,17 +148,6 @@ def env(request, record_testsuite_property):
sshmanager.close_all()


@pytest.fixture(scope="session")
def target(env):
"""Return the default target `main` configured in the supplied
configuration file."""
target = env.get_target()
if target is None:
raise UserError("Using target fixture without 'main' target in config")

return target


@pytest.fixture(scope="session")
def strategy(request, target):
"""Return the Strategy of the default target `main` configured in the
Expand Down