|
| 1 | +import sys |
| 2 | +from textwrap import dedent |
| 3 | +from typing import Generator |
| 4 | +from typing import List |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +import pytest |
| 8 | +from _pytest.pytester import Pytester |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture() |
| 12 | +def file_structure(pytester: Pytester) -> None: |
| 13 | + pytester.makepyfile( |
| 14 | + test_foo=""" |
| 15 | + from foo import foo |
| 16 | +
|
| 17 | + def test_foo(): |
| 18 | + assert foo() == 1 |
| 19 | + """ |
| 20 | + ) |
| 21 | + |
| 22 | + pytester.makepyfile( |
| 23 | + test_bar=""" |
| 24 | + from bar import bar |
| 25 | +
|
| 26 | + def test_bar(): |
| 27 | + assert bar() == 2 |
| 28 | + """ |
| 29 | + ) |
| 30 | + |
| 31 | + foo_py = pytester.mkdir("sub") / "foo.py" |
| 32 | + content = dedent( |
| 33 | + """ |
| 34 | + def foo(): |
| 35 | + return 1 |
| 36 | + """ |
| 37 | + ) |
| 38 | + foo_py.write_text(content, encoding="utf-8") |
| 39 | + |
| 40 | + bar_py = pytester.mkdir("sub2") / "bar.py" |
| 41 | + content = dedent( |
| 42 | + """ |
| 43 | + def bar(): |
| 44 | + return 2 |
| 45 | + """ |
| 46 | + ) |
| 47 | + bar_py.write_text(content, encoding="utf-8") |
| 48 | + |
| 49 | + |
| 50 | +def test_one_dir(pytester: Pytester, file_structure) -> None: |
| 51 | + pytester.makefile(".ini", pytest="[pytest]\npythonpath=sub\n") |
| 52 | + result = pytester.runpytest("test_foo.py") |
| 53 | + assert result.ret == 0 |
| 54 | + result.assert_outcomes(passed=1) |
| 55 | + |
| 56 | + |
| 57 | +def test_two_dirs(pytester: Pytester, file_structure) -> None: |
| 58 | + pytester.makefile(".ini", pytest="[pytest]\npythonpath=sub sub2\n") |
| 59 | + result = pytester.runpytest("test_foo.py", "test_bar.py") |
| 60 | + assert result.ret == 0 |
| 61 | + result.assert_outcomes(passed=2) |
| 62 | + |
| 63 | + |
| 64 | +def test_module_not_found(pytester: Pytester, file_structure) -> None: |
| 65 | + """Without the pythonpath setting, the module should not be found.""" |
| 66 | + pytester.makefile(".ini", pytest="[pytest]\n") |
| 67 | + result = pytester.runpytest("test_foo.py") |
| 68 | + assert result.ret == pytest.ExitCode.INTERRUPTED |
| 69 | + result.assert_outcomes(errors=1) |
| 70 | + expected_error = "E ModuleNotFoundError: No module named 'foo'" |
| 71 | + result.stdout.fnmatch_lines([expected_error]) |
| 72 | + |
| 73 | + |
| 74 | +def test_no_ini(pytester: Pytester, file_structure) -> None: |
| 75 | + """If no ini file, test should error.""" |
| 76 | + result = pytester.runpytest("test_foo.py") |
| 77 | + assert result.ret == pytest.ExitCode.INTERRUPTED |
| 78 | + result.assert_outcomes(errors=1) |
| 79 | + expected_error = "E ModuleNotFoundError: No module named 'foo'" |
| 80 | + result.stdout.fnmatch_lines([expected_error]) |
| 81 | + |
| 82 | + |
| 83 | +def test_clean_up(pytester: Pytester) -> None: |
| 84 | + """Test that the pythonpath plugin cleans up after itself.""" |
| 85 | + # This is tough to test behaviorly because the cleanup really runs last. |
| 86 | + # So the test make several implementation assumptions: |
| 87 | + # - Cleanup is done in pytest_unconfigure(). |
| 88 | + # - Not a hookwrapper. |
| 89 | + # So we can add a hookwrapper ourselves to test what it does. |
| 90 | + pytester.makefile(".ini", pytest="[pytest]\npythonpath=I_SHALL_BE_REMOVED\n") |
| 91 | + pytester.makepyfile(test_foo="""def test_foo(): pass""") |
| 92 | + |
| 93 | + before: Optional[List[str]] = None |
| 94 | + after: Optional[List[str]] = None |
| 95 | + |
| 96 | + class Plugin: |
| 97 | + @pytest.hookimpl(hookwrapper=True, tryfirst=True) |
| 98 | + def pytest_unconfigure(self) -> Generator[None, None, None]: |
| 99 | + nonlocal before, after |
| 100 | + before = sys.path.copy() |
| 101 | + yield |
| 102 | + after = sys.path.copy() |
| 103 | + |
| 104 | + result = pytester.runpytest_inprocess(plugins=[Plugin()]) |
| 105 | + assert result.ret == 0 |
| 106 | + |
| 107 | + assert before is not None |
| 108 | + assert after is not None |
| 109 | + assert any("I_SHALL_BE_REMOVED" in entry for entry in before) |
| 110 | + assert not any("I_SHALL_BE_REMOVED" in entry for entry in after) |
0 commit comments