import contextlib import importlib.util import io import os import pathlib import re import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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 HAS_XDIST = importlib.util.find_spec("xdist") is not None PYTEST_HERE = [sys.executable, "-m", "pytest", "-p", "no:cacheprovider"] class PytestArgs(unittest.TestCase): def test_runs_bin_when_no_argument_names_a_path(self): self.assertEqual(main.pytest_args([]), [str(BIN)]) self.assertEqual(main.pytest_args(["-k", "no_such_path_here"]), ["-k", "no_such_path_here", str(BIN)]) def test_passes_a_given_path_on_alone(self): with tempfile.TemporaryDirectory() as folder: test_file = pathlib.Path(folder, "test_x.py") test_file.write_text("") self.assertEqual(main.pytest_args([str(test_file)]), [str(test_file)]) self.assertEqual(main.pytest_args(["-x", f"{test_file}::T::test_a"]), ["-x", f"{test_file}::T::test_a"]) def test_the_pinned_command_is_readme_tests_incantation(self): self.assertEqual(main.PYTEST[1:], ["--python", main.PYTEST[2], "--with", "pytest-xdist==3.8.0", "pytest==9.2.0", "-n", "4"]) self.assertEqual(main.PYTEST[0], "uvx") @unittest.skipUnless(HAS_PYTEST, "needs pytest importable") class Run(unittest.TestCase): def run_in(self, folder: str, pytest: list[str], *args: str) -> tuple[int, str]: out = io.StringIO() with contextlib.redirect_stdout(out), contextlib.redirect_stderr(out): code = main.run(list(args), pytest, folder) return code, out.getvalue() def log_named(self, output: str) -> pathlib.Path: last = output.rstrip("\n").splitlines()[-1] self.assertTrue(last.startswith("Full log: "), output) path = pathlib.Path(last.removeprefix("Full log: ")) self.addCleanup(path.unlink, missing_ok=True) return path def test_a_passing_run_prints_the_summary_line_and_the_log_path(self): with tempfile.TemporaryDirectory() as folder: pathlib.Path(folder, "test_ok.py").write_text("def test_ok():\n\tprint('noise from a passing test')\n") code, output = self.run_in(folder, PYTEST_HERE, "-s", folder) self.assertEqual(code, 0) lines = output.rstrip("\n").splitlines() self.assertEqual(len(lines), 2, output) self.assertRegex(lines[0], r"^=+ 1 passed in .* =+$") 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_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"(?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_bad", output) self.assertIn("def test_bad", self.log_named(output).read_text()) 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) self.assertIn("run-tests: cannot start", output) self.assertNotIn("Full log: ", output) def test_a_run_that_prints_no_summary_line_shows_the_logs_last_lines(self): with tempfile.TemporaryDirectory() as folder: code, output = self.run_in(folder, PYTEST_HERE, "--no-such-option", folder) self.assertEqual(code, 1) self.assertIn("run-tests: pytest printed no summary line; the log ends:", output) self.assertIn("--no-such-option", output) self.log_named(output) if __name__ == "__main__": unittest.main()