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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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, unicode_literals, print_function
  10. import os
  11. import sys
  12. import glob
  13. import argparse
  14. try:
  15. import xunitparser
  16. except ImportError:
  17. sys.stderr.write('Please install the xunitparser python package to run this script\n')
  18. sys.stderr.flush()
  19. sys.exit(1)
  20. REPO_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
  21. def main():
  22. parser = argparse.ArgumentParser()
  23. parser.add_argument(
  24. '--reports-dir',
  25. default=os.path.join(REPO_ROOT, 'artifacts', 'xml-unittests-output'),
  26. help='Path to the directory where the JUnit XML reports can be found'
  27. )
  28. parser.add_argument(
  29. 'output_file',
  30. help='Path to the file containing the failed tests listing to be fed to --names-files'
  31. )
  32. options = parser.parse_args()
  33. total_xml_reports = 0
  34. failures = set()
  35. for fname in sorted(glob.glob(os.path.join(options.reports_dir, '*.xml'))):
  36. total_xml_reports += 1
  37. with open(fname) as rfh:
  38. test_suite, test_result = xunitparser.parse(rfh)
  39. if not test_result.errors and not test_result.failures:
  40. continue
  41. for test in test_suite:
  42. if test.bad:
  43. failures.add('{classname}.{methodname}'.format(**test.__dict__))
  44. if not total_xml_reports:
  45. parser.exit(status=1, message='No JUnit XML files were parsed')
  46. with open(options.output_file, 'w') as wfh:
  47. wfh.write(os.linesep.join(sorted(failures)))
  48. parser.exit(status=0)
  49. if __name__ == '__main__':
  50. main()