pytest-postprocess.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. import argparse
  3. import glob
  4. import os
  5. import xml.etree.ElementTree as ET
  6. from mute_utils import MuteTestCheck, mute_target, recalc_suite_info
  7. from junit_utils import get_property_value
  8. def update_testname(testcase):
  9. filename = get_property_value(testcase, "filename")
  10. clsname = testcase.get("classname")
  11. tstname = testcase.get("name")
  12. if filename is None:
  13. return f"{clsname}::{tstname}"
  14. filename = filename.split("/")
  15. test_fn = filename[-1]
  16. folder = "/".join(filename[:-1])
  17. testcase.set("classname", folder)
  18. clsname = clsname.split(".")[-1]
  19. test_name = f"{test_fn}::{clsname}::{tstname}"
  20. testcase.set("name", test_name)
  21. testcase.set("id", f"{folder}_{test_fn}_{clsname}_{tstname}")
  22. return f"{folder}/{test_name}"
  23. def postprocess_pytest(fn, mute_check, dry_run):
  24. tree = ET.parse(fn)
  25. root = tree.getroot()
  26. for testsuite in root.findall("testsuite"):
  27. need_recalc = False
  28. for testcase in testsuite.findall("testcase"):
  29. new_name = update_testname(testcase)
  30. if mute_check(new_name) and mute_target(testcase):
  31. print(f"mute {new_name}")
  32. need_recalc = True
  33. if need_recalc:
  34. recalc_suite_info(testsuite)
  35. print(f"{'(dry-run) ' if dry_run else ''}save {fn}")
  36. if not dry_run:
  37. tree.write(fn, xml_declaration=True, encoding="UTF-8")
  38. def main():
  39. parser = argparse.ArgumentParser()
  40. parser.add_argument("--filter-file", required=True)
  41. parser.add_argument("--dry-run", action="store_true", default=False)
  42. parser.add_argument("pytest_xml_path")
  43. args = parser.parse_args()
  44. if not os.path.isdir(args.pytest_xml_path):
  45. print(f"{args.pytest_xml_path} is not a directory, exit")
  46. raise SystemExit(-1)
  47. mute_check = MuteTestCheck(args.filter_file)
  48. for fn in glob.glob(os.path.join(args.pytest_xml_path, "*.xml")):
  49. postprocess_pytest(fn, mute_check, args.dry_run)
  50. if __name__ == "__main__":
  51. main()