Skip to content

Commit 8878baf

Browse files
committed
Merge branch 'pytest-plugin-blocker' into codeflash-trace-decorator
# Conflicts: # codeflash/discovery/pytest_new_process_discovery.py
2 parents b77a979 + c939dfe commit 8878baf

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

codeflash/discovery/pytest_new_process_discovery.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ def pytest_collection_finish(self, session) -> None:
1616
collected_tests.extend(session.items)
1717
pytest_rootdir = session.config.rootdir
1818

19+
def pytest_collection_modifyitems(config, items):
20+
skip_benchmark = pytest.mark.skip(reason="Skipping benchmark tests")
21+
for item in items:
22+
if "benchmark" in item.fixturenames:
23+
item.add_marker(skip_benchmark)
1924

2025
def parse_pytest_collection_results(pytest_tests: list[Any]) -> list[dict[str, str]]:
2126
test_results = []
@@ -34,7 +39,7 @@ def parse_pytest_collection_results(pytest_tests: list[Any]) -> list[dict[str, s
3439

3540
try:
3641
exitcode = pytest.main(
37-
[tests_root, "-pno:logging", "--collect-only", "-m", "not skip", "--benchmark-skip"], plugins=[PytestCollectionPlugin()]
42+
[tests_root, "-p no:logging", "--collect-only", "-m", "not skip",], plugins=[PytestCollectionPlugin()]
3843
)
3944
except Exception as e: # noqa: BLE001
4045
print(f"Failed to collect tests: {e!s}") # noqa: T201

codeflash/verification/test_runner.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
if TYPE_CHECKING:
1717
from codeflash.models.models import TestFiles
1818

19+
BEHAVIORAL_BLOCKLISTED_PLUGINS = ["benchmark"]
20+
BENCHMARKING_BLOCKLISTED_PLUGINS = ["cov", "benchmark", "profiling"]
1921

2022
def execute_test_subprocess(
2123
cmd_list: list[str], cwd: Path, env: dict[str, str] | None, timeout: int = 600
@@ -90,16 +92,18 @@ def run_behavioral_tests(
9092
else:
9193
coverage_cmd.extend(shlex.split(pytest_cmd, posix=IS_POSIX)[1:])
9294

95+
blocklist_args = [f"-p no:{plugin}" for plugin in BEHAVIORAL_BLOCKLISTED_PLUGINS if plugin != "cov"]
9396
results = execute_test_subprocess(
94-
coverage_cmd + common_pytest_args + result_args + test_files, cwd=cwd, env=pytest_test_env, timeout=600
97+
coverage_cmd + common_pytest_args + blocklist_args + result_args + test_files, cwd=cwd, env=pytest_test_env, timeout=600
9598
)
9699
logger.debug(
97100
f"Result return code: {results.returncode}, "
98101
f"{'Result stderr:' + str(results.stderr) if results.stderr else ''}"
99102
)
100103
else:
104+
blocklist_args = [f"-p no:{plugin}" for plugin in BEHAVIORAL_BLOCKLISTED_PLUGINS]
101105
results = execute_test_subprocess(
102-
pytest_cmd_list + common_pytest_args + result_args + test_files,
106+
pytest_cmd_list + common_pytest_args + blocklist_args + result_args + test_files,
103107
cwd=cwd,
104108
env=pytest_test_env,
105109
timeout=600, # TODO: Make this dynamic
@@ -174,8 +178,10 @@ def run_benchmarking_tests(
174178
result_args = [f"--junitxml={result_file_path.as_posix()}", "-o", "junit_logging=all"]
175179
pytest_test_env = test_env.copy()
176180
pytest_test_env["PYTEST_PLUGINS"] = "codeflash.verification.pytest_plugin"
181+
blocklist_args = [f"-p no:{plugin}" for plugin in BENCHMARKING_BLOCKLISTED_PLUGINS]
182+
177183
results = execute_test_subprocess(
178-
pytest_cmd_list + pytest_args + result_args + test_files,
184+
pytest_cmd_list + pytest_args + blocklist_args + result_args + test_files,
179185
cwd=cwd,
180186
env=pytest_test_env,
181187
timeout=600, # TODO: Make this dynamic

tests/test_unit_test_discovery.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,48 @@ def test_unit_test_discovery_unittest():
4747
# assert len(tests) > 0
4848
# Unittest discovery within a pytest environment does not work
4949

50+
def test_benchmark_unit_test_discovery_pytest():
51+
with tempfile.TemporaryDirectory() as tmpdirname:
52+
# Create a dummy test file
53+
test_file_path = Path(tmpdirname) / "test_dummy.py"
54+
test_file_content = """
55+
from bubble_sort import sorter
56+
57+
def test_benchmark_sort(benchmark):
58+
benchmark(sorter, [5, 4, 3, 2, 1, 0])
59+
60+
def test_normal_test():
61+
assert sorter(list(reversed(range(100)))) == list(range(100))
62+
63+
def test_normal_test2():
64+
assert sorter(list(reversed(range(100)))) == list(range(100))"""
65+
test_file_path.write_text(test_file_content)
66+
path_obj_tempdirname = Path(tmpdirname)
67+
68+
# Create a file that the test file is testing
69+
code_file_path = path_obj_tempdirname / "bubble_sort.py"
70+
code_file_content = """
71+
def sorter(arr):
72+
return sorted(arr)"""
73+
code_file_path.write_text(code_file_content)
74+
75+
# Create a TestConfig with the temporary directory as the root
76+
test_config = TestConfig(
77+
tests_root=path_obj_tempdirname,
78+
project_root_path=path_obj_tempdirname,
79+
test_framework="pytest",
80+
tests_project_rootdir=path_obj_tempdirname.parent,
81+
)
82+
83+
# Discover tests
84+
tests = discover_unit_tests(test_config)
85+
assert len(tests) == 1
86+
assert 'bubble_sort.sorter' in tests
87+
assert len(tests['bubble_sort.sorter']) == 2
88+
functions = [test.tests_in_file.test_function for test in tests['bubble_sort.sorter']]
89+
assert 'test_normal_test' in functions
90+
assert 'test_normal_test2' in functions
91+
assert 'test_benchmark_sort' not in functions
5092

5193
def test_discover_tests_pytest_with_temp_dir_root():
5294
with tempfile.TemporaryDirectory() as tmpdirname:

0 commit comments

Comments
 (0)