Now as your application is launched in the market, suddenly you are welcomed with a list of issues. As a developer, you must be thinking that everything was working well so from where these issues came, right?
So how to deal with this? Rather looking for a temporary solution you must think of a solution that can permanently help you in detecting such issues and help in eliminating it.
Automated tests are a useful tool to build and maintain the module. By using automated tests we can eliminate manual testing errors. It improves overall test coverage while reducing software testing efforts.
How to add unit tests in a module?
We can add unit tests to any Odoo module by using a "tests"subdirectory. The test runner will automatically run all ".py" files from tests subdirectory of the module. Note that, all ".py" files names in test directory must start with “test_”, otherwise that file will not be executed.
Steps to create a test.
1. Create a new module: Let’s create a new module that calculates employee designation based on his/her experience.
Here, I have created a model.py and in the model.py file, I have declared fields as following:
class Employee(models.Model):
_name = 'employee.employee'
name = fields.Char(required=True)
age = fields.Integer()
date_of_birth = fields.Date(String='Date of birth', required=True)
address = fields.Text()
email = fields.Char()
experience = fields.float()
designation = fields.Selection(
[('trainee', 'Trainee'),
('junior_developer', 'Junior developer'),
('senior_developer', 'Senior developer')],
compute="_compute_employee_designation")
In the model.py file add compute method as shown below.
@api.depends('experience')
def _compute_employee_designation(self):
'''
count experience using year
'''
if self.experience < 1:
self.designation = "trainee"
elif self.experience >= 1 and self.experience < 2:
self.designation = "junior_developer"
else:
self.designation = "senior_developer"
Now, we will check the functionality of this functions using test cases.
2. Add test cases: In order to write unit tests for the module, perform the following steps:
Create a subdirectory called “tests” inside your module:
Create "__init__.py" python file in the “tests” directory and import "test_employee" python file in that.
Now open the “test_employee.py” file and import common class to create a testcase class structure.
from odoo.tests import common
class TestEmployee(common.TransactionCase):
def setUp(self):
super(TestEmployee, self).setUp()
# Add the set up code here...
def test_compute_employee_designation(self):
# Add test code here
Here, I have used “common.TransactionsCase” that tells Odoo to rollback record after the test is done.
In the setUP() method we will create data and variables that are used by the test method. This method is automatically called before the execution of the test method.
Add data by the setUp() method to create an employee record as shown below.
Add the test method to check the designation of the employee.
def test_compute_employee_designation(self):
'''
This function test the employee_position function functionality
Here position of employee counting using the month
'''
# check position of the employee1
self.assertEqual(self.employee1.designation, 'junior_developer')
# check position of the employee2
self.assertEqual(self.employee2.designation, 'senior_developer')
# change the experience of employee2
self.employee2.write({
'experience': 0.2,
})
# again check the position of employee
self.assertEqual(self.employee2.designation, 'trainee')
3. How to run the test: To run the test we have to fire the following command. ./odoo-bin -i <module name> --test-enable -d <db name> --stop-after-init --addons-path=<addons path>
Writing and using tests in Odoo is quite easy to do. It is a great feature to use if you’re writing a lot of custom code and want to keep an overview.
We use cookies to deliver personalized content, analyze trends, administer the site, track user movements on the site, and collect demographic information about our user base as a whole. Accept all cookies for the best possible experience on our website or manage your preferences.
What For?