test.py 1.8 KB

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