File size: 2,680 Bytes
339ddd8 8947d88 339ddd8 8947d88 339ddd8 8947d88 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | """Tests for import-aware Python test selection."""
from __future__ import annotations
from pathlib import Path
from lingologos_python.test_selector import (
discover_test_files,
is_conftest,
is_test_file,
select_python_tests,
)
def test_is_test_file_conventions() -> None:
assert is_test_file("tests/test_alpha.py")
assert is_test_file("pkg/alpha_test.py")
assert not is_test_file("src/alpha.py")
assert not is_test_file("tests/conftest.py")
assert is_conftest("tests/conftest.py")
assert is_conftest("conftest.py")
assert not is_conftest("src/alpha.py")
def test_discover_test_files_prefers_tests_dir(sample_pkg: Path) -> None:
found = discover_test_files(sample_pkg)
assert found == ["tests/test_mathops.py", "tests/test_textops.py"]
def test_select_python_tests_follows_cross_file_imports(sample_pkg: Path) -> None:
selected_math = select_python_tests(sample_pkg, ["src/mathops.py"])
assert selected_math == ["tests/test_mathops.py", "tests/test_textops.py"]
selected_text = select_python_tests(sample_pkg, ["src/textops.py"])
assert selected_text == ["tests/test_textops.py"]
def test_select_python_tests_returns_self_when_test_file_changed(sample_pkg: Path) -> None:
assert select_python_tests(sample_pkg, ["tests/test_textops.py"]) == ["tests/test_textops.py"]
def test_select_python_tests_returns_empty_for_unknown_file(sample_pkg: Path) -> None:
assert select_python_tests(sample_pkg, ["src/unrelated_missing.py"]) == []
def test_a_conftest_change_selects_the_folder_of_tests_it_carries(tmp_path: Path) -> None:
"""A conftest holds fixtures no import edge shows, so its whole folder is selected."""
root = tmp_path / "conf_pkg"
(root / "tests" / "sub").mkdir(parents=True)
(root / "tests" / "conftest.py").write_text(
"import pytest\n\n\n@pytest.fixture\ndef shared():\n return 1\n",
encoding="ascii",
newline="\n",
)
(root / "tests" / "test_a.py").write_text(
"def test_a(shared):\n assert shared == 1\n", encoding="ascii", newline="\n"
)
(root / "tests" / "sub" / "conftest.py").write_text(
"import pytest\n\n\n@pytest.fixture\ndef deeper():\n return 2\n",
encoding="ascii",
newline="\n",
)
(root / "tests" / "sub" / "test_b.py").write_text(
"def test_b(deeper):\n assert deeper == 2\n", encoding="ascii", newline="\n"
)
broad = select_python_tests(root, ["tests/conftest.py"])
assert broad == ["tests/sub/test_b.py", "tests/test_a.py"]
narrow = select_python_tests(root, ["tests/sub/conftest.py"])
assert narrow == ["tests/sub/test_b.py"]
|