From 8d434891066aa1b1eec0e0ad0adc65a0dfa818e2 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 13 May 2023 12:00:19 -0300 Subject: [PATCH 1/2] Fix tests for pytest-xdist 3.3.0 --- tests/test_subtests.py | 12 ++++++------ tox.ini | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/test_subtests.py b/tests/test_subtests.py index 219b78a..0ca0317 100644 --- a/tests/test_subtests.py +++ b/tests/test_subtests.py @@ -29,7 +29,7 @@ def test_simple_terminal_normal(self, simple_script, testdir, mode): else: pytest.importorskip("xdist") result = testdir.runpytest("-n1") - expected_lines = ["gw0 [1]"] + expected_lines = ["1 worker [1 item]"] expected_lines += [ "* test_foo [[]custom[]] (i=1) *", @@ -54,7 +54,7 @@ def test_simple_terminal_verbose(self, simple_script, testdir, mode): pytest.importorskip("xdist") result = testdir.runpytest("-n1", "-v") expected_lines = [ - "gw0 [1]", + "1 worker [1 item]", "*gw0*100%* test_simple_terminal_verbose.py::test_foo*", "*gw0*100%* test_simple_terminal_verbose.py::test_foo*", "*gw0*100%* test_simple_terminal_verbose.py::test_foo*", @@ -87,7 +87,7 @@ def test_foo(subtests): else: pytest.importorskip("xdist") result = testdir.runpytest("-n1") - expected_lines = ["gw0 [1]"] + expected_lines = ["1 worker [1 item]"] expected_lines += ["* 1 passed, 3 skipped, 2 subtests passed in *"] result.stdout.fnmatch_lines(expected_lines) @@ -108,7 +108,7 @@ def test_foo(subtests): else: pytest.importorskip("xdist") result = testdir.runpytest("-n1") - expected_lines = ["gw0 [1]"] + expected_lines = ["1 worker [1 item]"] expected_lines += ["* 1 passed, 3 xfailed, 2 subtests passed in *"] result.stdout.fnmatch_lines(expected_lines) @@ -159,7 +159,7 @@ def test_simple_terminal_normal(self, simple_script, testdir, runner): else: pytest.importorskip("xdist") result = testdir.runpytest(simple_script, "-n1") - expected_lines = ["gw0 [1]"] + expected_lines = ["1 worker [1 item]"] result.stdout.fnmatch_lines( expected_lines + [ @@ -201,7 +201,7 @@ def test_simple_terminal_verbose(self, simple_script, testdir, runner): pytest.importorskip("xdist") result = testdir.runpytest(simple_script, "-n1", "-v") expected_lines = [ - "gw0 [1]", + "1 worker [1 item]", "*gw0*100%* SUBFAIL test_simple_terminal_verbose.py::T::test_foo*", "*gw0*100%* SUBFAIL test_simple_terminal_verbose.py::T::test_foo*", "*gw0*100%* PASSED test_simple_terminal_verbose.py::T::test_foo*", diff --git a/tox.ini b/tox.ini index f7d9c72..a2d7608 100644 --- a/tox.ini +++ b/tox.ini @@ -8,7 +8,7 @@ passenv = TRAVIS PYTEST_ADDOPTS deps = - pytest-xdist>=1.28 + pytest-xdist>=3.3.0 commands = pytest {posargs:tests} From cf7f1fa9c1e30d2103ef1bbdd54ab23eb5030f76 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 13 May 2023 11:45:00 -0300 Subject: [PATCH 2/2] Passing subtests no longer turn the output yellow (similar to warnings) Fix #86 --- CHANGELOG.rst | 4 ++++ pytest_subtests.py | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 647c3ee..65df75d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,9 +4,13 @@ CHANGELOG UNRELEASED ---------- +* Passing subtests no longer turn the pytest output to yellow (as if warnings have been issued) (`#86`_). Thanks to `Andrew-Brock`_ for providing the solution. * Now the ``msg`` contents of a subtest is displayed when running pytest with ``-v`` (`#6`_). .. _#6: https://github.com/pytest-dev/pytest-subtests/issues/6 +.. _#86: https://github.com/pytest-dev/pytest-subtests/issues/86 + +.. _`Andrew-Brock`: https://github.com/Andrew-Brock 0.10.0 (2022-02-15) ------------------- diff --git a/pytest_subtests.py b/pytest_subtests.py index 1ee4fb2..1a279e2 100644 --- a/pytest_subtests.py +++ b/pytest_subtests.py @@ -97,6 +97,27 @@ def pytest_configure(config): TestCaseFunction.addSubTest = _addSubTest TestCaseFunction.failfast = False + # Hack (#86): the terminal does not know about the "subtests" + # status, so it will by default turn the output to yellow. + # This forcibly adds the new 'subtests' status. + import _pytest.terminal + + new_types = tuple( + f"subtests {outcome}" for outcome in ("passed", "failed", "skipped") + ) + # We need to check if we are not re-adding because we run our own tests + # with pytester in-process mode, so this will be called multiple times. + if new_types[0] not in _pytest.terminal.KNOWN_TYPES: + _pytest.terminal.KNOWN_TYPES = _pytest.terminal.KNOWN_TYPES + new_types + + _pytest.terminal._color_for_type.update( + { + f"subtests {outcome}": _pytest.terminal._color_for_type[outcome] + for outcome in ("passed", "failed", "skipped") + if outcome in _pytest.terminal._color_for_type + } + ) + def pytest_unconfigure(): if hasattr(TestCaseFunction, "addSubTest"):