Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Feb 18, 2024
1 parent 0f95a5d commit b95d86b
Showing 1 changed file with 64 additions and 17 deletions.
81 changes: 64 additions & 17 deletions testing/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,12 +755,12 @@ def test_my_test():

def create_installed_doctests_and_tests_dir(
self, path: Path, monkeypatch: MonkeyPatch
) -> Tuple[Path, Path]:
) -> Tuple[Path, Path, Path]:
"""
Create a directory structure where the application code is installed in a virtual environment,
and the tests are in an outside ".tests" directory.
Return the paths to the core module (installed in the virtualenv), and the test module.
Return the paths to the core module (installed in the virtualenv), and the test modules.
"""
app = path / "src/app"
app.mkdir(parents=True)
Expand Down Expand Up @@ -788,14 +788,58 @@ def foo():

monkeypatch.syspath_prepend(site_packages)

# Create the tests file, outside 'src' and the virtualenv.
test_path = path / ".tests/test_core.py"
test_path.parent.mkdir(parents=True)
test_path.write_text(
"import app.core\n\ndef test(): pass",
# Create the tests files, outside 'src' and the virtualenv.
# We use the same test name on purpose, but in different directories, to ensure
# this works as advertised.
conftest_path1 = path / ".tests/a/conftest.py"
conftest_path1.parent.mkdir(parents=True)
conftest_path1.write_text(
dedent(
"""
import pytest
@pytest.fixture
def a_fix(): return "a"
"""
),
encoding="ascii",
)
test_path1 = path / ".tests/a/test_core.py"
test_path1.write_text(
dedent(
"""
import app.core
def test(a_fix):
assert a_fix == "a"
""",
),
encoding="ascii",
)

conftest_path2 = path / ".tests/b/conftest.py"
conftest_path2.parent.mkdir(parents=True)
conftest_path2.write_text(
dedent(
"""
import pytest
@pytest.fixture
def b_fix(): return "b"
"""
),
encoding="ascii",
)

test_path2 = path / ".tests/b/test_core.py"
test_path2.write_text(
dedent(
"""
import app.core
def test(b_fix):
assert b_fix == "b"
""",
),
encoding="ascii",
)
return (site_packages / "app/core.py"), test_path
return (site_packages / "app/core.py"), test_path1, test_path2

def test_import_using_normal_mechanism_first(
self, monkeypatch: MonkeyPatch, pytester: Pytester
Expand All @@ -804,20 +848,22 @@ def test_import_using_normal_mechanism_first(
Test import_path imports from the canonical location when possible first, only
falling back to its normal flow when the module being imported is not reachable via sys.path (#11475).
"""
core_py, test_path = self.create_installed_doctests_and_tests_dir(
core_py, test_path1, test_path2 = self.create_installed_doctests_and_tests_dir(
pytester.path, monkeypatch
)

# Imported from installed location via sys.path.
# core_py is reached from sys.path, so should be imported normally.
mod = import_path(core_py, mode="importlib", root=pytester.path)
assert mod.__name__ == "app.core"
assert mod.__file__ and Path(mod.__file__) == core_py

# Imported as a standalone module.
# Instead of '.tests.test_core', we import as "_tests.test_core" because
# tests are not reachable from sys.path, so they are imported as a standalone modules.
# Instead of '.tests.a.test_core', we import as "_tests.a.test_core" because
# importlib considers module names starting with '.' to be local imports.
mod = import_path(test_path, mode="importlib", root=pytester.path)
assert mod.__name__ == "_tests.test_core"
mod = import_path(test_path1, mode="importlib", root=pytester.path)
assert mod.__name__ == "_tests.a.test_core"
mod = import_path(test_path2, mode="importlib", root=pytester.path)
assert mod.__name__ == "_tests.b.test_core"

def test_import_using_normal_mechanism_first_integration(
self, monkeypatch: MonkeyPatch, pytester: Pytester
Expand All @@ -828,7 +874,7 @@ def test_import_using_normal_mechanism_first_integration(
We should not make this call in the same test as above, as the modules have already
been imported by separate import_path() calls.
"""
core_py, test_path = self.create_installed_doctests_and_tests_dir(
core_py, test_path1, test_path2 = self.create_installed_doctests_and_tests_dir(
pytester.path, monkeypatch
)
result = pytester.runpytest(
Expand All @@ -841,8 +887,9 @@ def test_import_using_normal_mechanism_first_integration(
result.stdout.fnmatch_lines(
[
f"{core_py.relative_to(pytester.path)} . *",
f"{test_path.relative_to(pytester.path)} . *",
"* 2 passed*",
f"{test_path1.relative_to(pytester.path)} . *",
f"{test_path2.relative_to(pytester.path)} . *",
"* 3 passed*",
]
)

Expand Down

0 comments on commit b95d86b

Please sign in to comment.