gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.8KB

  1. import importlib
  2. import importlib.util
  3. import inspect
  4. import logging
  5. import sys
  6. import threading
  7. from pathlib import Path
  8. from unittest import case
  9. from .. import tools
  10. from .tag_selector import TagsSelector
  11. from .suite import OdooSuite
  12. from .result import OdooTestResult
  13. _logger = logging.getLogger(__name__)
  14. def get_module_test_cases(module):
  15. """Return a suite of all test cases contained in the given module"""
  16. for obj in module.__dict__.values():
  17. if not isinstance(obj, type):
  18. continue
  19. if not issubclass(obj, case.TestCase):
  20. continue
  21. if obj.__module__ != module.__name__:
  22. continue
  23. test_case_class = obj
  24. test_cases = test_case_class.__dict__.items()
  25. if getattr(test_case_class, 'allow_inherited_tests_method', False):
  26. # keep iherited method for specific classes.
  27. # This is likely to be removed once a better solution is found
  28. test_cases = inspect.getmembers(test_case_class, callable)
  29. else:
  30. # sort test case to keep the initial behaviour.
  31. # This is likely to be removed in the future
  32. test_cases = sorted(test_cases, key=lambda pair: pair[0])
  33. for method_name, method in test_cases:
  34. if not callable(method):
  35. continue
  36. if not method_name.startswith('test'):
  37. continue
  38. yield test_case_class(method_name)
  39. def get_test_modules(module):
  40. """ Return a list of module for the addons potentially containing tests to
  41. feed get_module_test_cases() """
  42. results = _get_tests_modules(importlib.util.find_spec(f'odoo.addons.{module}'))
  43. results += list(_get_upgrade_test_modules(module))
  44. return results
  45. def _get_tests_modules(mod):
  46. spec = importlib.util.find_spec('.tests', mod.name)
  47. if not spec:
  48. return []
  49. tests_mod = importlib.import_module(spec.name)
  50. return [
  51. mod_obj
  52. for name, mod_obj in inspect.getmembers(tests_mod, inspect.ismodule)
  53. if name.startswith('test_')
  54. ]
  55. def _get_upgrade_test_modules(module):
  56. upgrade_modules = (
  57. f"odoo.upgrade.{module}",
  58. f"odoo.addons.{module}.migrations",
  59. f"odoo.addons.{module}.upgrades",
  60. )
  61. for module_name in upgrade_modules:
  62. if not importlib.util.find_spec(module_name):
  63. continue
  64. upg = importlib.import_module(module_name)
  65. for path in map(Path, upg.__path__):
  66. for test in path.glob("tests/test_*.py"):
  67. spec = importlib.util.spec_from_file_location(f"{upg.__name__}.tests.{test.stem}", test)
  68. if not spec:
  69. continue
  70. pymod = importlib.util.module_from_spec(spec)
  71. sys.modules[spec.name] = pymod
  72. spec.loader.exec_module(pymod)
  73. yield pymod
  74. def make_suite(module_names, position='at_install'):
  75. """ Creates a test suite for all the tests in the specified modules,
  76. filtered by the provided ``position`` and the current test tags
  77. :param list[str] module_names: modules to load tests from
  78. :param str position: "at_install" or "post_install"
  79. """
  80. config_tags = TagsSelector(tools.config['test_tags'])
  81. position_tag = TagsSelector(position)
  82. tests = (
  83. t
  84. for module_name in module_names
  85. for m in get_test_modules(module_name)
  86. for t in get_module_test_cases(m)
  87. if position_tag.check(t) and config_tags.check(t)
  88. )
  89. return OdooSuite(sorted(tests, key=lambda t: t.test_sequence))
  90. def run_suite(suite):
  91. # avoid dependency hell
  92. from ..modules import module
  93. module.current_test = True
  94. threading.current_thread().testing = True
  95. results = OdooTestResult()
  96. suite(results)
  97. threading.current_thread().testing = False
  98. module.current_test = False
  99. return results
上海开阖软件有限公司 沪ICP备12045867号-1