From a157a51225a47c177db87bbc31a3342096fe3442 Mon Sep 17 00:00:00 2001 From: Joe Rickerby Date: Mon, 5 Dec 2022 19:52:09 +0000 Subject: [PATCH] Use a unittest style test to support more test runners --- .../resources/testing_temp_dir_file.py | 25 +++++++++++-------- test/test_testing.py | 13 +++++++--- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cibuildwheel/resources/testing_temp_dir_file.py b/cibuildwheel/resources/testing_temp_dir_file.py index 4051d7a04..1788e7cf7 100644 --- a/cibuildwheel/resources/testing_temp_dir_file.py +++ b/cibuildwheel/resources/testing_temp_dir_file.py @@ -1,14 +1,17 @@ # this file is copied to the testing cwd, to raise the below error message if -# pytest is run from there +# pytest/unittest is run from there. +import unittest -def test(): - assert False, ( - "cibuildwheel executes tests from a different working directory to " - "your project. This ensures only your wheel is imported, preventing " - "Python from accessing files that haven't been packaged into the " - "wheel. Please specify a path to your tests when invoking pytest " - "using the {project} placeholder, e.g. `pytest {project}` or " - "`pytest {project}/tests`. cibuildwheel will replace {project} with " - "the path to your project." - ) + +class TestStringMethods(unittest.TestCase): + def test_fail(self): + self.fail( + "cibuildwheel executes tests from a different working directory to " + "your project. This ensures only your wheel is imported, preventing " + "Python from accessing files that haven't been packaged into the " + "wheel. Please specify a path to your tests when invoking pytest " + "using the {project} placeholder, e.g. `pytest {project}` or " + "`pytest {project}/tests`. cibuildwheel will replace {project} with " + "the path to your project." + ) diff --git a/test/test_testing.py b/test/test_testing.py index 17eab4465..f012819ad 100644 --- a/test/test_testing.py +++ b/test/test_testing.py @@ -154,7 +154,10 @@ def test_failing_test(tmp_path): assert len(os.listdir(output_dir)) == 0 -def test_bare_pytest_invocation(tmp_path: Path, capfd: pytest.CaptureFixture[str]): +@pytest.mark.parametrize("test_runner", ["pytest", "unittest"]) +def test_bare_pytest_invocation( + tmp_path: Path, capfd: pytest.CaptureFixture[str], test_runner: str +): """Check that if a user runs pytest in the the test cwd, it raises a helpful error""" project_dir = tmp_path / "project" output_dir = tmp_path / "output" @@ -165,8 +168,10 @@ def test_bare_pytest_invocation(tmp_path: Path, capfd: pytest.CaptureFixture[str project_dir, output_dir=output_dir, add_env={ - "CIBW_TEST_REQUIRES": "pytest", - "CIBW_TEST_COMMAND": "python -m pytest", + "CIBW_TEST_REQUIRES": "pytest" if test_runner == "pytest" else "", + "CIBW_TEST_COMMAND": ( + "python -m pytest" if test_runner == "pytest" else "python -m unittest" + ), # Skip CPython 3.8 on macOS arm64, see comment above in # 'test_failing_test' "CIBW_SKIP": "cp38-macosx_arm64", @@ -179,5 +184,5 @@ def test_bare_pytest_invocation(tmp_path: Path, capfd: pytest.CaptureFixture[str assert ( "Please specify a path to your tests when invoking pytest using the {project} placeholder" - in captured.out + in captured.out + captured.err )