diff --git a/log_all_test_params.py b/log_all_test_params.py new file mode 100644 index 000000000..057b943f7 --- /dev/null +++ b/log_all_test_params.py @@ -0,0 +1,109 @@ +import os +import ast +import pprint + +def extract_all_parametrize(filepath): + with open(filepath, "r") as f: + content = f.read() + + parametrize_data = {} + print(f"\n📂 Fichier : {filepath}") + + try: + tree = ast.parse(content, filename=filepath) + except Exception as e: + print(f"❌ Impossible de parser le fichier entier : {e}") + return parametrize_data + + for node in tree.body: + if isinstance(node, ast.FunctionDef) and node.name.startswith("test_"): + print(f" 🔍 Fonction test détectée : {node.name}") + param_names = None + param_values = [] + + for deco in node.decorator_list: + # Vérifie que c'est un appel à pytest.mark.parametrize + if ( + isinstance(deco, ast.Call) + and isinstance(deco.func, ast.Attribute) + and deco.func.attr == "parametrize" + ): + try: + # noms de paramètres + names_node = deco.args[0] + if isinstance(names_node, ast.Str) or ( + isinstance(names_node, ast.Constant) and isinstance(names_node.value, str) + ): + raw_str = getattr(names_node, "s", names_node.value) + param_names = [x.strip() for x in raw_str.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 = [] + + # valeurs + values_node = deco.args[1] + if isinstance(values_node, ast.List): + for elt in values_node.elts: + if isinstance(elt, ast.Call): # ex: pytest.param(...) + value = [] + for arg in elt.args: + try: + value.append(ast.literal_eval(arg)) + except Exception: + value.append("") + param_values.append(value) + elif isinstance(elt, ast.Tuple): + try: + value = [ast.literal_eval(e) for e in elt.elts] + param_values.append(value) + except Exception: + param_values.append([""]) + else: + try: + value = [ast.literal_eval(elt)] + param_values.append(value) + except Exception: + param_values.append([""]) + + if param_names: + parametrize_data[node.name] = { + "names": param_names, + "values": param_values + } + print(f" ✅ Paramétré : {param_names} → {len(param_values)} valeurs") + + except Exception as e: + print(f" ⚠️ Décorateur parametrize ignoré : {e}") + + if not param_names: + print(f" ⚠️ Pas de parametrize") + + return parametrize_data + +def find_test_files(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 traitement de {filepath} : {e}") + + return all_params + +if __name__ == "__main__": + result = extract_all_tests_params("tests") + print("\n📊 Résultat final :") + pprint.pprint(result, sort_dicts=False)