2018-01-16 06:58:15 +01:00
|
|
|
:banner: banners/testing_modules.jpg
|
|
|
|
|
|
|
|
.. _reference/testing:
|
|
|
|
|
|
|
|
|
|
|
|
===============
|
|
|
|
Testing Modules
|
|
|
|
===============
|
|
|
|
|
2018-02-12 08:55:56 +01:00
|
|
|
Flectra provides support for testing modules using unittest.
|
2018-01-16 06:58:15 +01:00
|
|
|
|
|
|
|
To write tests, simply define a ``tests`` sub-package in your module, it will
|
|
|
|
be automatically inspected for test modules. Test modules should have a name
|
|
|
|
starting with ``test_`` and should be imported from ``tests/__init__.py``,
|
|
|
|
e.g.
|
|
|
|
|
|
|
|
.. code-block:: text
|
|
|
|
|
|
|
|
your_module
|
|
|
|
|-- ...
|
|
|
|
`-- tests
|
|
|
|
|-- __init__.py
|
|
|
|
|-- test_bar.py
|
|
|
|
`-- test_foo.py
|
|
|
|
|
|
|
|
and ``__init__.py`` contains::
|
|
|
|
|
|
|
|
from . import test_foo, test_bar
|
|
|
|
|
|
|
|
.. 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
|
2018-02-12 08:55:56 +01:00
|
|
|
`unittest documentation`_, but Flectra provides a number of utilities and helpers
|
|
|
|
related to testing Flectra content (modules, mainly):
|
2018-01-16 06:58:15 +01:00
|
|
|
|
2018-02-12 08:55:56 +01:00
|
|
|
.. autoclass:: flectra.tests.common.TransactionCase
|
2018-01-16 06:58:15 +01:00
|
|
|
:members: browse_ref, ref
|
|
|
|
|
2018-02-12 08:55:56 +01:00
|
|
|
.. autoclass:: flectra.tests.common.SingleTransactionCase
|
2018-01-16 06:58:15 +01:00
|
|
|
:members: browse_ref, ref
|
|
|
|
|
2018-02-12 08:55:56 +01:00
|
|
|
.. autoclass:: flectra.tests.common.SavepointCase
|
2018-01-16 06:58:15 +01:00
|
|
|
|
2018-02-12 08:55:56 +01:00
|
|
|
.. autoclass:: flectra.tests.common.HttpCase
|
2018-01-16 06:58:15 +01:00
|
|
|
:members: browse_ref, ref, url_open, phantom_js
|
|
|
|
|
|
|
|
By default, tests are run once right after the corresponding module has been
|
|
|
|
installed. Test cases can also be configured to run after all modules have
|
|
|
|
been installed, and not run right after the module installation:
|
|
|
|
|
2018-02-12 08:55:56 +01:00
|
|
|
.. autofunction:: flectra.tests.common.at_install
|
2018-01-16 06:58:15 +01:00
|
|
|
|
2018-02-12 08:55:56 +01:00
|
|
|
.. autofunction:: flectra.tests.common.post_install
|
2018-01-16 06:58:15 +01:00
|
|
|
|
|
|
|
The most common situation is to use
|
2018-02-12 08:55:56 +01:00
|
|
|
:class:`~flectra.tests.common.TransactionCase` and test a property of a model
|
2018-01-16 06:58:15 +01:00
|
|
|
in each method::
|
|
|
|
|
|
|
|
class TestModelA(common.TransactionCase):
|
|
|
|
def test_some_action(self):
|
|
|
|
record = self.env['model.a'].create({'field': 'value'})
|
|
|
|
record.some_action()
|
|
|
|
self.assertEqual(
|
|
|
|
record.field,
|
|
|
|
expected_field_value)
|
|
|
|
|
|
|
|
# other tests...
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
Test methods must start with ``test_``
|
|
|
|
|
|
|
|
Running tests
|
|
|
|
-------------
|
|
|
|
|
|
|
|
Tests are automatically run when installing or updating modules if
|
2018-02-12 08:55:56 +01:00
|
|
|
:option:`--test-enable <flectra-bin --test-enable>` was enabled when starting the
|
|
|
|
Flectra server.
|
2018-01-16 06:58:15 +01:00
|
|
|
|
2018-02-12 08:55:56 +01:00
|
|
|
As of Flectra 8, running tests outside of the install/update cycle is not
|
2018-01-16 06:58:15 +01:00
|
|
|
supported.
|
|
|
|
|
|
|
|
.. _unittest documentation: https://docs.python.org/2/library/unittest.html
|