Chapter 12 - Odoo 15 Development Book

Python Testing

With regards to growing huge applications, involving robotized experiments is a decent practice for working on the unwavering quality of your module. This makes your module more vigorous. Consistently, Odoo discharges another form of its product, and robotized experiments are extremely useful in distinguishing relapse in your application, which might have been brought about by an adaptation redesign. Fortunately, any Odoo structure accompanies different mechanized testing utilities. Odoo incorporates the accompanying three fundamental sorts of tests:

  • Python unit tests: useful for testing model business logic
  • JS unit tests: useful to test the javascript code in isolation
  • Tours: tours simulate a real situation. They ensure that the python and the javascript parts properly talk to each other.

Adding Python Test Cases

Python test cases are used to check the correctness of business logic.Odoo provides support for testing modules using Python’s unittest library.

Test file structure

your_module

---

----tests

------__init__.py

------test_sale.py

------test_purchase.py

While writing tests

  • Define a tests sub-package in your module.
  • All the test files inside Tests sub-package should start with string test_
  • All tests must be included in tests/__init__.py
  • All tests must extend an Odoo test case class
  • All test methods must start with def test_

Here, the tests/__init__.py contains

from . import test_sale
from . import test_purchase

Extend an Odoo test case class

Odoo provides a number of utilities and helpers related to testing. Odoo content mainly test case classes, while writing test cases in Odoo, we need to extend any of these classes.

  • odoo.tests.common.TransactionCase
  • It is the most commonly used test case class. The test class in which all test methods are run in a single transaction, but each test method is run in a sub-transaction managed by a savepoint. The transaction’s cursor is always closed without committing

  • Odoo.tests.common.SingleTransactionCase
  • TestCase, in which all test methods are run in the same transaction, the transaction is started with the first test method and rolled back at the end of the last.

  • odoo.tests.common.HttpCase
  • odoo.tests.common.SavepointCase
  • Transactional HTTP TestCase with url_open and Chrome headless helpers.

After extending any of these classes, your code looks like the following

# -*- coding: utf-8 -*-
from odoo.tests import TransactionCase
class MyTest(TransactionCase):

All the test files should start with test_

While creating new test files, make sure that all of your test files are prefixed with test_,.

E.g., test_sale.py or test_purchase.py

All the test methods should start with test_

While writing test cases, the test method should start with test_

Eg:

# -*- coding: utf-8 -*-
from odoo.tests import TransactionCase
class MyTest(TransactionCase):
   def test_string_concatenation_and_length(self):
       //add button here

Writing A Test- Example

# -*- coding: utf-8 -*-
from odoo.tests import TransactionCase
class MyTest(TransactionCase):
   def test_string_concatenation_and_length(self):
       student = self.env['student.student'].create({'f_name': "Ajay", 'l_name': "Malhothra"})
       """student name checking"""
       self.assertEqual(
           # Actual results from function call...
           student.name,
           # Expected results from the function call...
           "AjayMalhothra"
       )
       """student name length checking"""
       self.assertEqual(
           # Actual results from function call...
           student.string_length(),
           # Expected results from the function call...
           15
       )

The above example, we wrote one test method called test_string_concatenation_and_length() , and it will check the string concatenation and length of the concatenated string.

Running Tests

We can run the odoo test cases in different ways

  • Enabling --test-enable when starting the Odoo server. It runs tests after module installation or update.
  • Using --test-file <file>, it will runs a python test file
  • Using --test-tags [-][tag][/module][:class][.method]
whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message