Skip to content

Commit 8bd8068

Browse files
committed
implement suggestions
1 parent 8a3ea91 commit 8bd8068

File tree

1 file changed

+37
-43
lines changed

1 file changed

+37
-43
lines changed

codeflash/discovery/discover_unit_tests.py

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,10 @@ def process_test_files(
207207
with test_files_progress_bar(
208208
total=len(file_to_test_map), description="Processing test files"
209209
) as (progress, task_id):
210-
211210
for test_file, functions in file_to_test_map.items():
212211
try:
213212
script = jedi.Script(path=test_file, project=jedi_project)
214-
test_functions = set()
213+
test_functions_by_name: dict[str, TestFunction] = {}
215214

216215
all_names = script.get_names(all_scopes=True, references=True)
217216
all_defs = script.get_names(all_scopes=True, definitions=True)
@@ -238,22 +237,20 @@ def process_test_files(
238237
function.test_function
239238
)[1]
240239
if function_name in top_level_functions:
241-
test_functions.add(
242-
TestFunction(
243-
function_name,
244-
function.test_class,
245-
parameters,
246-
function.test_type,
247-
)
248-
)
249-
elif function.test_function in top_level_functions:
250-
test_functions.add(
251-
TestFunction(
252-
function.test_function,
240+
qualified_name = function_name
241+
test_functions_by_name[qualified_name] = TestFunction(
242+
function_name,
253243
function.test_class,
254-
None,
244+
parameters,
255245
function.test_type,
256246
)
247+
elif function.test_function in top_level_functions:
248+
qualified_name = function.test_function
249+
test_functions_by_name[qualified_name] = TestFunction(
250+
function.test_function,
251+
function.test_class,
252+
None,
253+
function.test_type,
257254
)
258255
elif UNITTEST_PARAMETERIZED_TEST_NAME_REGEX.match(
259256
function.test_function
@@ -262,13 +259,12 @@ def process_test_files(
262259
"", function.test_function
263260
)
264261
if base_name in top_level_functions:
265-
test_functions.add(
266-
TestFunction(
267-
function_name=base_name,
268-
test_class=function.test_class,
269-
parameters=function.test_function,
270-
test_type=function.test_type,
271-
)
262+
qualified_name = base_name
263+
test_functions_by_name[qualified_name] = TestFunction(
264+
function_name=base_name,
265+
test_class=function.test_class,
266+
parameters=function.test_function,
267+
test_type=function.test_type,
272268
)
273269

274270
elif test_framework == "unittest":
@@ -289,7 +285,8 @@ def process_test_files(
289285
)
290286

291287
if is_parameterized and new_function == def_name.name:
292-
test_functions.add(
288+
qualified_name = f"{matched_name}.{def_name.name}"
289+
test_functions_by_name[qualified_name] = (
293290
TestFunction(
294291
function_name=def_name.name,
295292
test_class=matched_name,
@@ -298,7 +295,8 @@ def process_test_files(
298295
)
299296
)
300297
elif function == def_name.name:
301-
test_functions.add(
298+
qualified_name = f"{matched_name}.{def_name.name}"
299+
test_functions_by_name[qualified_name] = (
302300
TestFunction(
303301
function_name=def_name.name,
304302
test_class=matched_name,
@@ -307,13 +305,6 @@ def process_test_files(
307305
)
308306
)
309307

310-
test_functions_list = list(test_functions)
311-
test_functions_raw = [elem.function_name for elem in test_functions_list]
312-
313-
test_functions_by_name = defaultdict(list)
314-
for i, func_name in enumerate(test_functions_raw):
315-
test_functions_by_name[func_name].append(i)
316-
317308
for name in all_names:
318309
if name.full_name is None:
319310
continue
@@ -347,22 +338,25 @@ def process_test_files(
347338
and definition[0].module_name != name.module_name
348339
and definition[0].full_name is not None
349340
):
350-
for index in test_functions_by_name[scope]:
351-
scope_test_function = test_functions_list[index].function_name
352-
scope_test_class = test_functions_list[index].test_class
353-
scope_parameters = test_functions_list[index].parameters
354-
test_type = test_functions_list[index].test_type
355-
356-
if scope_parameters is not None:
357-
if test_framework == "pytest":
358-
scope_test_function += "[" + scope_parameters + "]"
359-
if test_framework == "unittest":
360-
scope_test_function += "_" + scope_parameters
341+
test_function_obj = test_functions_by_name[scope]
342+
scope_test_function = test_function_obj.function_name
343+
scope_test_class = test_function_obj.test_class
344+
scope_parameters = test_function_obj.parameters
345+
test_type = test_function_obj.test_type
346+
347+
if scope_parameters is not None:
348+
if test_framework == "pytest":
349+
scope_test_function += "[" + scope_parameters + "]"
361350

362351
full_name_without_module_prefix = definition[
363352
0
364353
].full_name.replace(definition[0].module_name + ".", "", 1)
365-
qualified_name_with_modules_from_root = f"{module_name_from_file_path(definition[0].module_path, project_root_path)}.{full_name_without_module_prefix}"
354+
module_name = module_name_from_file_path(
355+
definition[0].module_path, project_root_path
356+
)
357+
qualified_name_with_modules_from_root = (
358+
f"{module_name}.{full_name_without_module_prefix}"
359+
)
366360

367361
function_to_test_map[qualified_name_with_modules_from_root].add(
368362
FunctionCalledInTest(

0 commit comments

Comments
 (0)