96 lines
3.4 KiB
Python
96 lines
3.4 KiB
Python
import os
|
|
import pprint
|
|
import ast
|
|
|
|
def extract_all_parametrize(filepath):
|
|
with open(filepath, "r") as f:
|
|
lines = f.readlines()
|
|
|
|
parametrize_data = {}
|
|
i = 0
|
|
|
|
while i < len(lines):
|
|
line = lines[i]
|
|
if line.strip().startswith("def test_"):
|
|
func_lines = [line]
|
|
i += 1
|
|
# Récupère les lignes suivantes jusqu'à la fin de la fonction
|
|
while i < len(lines) and (lines[i].startswith(" ") or lines[i].strip() == ""):
|
|
func_lines.append(lines[i])
|
|
i += 1
|
|
# Ajoute les décorateurs au-dessus
|
|
j = i - len(func_lines) - 1
|
|
while j >= 0 and lines[j].strip().startswith("@"):
|
|
func_lines.insert(0, lines[j])
|
|
j -= 1
|
|
|
|
try:
|
|
tree = ast.parse("".join(func_lines))
|
|
func_node = tree.body[0]
|
|
deco_list = func_node.decorator_list
|
|
|
|
for deco in deco_list:
|
|
if (
|
|
isinstance(deco, ast.Call)
|
|
and isinstance(deco.func, ast.Attribute)
|
|
and deco.func.attr == "parametrize"
|
|
):
|
|
names_node = deco.args[0]
|
|
values_node = deco.args[1]
|
|
|
|
if isinstance(names_node, ast.Str):
|
|
param_names = [x.strip() for x in names_node.s.split(",")]
|
|
elif isinstance(names_node, ast.List):
|
|
param_names = [elt.s for elt in names_node.elts if isinstance(elt, ast.Str)]
|
|
else:
|
|
param_names = []
|
|
|
|
param_values = []
|
|
if isinstance(values_node, ast.List):
|
|
for elt in values_node.elts:
|
|
if isinstance(elt, ast.Tuple):
|
|
value = [ast.literal_eval(e) for e in elt.elts]
|
|
else:
|
|
value = [ast.literal_eval(elt)]
|
|
param_values.append(value)
|
|
|
|
parametrize_data[func_node.name] = {
|
|
"names": param_names,
|
|
"values": param_values
|
|
}
|
|
|
|
except Exception as e:
|
|
print(f"⚠️ Fonction ignorée (fichier {filepath}): {e}")
|
|
|
|
else:
|
|
i += 1
|
|
|
|
return parametrize_data
|
|
|
|
def find_test_files(root_dir):
|
|
"""Retourne la liste des chemins vers les fichiers test_*.py dans root_dir"""
|
|
test_files = []
|
|
for dirpath, _, filenames in os.walk(root_dir):
|
|
for fname in filenames:
|
|
if fname.startswith("test_") and fname.endswith(".py"):
|
|
test_files.append(os.path.join(dirpath, fname))
|
|
return test_files
|
|
|
|
def extract_all_tests_params(root_dir):
|
|
all_params = {}
|
|
test_files = find_test_files(root_dir)
|
|
|
|
for filepath in test_files:
|
|
try:
|
|
param_data = extract_all_parametrize(filepath)
|
|
if param_data:
|
|
all_params[filepath] = param_data
|
|
except Exception as e:
|
|
print(f"❌ Erreur lors du parsing de {filepath} : {e}")
|
|
|
|
return all_params
|
|
|
|
if __name__ == "__main__":
|
|
result = extract_all_tests_params("tests")
|
|
pprint.pprint(result, sort_dicts=False)
|