1
0

generate-names-file-from-failed-test-reports.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. """
  3. tests.support.generate-from-names-from-failed-test-reports
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. This script is meant as a stop-gap until we move to PyTest to provide a functionality similar to
  6. PyTest's --last-failed where PyTest only runs last failed tests.
  7. """
  8. # pylint: disable=resource-leakage
  9. from __future__ import absolute_import, print_function, unicode_literals
  10. import argparse
  11. import glob
  12. import os
  13. import sys
  14. try:
  15. import xunitparser
  16. except ImportError:
  17. sys.stderr.write(
  18. "Please install the xunitparser python package to run this script\n"
  19. )
  20. sys.stderr.flush()
  21. sys.exit(1)
  22. REPO_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
  23. def main():
  24. parser = argparse.ArgumentParser()
  25. parser.add_argument(
  26. "--reports-dir",
  27. default=os.path.join(REPO_ROOT, "artifacts", "xml-unittests-output"),
  28. help="Path to the directory where the JUnit XML reports can be found",
  29. )
  30. parser.add_argument(
  31. "output_file",
  32. help="Path to the file containing the failed tests listing to be fed to --names-files",
  33. )
  34. options = parser.parse_args()
  35. total_xml_reports = 0
  36. failures = set()
  37. for fname in sorted(glob.glob(os.path.join(options.reports_dir, "*.xml"))):
  38. total_xml_reports += 1
  39. with open(fname) as rfh:
  40. test_suite, test_result = xunitparser.parse(rfh)
  41. if not test_result.errors and not test_result.failures:
  42. continue
  43. for test in test_suite:
  44. if test.bad:
  45. failures.add("{classname}.{methodname}".format(**test.__dict__))
  46. if not total_xml_reports:
  47. parser.exit(status=1, message="No JUnit XML files were parsed")
  48. with open(options.output_file, "w") as wfh:
  49. wfh.write(os.linesep.join(sorted(failures)))
  50. parser.exit(status=0)
  51. if __name__ == "__main__":
  52. main()