Development Book V17: Test cases

Python test

Using robotized trials is a good approach for improving the stability of your module when developing large apps. Your module becomes more active as a result. Odoo releases an alternate version of its product on a regular basis, and automated tests are very helpful in identifying application relapses that may have resulted from adaptive redesign. Fortunately, various automated testing tools come with every Odoo framework. Odoo has the following three basic types of tests:

  • Python Unit Testing: beneficial for evaluating business logic models
  • JavaScript Unit Testing: helpful for testing the code independently.
  • Tours: tours act as a lifelike simulation. They guarantee that the javascript and python components communicate with each other correctly.

Adding Python Test Cases

your_module
|
|---- …
|----  python_test_cases
      |-----     __init__.py
      |------    test_purchase.py
      |-----    test_sale.py

When composing examinations

  • You should define a test subpackage in your module.
  • Test_ is a required prefix for all test files in the Tests subpackage
  • Test_ is a required prefix for all test files in the Tests subpackage
  • Every test needs to expand upon the Odoo test case class.
  • Every test method needs to begin with def test_.

Here, the tests/__init__.py contains

from . import test_sale from . import test_purchase

Extend an Odoo test case class

Odoo offers several testing-related tools and resources. Test case classes make up the majority of Odoo content; therefore, we must extend any of these classes while building test cases in Odoo.

  • odoo.tests.common.TransactionCase
    This test case class is utilized the most frequently. The test class in which every test method is executed in a sub-transaction overseen by a savepoint, yet all test methods are executed in a single transaction. The transaction's cursor is never committed; it is always closed.
  • odoo.tests.common.SingleTransactionCase TestCase: In this scenario, the transaction is initiated with the first test method and rolled back at the conclusion of the last. All test methods are executed within the same transaction.
  • odoo.tests.common.HttpCase
  • odoo.tests.common.SavepointCase

Chrome headless helpers and url_open are used in this transactional HTTP test case.

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_

Make careful to prefix all of your test files with test_, when you create new ones.

All the test methods should start with test_

Test_ should be the first character in the test method when creating test cases.

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

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)

In the example above, we have written a test method called test_string_concatenation_and_length(), which checks the length of the concatenated string as well as the string concatenation.

Running Tests

We can run the odoo test cases in different ways

  • Starting the Odoo server with --test-enable enabled. After installing or updating a module, tests are conducted.
  • It will run a Python test file by using the option --test-file .
  • Using the [-] --test-tags[tag][/module][:class][.method]
whatsapp
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