Testing is a process through which we can ensure the functionality and quality of our modules or code. With testing, we can ensure that the system meets the requirements that guided its design and development. In case of Odoo, they are providing some features for testing the modules within Odoo. We can simply write the test cases and ensure the correct functionality. There are some points we have to consider before writing a test case. In this blog, we will discuss the different steps for writing a test case in Odoo.
First, we will discuss the structure of test cases.
For writing a test case, we have to create a subdirectory, ‘tests’ in your module and import it in your_module/__init__.py. Inside the ‘tests’ directory, you can define your test cases using python files. The hierarchy should be like the following.
your_module
|-- ...
`-- tests
|-- __init__.py
|-- test_bar.py
` -- test_foo.py
Note that the tests which are not imported in your_module/tests/__init__.py will not be executed.
By default, tests are run just right after the corresponding module has been installed. Test cases can also be configured to run after all modules are installed.
Writing Test Cases
- The most common situation is to use transaction case and test a property of a model in each method.
- The TransactionCase tests use a different transaction for each test and are rolled back at the end. We can also use the SingleTransactionCase that runs all the tests in a single transaction that is rolled back only at the end of the last test.
from odoo.tests.common import TransactionCase
class SampleTest(TransactionCase):
def setUp(self):
super(SampleTest, self).setUp()
# Add test setup code here
self._record = self.env['model.a'].create({'field': 'value'})
def test_fsm_operations(self):
# Add test code
self.assertEqual(self._record.field, expected value, msg=”Value changed !”)
The Setp() is used to set up the test and once the setup is done, we can construct the tests. We can ensure the functionality by performing the required actions in those tests. For example, we can create records, execute any functions, check the values etc. In the above code, the ‘self.assertEqual()’ is used to verify the field value of the created record.
It will check the field value with the provided value and if the test fails, then we will get the message specified as information.
Instead of assertEqual(), we can use assertTrue(), assertFalse(), assertRaises(), etc for this purpose.
How To Run Test Cases?
For running the tests, you have to use test_enable = True in your odoo-server.conf file. After it is enabled, tests will automatically run when you update or install the modules.
Note that, tests defined for all the modules will be executed when you run Odoo. If you need to test a particular module only, then you can achieve this by running
“python ./odoo.py -i module_to_test --log-level=test -d database_name -c config_file --test-enable --stop-after-init”
Here the “-c config_file” part is optional, we can use it to specify the Odoo-server.conf if multiple instances of Odoo is running there.