99from codeflash .verification .verification_utils import TestConfig
1010
1111
12- def test_unittest_runner (tmp_path : Path ):
12+ def test_unittest_runner ():
1313 code = """import time
1414import gc
1515import unittest
@@ -34,23 +34,24 @@ def test_sort(self):
3434 tests_project_rootdir = cur_dir_path .parent ,
3535 )
3636
37- test_file_path = tmp_path / "test_unittest_runner.py"
38- test_file_path .write_text (code , encoding = "utf-8" )
39- test_files = TestFiles (
40- test_files = [TestFile (instrumented_behavior_file_path = test_file_path , test_type = TestType .EXISTING_UNIT_TEST )]
41- )
42- result_file , process , _ , _ = run_behavioral_tests (
43- test_files ,
44- test_framework = config .test_framework ,
45- cwd = Path (config .project_root_path ),
46- test_env = os .environ .copy (),
47- )
48- results = parse_test_xml (result_file , test_files , config , process )
37+ with tempfile .TemporaryDirectory (dir = cur_dir_path ) as temp_dir :
38+ test_file_path = Path (temp_dir ) / "test_xx.py"
39+ test_files = TestFiles (
40+ test_files = [TestFile (instrumented_behavior_file_path = test_file_path , test_type = TestType .EXISTING_UNIT_TEST )]
41+ )
42+ test_file_path .write_text (code , encoding = "utf-8" )
43+ result_file , process , _ , _ = run_behavioral_tests (
44+ test_files ,
45+ test_framework = config .test_framework ,
46+ cwd = Path (config .project_root_path ),
47+ test_env = os .environ .copy (),
48+ )
49+ results = parse_test_xml (result_file , test_files , config , process )
4950 assert results [0 ].did_pass , "Test did not pass as expected"
5051 result_file .unlink (missing_ok = True )
5152
5253
53- def test_pytest_runner (tmp_path : Path ):
54+ def test_pytest_runner ():
5455 code = """
5556def sorter(arr):
5657 arr.sort()
@@ -77,22 +78,23 @@ def test_sort():
7778 else :
7879 test_env ["PYTHONPATH" ] += os .pathsep + str (config .project_root_path )
7980
80- test_file_path = tmp_path / "test_pytest_runner.py"
81- test_file_path .write_text (code , encoding = "utf-8" )
82- test_files = TestFiles (
83- test_files = [TestFile (instrumented_behavior_file_path = test_file_path , test_type = TestType .EXISTING_UNIT_TEST )]
84- )
85- result_file , process , _ , _ = run_behavioral_tests (
86- test_files ,
87- test_framework = config .test_framework ,
88- cwd = Path (config .project_root_path ),
89- test_env = test_env ,
90- pytest_timeout = 1 ,
91- pytest_target_runtime_seconds = 1 ,
92- )
93- results = parse_test_xml (
94- test_xml_file_path = result_file , test_files = test_files , test_config = config , run_result = process
95- )
81+ with tempfile .TemporaryDirectory (dir = cur_dir_path ) as temp_dir :
82+ test_file_path = Path (temp_dir ) / "test_xx.py"
83+ test_files = TestFiles (
84+ test_files = [TestFile (instrumented_behavior_file_path = test_file_path , test_type = TestType .EXISTING_UNIT_TEST )]
85+ )
86+ test_file_path .write_text (code , encoding = "utf-8" )
87+ result_file , process , _ , _ = run_behavioral_tests (
88+ test_files ,
89+ test_framework = config .test_framework ,
90+ cwd = Path (config .project_root_path ),
91+ test_env = test_env ,
92+ pytest_timeout = 1 ,
93+ pytest_target_runtime_seconds = 1 ,
94+ )
95+ results = parse_test_xml (
96+ test_xml_file_path = result_file , test_files = test_files , test_config = config , run_result = process
97+ )
9698 assert results [0 ].did_pass , "Test did not pass as expected"
9799 result_file .unlink (missing_ok = True )
98100
@@ -123,22 +125,23 @@ def test_sort():
123125 else :
124126 test_env ["PYTHONPATH" ] += os .pathsep + str (config .project_root_path )
125127
126- test_file_path2 = tmp_path / "test_pytest_runner_import_error.py"
127- test_file_path2 .write_text (code , encoding = "utf-8" )
128- test_files = TestFiles (
129- test_files = [TestFile (instrumented_behavior_file_path = test_file_path2 , test_type = TestType .EXISTING_UNIT_TEST )]
130- )
131- result_file , process , _ , _ = run_behavioral_tests (
132- test_files ,
133- test_framework = config .test_framework ,
134- cwd = Path (config .project_root_path ),
135- test_env = test_env ,
136- pytest_timeout = 1 ,
137- pytest_target_runtime_seconds = 1 ,
138- )
139- results = parse_test_xml (
140- test_xml_file_path = result_file , test_files = test_files , test_config = config , run_result = process
141- )
128+ with tempfile .TemporaryDirectory (dir = cur_dir_path ) as temp_dir :
129+ test_file_path = Path (temp_dir ) / "test_xx.py"
130+ test_files = TestFiles (
131+ test_files = [TestFile (instrumented_behavior_file_path = test_file_path , test_type = TestType .EXISTING_UNIT_TEST )]
132+ )
133+ test_file_path .write_text (code , encoding = "utf-8" )
134+ result_file , process , _ , _ = run_behavioral_tests (
135+ test_files ,
136+ test_framework = config .test_framework ,
137+ cwd = Path (config .project_root_path ),
138+ test_env = test_env ,
139+ pytest_timeout = 1 ,
140+ pytest_target_runtime_seconds = 1 ,
141+ )
142+ results = parse_test_xml (
143+ test_xml_file_path = result_file , test_files = test_files , test_config = config , run_result = process
144+ )
142145 match = ImportErrorPattern .search (process .stdout ).group ()
143146 assert match == "ModuleNotFoundError: No module named 'torch_does_not_exist'"
144147 result_file .unlink (missing_ok = True )
0 commit comments