Skip to content

Commit 110908b

Browse files
committed
Add specific tests for importable modules from tests folders
1 parent 162f22c commit 110908b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

testing/test_collection.py

+39
Original file line numberDiff line numberDiff line change
@@ -1391,3 +1391,42 @@ def test_check():
13911391
)
13921392
result = testdir.runpytest("-v", "--import-mode=importlib")
13931393
result.stdout.fnmatch_lines(["* 1 passed in *"])
1394+
1395+
def setup_conftest_and_foo(self, testdir):
1396+
"""Setup a tests folder to be used to test if modules in that folder can be imported
1397+
due to side-effects of --import-mode or not."""
1398+
testdir.makepyfile(
1399+
**{
1400+
"tests/conftest.py": "",
1401+
"tests/foo.py": """
1402+
def foo(): return 42
1403+
""",
1404+
"tests/test_foo.py": """
1405+
def test_check():
1406+
from foo import foo
1407+
assert foo() == 42
1408+
""",
1409+
}
1410+
)
1411+
1412+
def test_modules_importable_as_side_effect(self, testdir):
1413+
"""In import-modes `prepend` and `append`, we are able to import modules from folders
1414+
containing conftest.py files due to the side effect of changing sys.path."""
1415+
self.setup_conftest_and_foo(testdir)
1416+
result = testdir.runpytest("-v", "--import-mode=prepend")
1417+
result.stdout.fnmatch_lines(["* 1 passed in *"])
1418+
1419+
def test_modules_not_importable_as_side_effect(self, testdir):
1420+
"""In import-mode `importlib`, modules in folders containing conftest.py are not
1421+
importable, as don't change sys.path or sys.modules as side effect of importing
1422+
the conftest.py file.
1423+
"""
1424+
self.setup_conftest_and_foo(testdir)
1425+
result = testdir.runpytest("-v", "--import-mode=importlib")
1426+
result.stdout.fnmatch_lines(
1427+
[
1428+
"*ModuleNotFoundError: No module named 'foo'",
1429+
"tests?test_foo.py:2: ModuleNotFoundError",
1430+
"* 1 failed in *",
1431+
]
1432+
)

0 commit comments

Comments
 (0)