python_app_test.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import sys
  4. sys.dont_write_bytecode = True # Don't generate .pyc files / __pycache__ directories
  5. import os
  6. import sys
  7. import unittest
  8. # Python 3 moved urlopen to urllib.requests
  9. try:
  10. from urllib.request import urlopen
  11. except ImportError:
  12. from urllib import urlopen
  13. basedir = os.path.abspath(os.path.dirname(__file__))
  14. sys.path.append(os.path.join(os.path.split(basedir)[0], "modules", "python", "test"))
  15. from tests_common import NewOpenCVTests
  16. def load_tests(loader, tests, pattern):
  17. cwd = os.getcwd()
  18. config_file = 'opencv_apps_python_tests.cfg'
  19. locations = [cwd, basedir]
  20. if os.path.exists(config_file):
  21. with open(config_file, 'r') as f:
  22. locations += [str(s).strip() for s in f.readlines()]
  23. else:
  24. print('WARNING: OpenCV tests config file ({}) is missing, running subset of tests'.format(config_file))
  25. tests_pattern = os.environ.get('OPENCV_APPS_TEST_FILTER', 'test_*') + '.py'
  26. if tests_pattern != 'test_*.py':
  27. print('Tests filter: {}'.format(tests_pattern))
  28. processed = set()
  29. for l in locations:
  30. if not os.path.isabs(l):
  31. l = os.path.normpath(os.path.join(cwd, l))
  32. if l in processed:
  33. continue
  34. processed.add(l)
  35. print('Discovering python tests from: {}'.format(l))
  36. sys_path_modify = l not in sys.path
  37. if sys_path_modify:
  38. sys.path.append(l) # Hack python loader
  39. discovered_tests = loader.discover(l, pattern=tests_pattern, top_level_dir=l)
  40. print(' found {} tests'.format(discovered_tests.countTestCases()))
  41. tests.addTests(loader.discover(l, pattern=tests_pattern))
  42. if sys_path_modify:
  43. sys.path.remove(l)
  44. return tests
  45. if __name__ == '__main__':
  46. NewOpenCVTests.bootstrap()