How to Write Test Case in the Odoo 16 ERP

How to Write Test Case in the Odoo 16 ERP

2 minutes, 23 seconds Read

A test is a cycle to ensure the usefulness and completeness of a module or code. Through testing, we can ensure that the framework meets the requirements that guide its planning and improvement. Odoo shows some highlights for testing, including modules. Experiments can be easily put together to ensure adequate returns. Before wrapping up the experiment, there are some important points to consider. 

This blog will guide you through the different steps of creating an experiment in Odoo.

First, examine the structure of the test case.

To create test cases in Odoo, you need to create a subfolder ‘test’ in your module and import it into your_module/__init__.py. Within the “test” index, you can use Python libraries to characterize your test cases. The order ought to resemble the accompanying.

How to Write Test Case in the Odoo 16 ERP-cybrosys

And inside the __init__.py

from . import test_function1,test_function2

Note: Only tests imported in your_module/tests/__init__.py will be run.

Requirements for writing test cases

How would you write a test for this? First, there are some prerequisites for a particular Odoo test framework. Let’s see how these are applied within the sample test we created. Here are some things to consider:

* All tests should extend to Odoo action class

* All tests must be included in the tests/__init__.py

* All test files should start like test_.py

* All test methods should start like def test_name(self):

* All tests should be in the module’s tests directory

Creating the Classes:-

There are some predefined test classes for working with Odoo. Once you’ve extended them with your module, you can use them.

odoo.tests.common.TransactionCase 	#Most common
odoo.tests.common.SingleTransactionCase
odoo.tests.common.HttpCase
odoo.tests.common.SavepointCase

The test class looks like this:

from odoo.tests import common
class TestModule(common.TransactionCase):
    def setUp(self):
        super(TestModule, self).setUp()
        # Add test setUp code here
    def test_some_actions(self):
        # Add test code here

The setup() method creates the data and variables we want to use. These are typically stored as class attributes so they can be used in test methods. Odoo uses the unittest extension library unittest.

Example:

You can write simple tests by running code from the object under test, looking for the result you want to check, and using the Assert function to compare it to the expected result.

  def test_some_actions(self):
        # add a record in a model.
        record = self.env['test.model'].create({'field':     'value'})
        record.some_action()
        # Check if the field and match the expected result
        self.assertEqual(record.field, 'expected_field_value')
        print('Your test was successful!')

Running Tests:

You can run your tests by running your test cases directly from the odoo.py or odoo-bin executables.

./odoo-bin -c {conf file} -d {my_database} -i {modules_to_install} --test-enable

When you enable –test-enable when starting the Odoo server. Test cases are automatically executed as soon as you update or install a module.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *