Skip to content

Commit

Permalink
Use a unittest style test to support more test runners
Browse files Browse the repository at this point in the history
  • Loading branch information
joerick committed Dec 5, 2022
1 parent c417dce commit a157a51
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
25 changes: 14 additions & 11 deletions cibuildwheel/resources/testing_temp_dir_file.py
Original file line number Diff line number Diff line change
@@ -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."
)
13 changes: 9 additions & 4 deletions test/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand All @@ -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
)

0 comments on commit a157a51

Please sign in to comment.