[IMP] test discovery, documentation, deprecation warnings

* document and warn that checks and fast_suite in tests sub-packages are
  deprecated and have no effect
* avoid iterating all currently loaded modules when looking for test
  modules in a tests sub-package
* replace use of __import__ by importlib

Fixes #3152
This commit is contained in:
Xavier Morel 2015-01-15 13:28:25 +01:00
parent da4db6a8ae
commit 9808ca3e31
2 changed files with 26 additions and 13 deletions

View File

@ -24,8 +24,16 @@ and ``__init__.py`` contains::
from . import test_foo, test_bar
.. note:: test modules which are not imported from ``tests/__init__.py`` will
not be run
.. warning::
test modules which are not imported from ``tests/__init__.py`` will not be
run
.. versionchanged:: 8.0
previously, the test runner would only run modules added to two lists
``fast_suite`` and ``checks`` in ``tests/__init__.py``. In 8.0 it will
run all imported modules
The test runner will simply run any test case, as described in the official
`unittest documentation`_, but Odoo provides a number of utilities and helpers
@ -46,8 +54,8 @@ been installed, and not run right after the module installation:
.. autofunction:: openerp.tests.common.post_install
The most common situation is to use
:class:`~openerp.tests.common.TransactionCase` and test a property of a of a
model in each method::
:class:`~openerp.tests.common.TransactionCase` and test a property of a model
in each method::
class TestModelA(common.TransactionCase):
def test_some_action(self):
@ -67,7 +75,7 @@ Tests are automatically run when installing or updating modules if
Odoo server.
As of Odoo 8, running tests outside of the install/update cycle is not
supported.
.. _unittest2: http://pypi.python.org/pypi/unittest2
.. _unittest documentation: https://docs.python.org/2/library/unittest.html

View File

@ -22,6 +22,8 @@
import functools
import imp
import importlib
import inspect
import itertools
import logging
import os
@ -366,23 +368,26 @@ def adapt_version(version):
return version
def get_test_modules(module):
""" Return a list of module for the addons potentialy containing tests to
""" Return a list of module for the addons potentially containing tests to
feed unittest2.TestLoader.loadTestsFromModule() """
# Try to import the module
module = 'openerp.addons.' + module + '.tests'
modpath = 'openerp.addons.' + module
try:
__import__(module)
mod = importlib.import_module('.tests', modpath)
except Exception, e:
# If module has no `tests` sub-module, no problem.
if str(e) != 'No module named tests':
_logger.exception('Can not `import %s`.', module)
return []
# include submodules too
result = [mod_obj for name, mod_obj in sys.modules.iteritems()
if mod_obj # mod_obj can be None
if name.startswith(module)
if re.search(r'test_\w+$', name)]
if hasattr(mod, 'fast_suite') or hasattr(mod, 'checks'):
_logger.warn(
"Found deprecated fast_suite or checks attribute in test module "
"%s. These have no effect in or after version 8.0.",
mod.__name__)
result = [mod_obj for name, mod_obj in inspect.getmembers(mod, inspect.ismodule)
if name.startswith('test_')]
return result
# Use a custom stream object to log the test executions.