Print each failing test's error lines, which pytest-xdist leaves out of the short summary

This commit is contained in:
2026-11-12 08:41:13 +01:00
parent e4e3d8cff0
commit e86a0cbc0c
3 changed files with 69 additions and 15 deletions
+13 -7
View File
@@ -3,6 +3,7 @@ import importlib.util
import io
import os
import pathlib
import re
import sys
import tempfile
import unittest
@@ -14,7 +15,8 @@ from run_tests import main
BIN = pathlib.Path(__file__).resolve().parents[1]
# The suite runs under pytest, and `python3 -m unittest` may run it where pytest is not installed.
HAS_PYTEST = importlib.util.find_spec("pytest") is not None
PYTEST_HERE = [sys.executable, "-m", "pytest", "-p", "no:cacheprovider", "-p", "no:xdist"]
HAS_XDIST = importlib.util.find_spec("xdist") is not None
PYTEST_HERE = [sys.executable, "-m", "pytest", "-p", "no:cacheprovider"]
class PytestArgs(unittest.TestCase):
@@ -60,17 +62,21 @@ class Run(unittest.TestCase):
self.assertIn("noise from a passing test", self.log_named(output).read_text())
def test_a_failing_run_prints_each_failing_test_with_its_error(self):
# pytest-xdist drops the error from the short summary's lines, so the run takes it from the FAILURES section.
workers = ["-n", "2"] if HAS_XDIST else []
with tempfile.TemporaryDirectory() as folder:
pathlib.Path(folder, "test_bad.py").write_text("def test_ok():\n\tpass\n\ndef test_bad():\n\tassert 1 == 2, 'one is not two'\n\ndef test_worse():\n\traise KeyError('missing')\n")
code, output = self.run_in(folder, PYTEST_HERE, folder)
pathlib.Path(folder, "test_bad.py").write_text("def test_ok():\n\tpass\n\ndef test_bad():\n\tassert 1 == 2, 'one is not two'\n\ndef test_long():\n\traise ValueError('\\n'.join(map(str, range(30))))\n")
code, output = self.run_in(folder, PYTEST_HERE, *workers, folder)
self.assertEqual(code, 1)
self.assertRegex(output, r"FAILED .*test_bad\.py::test_bad - AssertionError: one is not two")
self.assertRegex(output, r"FAILED .*test_bad\.py::test_worse - KeyError: 'missing'")
self.assertRegex(output, r"(?m)^_+ test_bad _+\nE +AssertionError: one is not two\n(E .*\n)*.*test_bad\.py:5: AssertionError$")
self.assertRegex(output, r"(?m)^FAILED .*test_bad\.py::test_bad\b")
self.assertRegex(output, r"(?m)^E +… \d+ more lines in the log$")
self.assertEqual(len(re.findall(r"(?m)^E ", output)), 2 + main.ERROR_LINES + 1)
self.assertRegex(output, r"=+ 2 failed, 1 passed in .* =+\n")
self.assertNotIn("def test_ok", output)
self.assertNotIn("def test_bad", output)
self.assertIn("def test_bad", self.log_named(output).read_text())
def test_a_run_that_cannot_start_says_so_and_prints_the_logs_last_lines(self):
def test_a_run_that_cannot_start_says_so(self):
with tempfile.TemporaryDirectory() as folder:
code, output = self.run_in(folder, [os.path.join(folder, "no-such-uvx")])
self.assertEqual(code, 1)