Check duplicate test names
Experiment with checking for duplicate test names.
Functions⚓︎
run ⚓︎
run(test_path)
Check for duplicates in the test suite.
Inspired by: https://stackoverflow.com/a/67840804/3219667
Source code in calcipy/experiments/check_duplicate_test_names.py
def run(test_path: Path) -> List[str]: # noqa: C901
"""Check for duplicates in the test suite.
Inspired by: https://stackoverflow.com/a/67840804/3219667
"""
summary = set()
duplicates = []
for path_test in test_path.rglob('test_*.py'):
LOGGER.info(path_test.as_posix())
parsed_ast = ast.parse(path_test.read_text())
for node in parsed_ast.body:
if isinstance(node, ast.FunctionDef):
if node.name in summary and node.name.startswith('test_'):
duplicates.append(node.name)
summary.add(node.name)
_show_info(node)
elif isinstance(node, ast.ClassDef):
LOGGER.info('Class name', name=node.name)
for method in node.body:
if isinstance(method, ast.FunctionDef):
_show_info(method)
for node in ast.walk(parsed_ast): # type: ignore[assignment]
if isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef) and node.name not in summary:
LOGGER.info('Found new function(s) through walking')
_show_info(node)
summary.add(node.name)
if duplicates:
LOGGER.error('Found Duplicates', duplicates=duplicates)
return duplicates