Python tests are added to addon modules by using a tests/
subdirectory. The test runner will automatically discover tests in the subdirectories with that particular name.
The tests on our todo_wizard
addon will be in a tests/test_wizard.py
file. We will need to add the tests/__init__.py
file:
from . import test_wizard
And this would be the basic skeleton for the tests/test_wizard.py
:
# -*- coding: utf-8 -*- from odoo.tests.common import TransactionCase class TestWizard(TransactionCase): def setUp(self, *args, **kwargs): super(TestWizard, self).setUp(*args, **kwargs) # Add test setup code here... def test_populate_tasks(self): "Populate tasks buttons should add two tasks" # Add test code
Odoo provides a few classes to use for tests. The TransactionCase
tests uses a different transaction for each test, that is automatically rolled back at the end. We can also use the SingleTransactionCase
, that runs all tests in a single transaction, that is rolled back only at the end of the last test. This can be useful when you want the final state of each test to be the initial state for the following test.
The setUp()
method is where we prepare data and variables to be used. We will usually store them as class attributes, so that they are available to be used in the test methods.
Tests should then be implemented as class methods, like test_populate_tasks()
. The test cases method names must begin with a test_
prefix. They are automatically discovered, and this prefix is what identifies the methods implementing test cases.
Methods will be run in order of the test function names. When using the TransactionCase
class, a rollback will be done at the end of each. The method's docstring is shown when the tests are run, and should provides a short description for it.
These test classes are wrappers around unittest
testcases. This is part of the Python standard library, and you may refer to its documentation for more details at
https://docs.python.org/2/library/unittest.html
.
To be more precise, Odoo uses a unittest
extension library, unittest2
.