The two previous recipes showed how to write tests. Let's see how to run them! This recipe works for both YAML and Python unit tests.
We will be reusing the tests for the my_module
module from one of the previous recipes. You will need an instance with the addon installed. In this recipe, we assume that the instance configuration file is in project.cfg
.
To run the tests for my module addon, run the following command:
$ odoo/odoo.py -c project.cfg --test-enable --log-level=error --stop-after-init -u my_module
The key part in this recipe is the --test-enable
command-line flag that tells Odoo to run the tests. The --stop-after-init
flag will stop the instance after the tests have run and -u
will update the specified module. When an update (or install) is performed in test mode, all the affected addon modules' tests are run (this includes dependencies automatically installed or reverse dependencies automatically updated; see the recipe Install and upgrade local addon modules in Chapter 2, Managing Odoo Server Instances, for more information on this).
You can run the tests using the following log configuration: --log-level=error --log-handler=openerp.modules.loading:INFO
. This allows to have information about the various tests' files to be processed but not the details of the logs of the operations, only the error messages.
The main drawback of this way of running tests is that you have to run all the tests for a given addon module, even if you know that they all pass but the one you are trying to fix, and you also have to update the addon (which can be in itself a costly operation).
There is a way around this. If you want to run the tests in my_module/tests/test_library.py
, you can use the following command:
$ odoo/odoo.py -c project.cfg --log-level=error --stop-after-init --test-file my_module/tests/test_library.py
This will run only the tests defined in that file, and it works too if you specify a file containing YAML tests. It will also skip updating the module, so it will not work if you have changed the model structure by adding new fields or modified the data files of the addon since the last time the module was updated.
One final note about tests:
Always be suspicious if your tests are not in error on the first run. There is a good chance this means you made a mistake. A typical goof is to forget to add the YAML test file in the __openerp__.py
file, or to forget to import the test file in tests/__init__.py
. It is always a good idea when tests are passing on the first run to force a failure, for instance, by adding assert False
to the first line of a method and running them again.