Files
tryton/trytond/doc/topics/testing.rst
T

108 lines
2.8 KiB
ReStructuredText
Raw Normal View History

2017-10-02 11:38:31 +02:00
.. _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
----------
2022-03-27 16:06:09 +02:00
Tryton provides the :class:`~trytond.tests.test_tryton.ModuleTestCase` class
that bundles a set of tests that are useful for every module.
2017-10-02 11:38:31 +02:00
2022-04-06 18:19:49 +02:00
Unit tests in :class:`~trytond.tests.test_tryton.ModuleTestCase` can be
2022-03-27 16:06:09 +02:00
decorated with :func:`~trytond.tests.test_tryton.with_transaction` to run the
test in a transaction.
2017-10-02 11:38:31 +02:00
To use it in your own module you just have to inherit from
2022-03-27 16:06:09 +02:00
:class:`~trytond.tests.test_tryton.ModuleTestCase` and set the class attribute
:attr:`~trytond.tests.test_tryton.ModuleTestCase.module` to the name of your
module.
2017-10-02 11:38:31 +02:00
2022-03-27 16:06:09 +02:00
.. code-block:: python
2017-10-02 11:38:31 +02:00
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)
2017-10-02 11:38:31 +02:00
del ModuleTestCase
2017-10-02 11:38:31 +02:00
.. note::
The ``ModuleTestCase`` must be deleted to not be discovered by ``unittest``
as it fails to run without module declaration.
2017-10-02 11:38:31 +02:00
.. _doctests: https://docs.python.org/library/doctest.html
.. _unittest: https://docs.python.org/library/unittest.html
Running trytond's tests
-----------------------
You can run a specific test file using ``unittest`` command line like:
.. code-block:: console
$ python -m unittest trytond.tests.test_tools
To run all trytond's tests using discover of ``unittest`` with:
.. code-block:: console
$ python -m unittest discover -s trytond.tests
To run all modules tests:
.. code-block:: console
$ python -m unittest discover -s trytond.modules
2017-10-02 11:38:31 +02:00
Running your module's tests
---------------------------
You just need to replace the directory path with the one of your module:
2017-10-02 11:38:31 +02:00
2022-03-27 16:06:09 +02:00
.. code-block:: console
$ python -m unittest discover -s trytond.modules.my_module.tests
2017-10-02 11:38:31 +02:00
Extending trytond's tests
-------------------------
Python modules extending ``trytond`` core can define additional classes to
register in ``tests`` module.
Those modules must create an entry point ``trytond.tests`` which defines a
``register`` function to be called with the module name.
Testing options
2017-10-02 11:38:31 +02:00
~~~~~~~~~~~~~~~
Tryton runs tests against the configured database backend.
You can specify the name of the database to use via the environment variable
``DB_NAME``.
Otherwise it generates a random name.
2017-10-02 11:38:31 +02:00
A configuration file can be used by setting its path to the environment
variable ``TRYTOND_CONFIG``.
2017-10-02 11:38:31 +02:00
The tests recreate frequently the database. You can accelerate the creation by
setting a cache directory in ``DB_CACHE`` environment which will be used to
dump and restore initial databases backups.