Files
tryton/doc/topics/testing.rst
T
Cédric Krier 8f691f23ab Test building documentation
issue11367
review397811002
2022-04-06 18:19:49 +02:00

113 lines
3.2 KiB
ReStructuredText

.. _topics-testing:
=======
Testing
=======
Tryton supports both functional and unit tests.
Testing your module
~~~~~~~~~~~~~~~~~~~
Functional tests
----------------
Functional tests are written as doctests_ using proteus.
Unit tests
----------
Tryton provides the :class:`~trytond.tests.test_tryton.ModuleTestCase` class
that bundles a set of tests that are useful for every module.
Unit tests in :class:`~trytond.tests.test_tryton.ModuleTestCase` can be
decorated with :func:`~trytond.tests.test_tryton.with_transaction` to run the
test in a transaction.
To use it in your own module you just have to inherit from
:class:`~trytond.tests.test_tryton.ModuleTestCase` and set the class attribute
:attr:`~trytond.tests.test_tryton.ModuleTestCase.module` to the name of your
module.
.. code-block:: python
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
class MyModuleTestCase(ModuleTestCase):
"My Module Test Case"
module = 'my_module'
@with_transaction()
def test_method(self):
"Test method"
self.assertTrue(True)
Tests from this modules are found by the function
``trytond.modules.my_module.tests.suite`` which must return a
``unittest.TestSuite`` containing all the module's tests. This function is
called by the Tryton test runner script to gather all the tests.
A typical ``suite()`` function thus looks like this:
.. code-block:: python
def suite():
suite = trytond.tests.test_tryton.suite()
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(
MyModuleTestCase))
suite.addTests(doctest.DocFileSuite('scenario_my_module.rst',
tearDown=doctest_teardown, encoding='utf-8',
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
checker=doctest_checker))
return suite
.. _doctests: https://docs.python.org/library/doctest.html
.. _unittest: https://docs.python.org/library/unittest.html
Running your module's tests
---------------------------
Tryton provides the script ``trytond/tests/run-tests.py``, just invoke it like
that:
.. code-block:: console
$ python -m trytond.tests.run-tests -m my_module
Testing trytond
~~~~~~~~~~~~~~~
Extending tests
---------------
Python modules extending ``trytond`` core can define additional tests that
should be added to the existing ones.
Those modules must create an entry point ``trytond.tests``.
Any file in the module path specified by this entry point starting with
``test_`` and ending by ``.py`` will be imported.
Each of those file must define a ``suite()`` function that returns a
``unittest.TestSuite`` that will be included in the ``trytond`` test suite.
If the module from the entry point defines a ``register`` function it will be
called when registering the test-specific models in the
:class:`~trytond.pool.Pool`.
Running trytond tests
---------------------
You should use the script ``trytond/tests/run-tests.py`` by invoking it like
that:
.. code-block:: console
$ python -m trytond.tests.run-tests
You can use a different configuration file to check ``trytond`` against
different backend:
.. code-block:: console
$ python -m trytond.tests.run-tests -c <config>