Skip to content

Commit c2d494f

Browse files
committed
test
1 parent 1c6a11c commit c2d494f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/test_test_runner.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import os
24
import tempfile
35
from pathlib import Path
@@ -96,3 +98,51 @@ def test_sort():
9698
)
9799
assert results[0].did_pass, "Test did not pass as expected"
98100
result_file.unlink(missing_ok=True)
101+
102+
code = """import torch
103+
def sorter(arr):
104+
print(torch.ones(1))
105+
arr.sort()
106+
return arr
107+
108+
def test_sort():
109+
arr = [5, 4, 3, 2, 1, 0]
110+
output = sorter(arr)
111+
assert output == [0, 1, 2, 3, 4, 5]
112+
"""
113+
cur_dir_path = Path(__file__).resolve().parent
114+
config = TestConfig(
115+
tests_root=cur_dir_path,
116+
project_root_path=cur_dir_path,
117+
test_framework="pytest",
118+
tests_project_rootdir=cur_dir_path.parent,
119+
)
120+
121+
test_env = os.environ.copy()
122+
test_env["CODEFLASH_TEST_ITERATION"] = "0"
123+
test_env["CODEFLASH_TRACER_DISABLE"] = "1"
124+
if "PYTHONPATH" not in test_env:
125+
test_env["PYTHONPATH"] = str(config.project_root_path)
126+
else:
127+
test_env["PYTHONPATH"] += os.pathsep + str(config.project_root_path)
128+
129+
with tempfile.NamedTemporaryFile(prefix="test_xx", suffix=".py", dir=cur_dir_path) as fp:
130+
test_files = TestFiles(
131+
test_files=[TestFile(instrumented_behavior_file_path=Path(fp.name), test_type=TestType.EXISTING_UNIT_TEST)]
132+
)
133+
fp.write(code.encode("utf-8"))
134+
fp.flush()
135+
result_file, process, _, _ = run_behavioral_tests(
136+
test_files,
137+
test_framework=config.test_framework,
138+
cwd=Path(config.project_root_path),
139+
test_env=test_env,
140+
pytest_timeout=1,
141+
pytest_target_runtime_seconds=1,
142+
)
143+
results = parse_test_xml(
144+
test_xml_file_path=result_file, test_files=test_files, test_config=config, run_result=process
145+
)
146+
match = re.search(r"^.*ModuleNotFoundError.*$", process.stdout, re.MULTILINE).group()
147+
assert match=="E ModuleNotFoundError: No module named 'torch'"
148+
result_file.unlink(missing_ok=True)

0 commit comments

Comments
 (0)