Make system tests compatible with pytest 8.0.0+

The pytest collection mechanism has been overhauled in pytest 8.0.0,
resulting in a different node tree when collecting the tests. Ensure the
paths / names we're using that are derived from the node tree are
consistent across different pytest versions.

Particularly, this has affected the convenience symlink name (which is
supposed to be in the form of e.g. dns64_sh_dns64 for the dns64 module
and tests_sh_dns64.py module) and the test name that's logged at the
start of the test, which is supposed to include the system test
directory relative to the root system test directory as well as the
module name (e.g. dns64/tests_sh_dns64.py).

Related https://github.com/pytest-dev/pytest/issues/7777

(cherry picked from commit 7118cbed98)
This commit is contained in:
Nicki Křížek 2024-05-27 16:10:04 +02:00
parent a25918c9f5
commit 2a0c3c8c14

View file

@ -402,7 +402,7 @@ def system_test_dir(request, env, system_test_name):
shutil.copytree(system_test_root / system_test_name, testdir)
# Create a convenience symlink with a stable and predictable name
module_name = SYMLINK_REPLACEMENT_RE.sub(r"\1", request.node.name)
module_name = SYMLINK_REPLACEMENT_RE.sub(r"\1", str(_get_node_path(request.node)))
symlink_dst = system_test_root / module_name
unlink(symlink_dst)
symlink_dst.symlink_to(os.path.relpath(testdir, start=system_test_root))
@ -495,6 +495,15 @@ def _run_script(
isctest.log.debug(" exited with %d", returncode)
def _get_node_path(node) -> Path:
if isinstance(node.parent, pytest.Session):
if _pytest_major_ver >= 8:
return Path()
return Path(node.name)
assert node.parent is not None
return _get_node_path(node.parent) / node.name
@pytest.fixture(scope="module")
def shell(env, system_test_dir):
"""Function to call a shell script with arguments."""
@ -594,7 +603,7 @@ def system_test(
pytest.fail(f"get_core_dumps.sh exited with {exc.returncode}")
os.environ.update(env) # Ensure pytests have the same env vars as shell tests.
isctest.log.info(f"test started: {request.node.name}")
isctest.log.info(f"test started: {_get_node_path(request.node)}")
port = int(env["PORT"])
isctest.log.info("using port range: <%d, %d>", port, port + PORTS_PER_TEST - 1)