driver.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Default driver for run tests (run-*.test).
  2. This imports the 'native' module (containing the compiled test cases)
  3. and calls each function starting with test_, and reports any
  4. exceptions as failures.
  5. Test cases can provide a custom driver.py that overrides this file.
  6. """
  7. import sys
  8. import native
  9. failures = []
  10. for name in dir(native):
  11. if name.startswith('test_'):
  12. test_func = getattr(native, name)
  13. try:
  14. test_func()
  15. except Exception as e:
  16. failures.append((name, sys.exc_info()))
  17. if failures:
  18. from traceback import print_exception, format_tb
  19. import re
  20. def extract_line(tb):
  21. formatted = '\n'.join(format_tb(tb))
  22. m = re.search('File "(native|driver).py", line ([0-9]+), in (test_|<module>)', formatted)
  23. if m is None:
  24. return "0"
  25. return m.group(1)
  26. # Sort failures by line number of test function.
  27. failures = sorted(failures, key=lambda e: extract_line(e[1][2]))
  28. # If there are multiple failures, print stack traces of all but the final failure.
  29. for name, e in failures[:-1]:
  30. print(f'<< {name} >>')
  31. sys.stdout.flush()
  32. print_exception(*e)
  33. print()
  34. sys.stdout.flush()
  35. # Raise exception for the last failure. Test runner will show the traceback.
  36. print(f'<< {failures[-1][0]} >>')
  37. sys.stdout.flush()
  38. raise failures[-1][1][1]