Writing Test Cases in Odoo 14

Writing Test Cases in Odoo 14

Testing is a cycle through which we can guarantee the usefulness and nature of our modules or code. With testing, we can guarantee that the framework meets the prerequisites that guided its plan and improvement. In Odoo, they are giving a few highlights for testing the modules inside it. We can just compose the experiments and guarantee the right usefulness. There are a few focuses we need to consider prior to composing an experiment. In this blog, we will talk about the various strides for composing an experiment in Odoo.

To start with, we will examine the construction of test cases.

In order to write a test case in odoo, we have to make a sub folder, ‘test’ in our module and import it inside your_module/__init__.py. Inside the ‘test’ index, we can characterize your test case utilising python libraries. The order ought to resemble the accompanying.

writing-test-cases-in-odoo14s

Note that the tests which are not imported in your_module/tests/__init__.py won’t be executed.

And inside the __init__.py

from . import test_bar,test_foo
Note : Only the tests which are imported inside your_module/tests/__init__.py will be executed.
Requirements for Writing A Test Case
How will we write a test for it? Initially, we have some specific prerequisites for the Odoo test Framework, which we must make use of.Let us see how to apply those inside the sample test we have written. The points to be considered are as follows:
> All the tests should extend an Odoo action class
> All the tests should be included inside tests/__init__.py
> All the test files should start like test_.py
> All the test methods should start like def test_(self):
> All the tests should be put inside the tests directory of your module
Extend An Odoo Test Class
We have some predefined test classes to work within Odoo, Once we extend them inside our module, we can use it.
odoo.tests.common.TransactionCase     #Most common
odoo.tests.common.SingleTransactionCase
odoo.tests.common.HttpCase
odoo.tests.common.SavepointCase
Now our test class should look like this:
# -*- coding: utf-8 -*-
from odoo.tests import TransactionCase
class MyTests(TransactionCase):
   ...
Running Tests
At last, In request to run our tests, We can run our test cases straightforwardly from the odoo.py or odoo-bin executables.
Once, if we have enabled the –test-enable when starting our odoo server. Our test cases will automatically run once we update or install the modules.
--test-enable
runs tests after installing modules
Requirements for running a test case
Like the prerequisites for composing a test. We have to ensure the following points before running the test cases.
> Always run odoo with the –test-enable flag.
> Always run odoo with the – d {my_database} flag.
> Always run odoo with the – i {modules_to_install}  flag.

Similar Posts

Leave a Reply

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