rununittests.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright 2009 Baptiste Lepilleur and The JsonCpp Authors
  2. # Distributed under MIT license, or public domain if desired and
  3. # recognized in your jurisdiction.
  4. # See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
  5. from __future__ import print_function
  6. from __future__ import unicode_literals
  7. from io import open
  8. from glob import glob
  9. import sys
  10. import os
  11. import os.path
  12. import subprocess
  13. import optparse
  14. VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes'
  15. class TestProxy(object):
  16. def __init__(self, test_exe_path, use_valgrind=False):
  17. self.test_exe_path = os.path.normpath(os.path.abspath(test_exe_path))
  18. self.use_valgrind = use_valgrind
  19. def run(self, options):
  20. if self.use_valgrind:
  21. cmd = VALGRIND_CMD.split()
  22. else:
  23. cmd = []
  24. cmd.extend([self.test_exe_path, '--test-auto'] + options)
  25. try:
  26. process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  27. except:
  28. print(cmd)
  29. raise
  30. stdout = process.communicate()[0]
  31. if process.returncode:
  32. return False, stdout
  33. return True, stdout
  34. def runAllTests(exe_path, use_valgrind=False):
  35. test_proxy = TestProxy(exe_path, use_valgrind=use_valgrind)
  36. status, test_names = test_proxy.run(['--list-tests'])
  37. if not status:
  38. print("Failed to obtain unit tests list:\n" + test_names, file=sys.stderr)
  39. return 1
  40. test_names = [name.strip() for name in test_names.decode('utf-8').strip().split('\n')]
  41. failures = []
  42. for name in test_names:
  43. print('TESTING %s:' % name, end=' ')
  44. succeed, result = test_proxy.run(['--test', name])
  45. if succeed:
  46. print('OK')
  47. else:
  48. failures.append((name, result))
  49. print('FAILED')
  50. failed_count = len(failures)
  51. pass_count = len(test_names) - failed_count
  52. if failed_count:
  53. print()
  54. for name, result in failures:
  55. print(result)
  56. print('%d/%d tests passed (%d failure(s))' % ( pass_count, len(test_names), failed_count))
  57. return 1
  58. else:
  59. print('All %d tests passed' % len(test_names))
  60. return 0
  61. def main():
  62. from optparse import OptionParser
  63. parser = OptionParser(usage="%prog [options] <path to test_lib_json.exe>")
  64. parser.add_option("--valgrind",
  65. action="store_true", dest="valgrind", default=False,
  66. help="run all the tests using valgrind to detect memory leaks")
  67. parser.enable_interspersed_args()
  68. options, args = parser.parse_args()
  69. if len(args) != 1:
  70. parser.error('Must provides at least path to test_lib_json executable.')
  71. sys.exit(1)
  72. exit_code = runAllTests(args[0], use_valgrind=options.valgrind)
  73. sys.exit(exit_code)
  74. if __name__ == '__main__':
  75. main()