Python Testing
Keeping in mind the growth of large-scale applications, including robotics experiments, it is the right way to work on the unwavering quality of modules. This will harden the module. Odoo consistently releases alternative forms of products, and robotized experiments are very helpful in detecting application fallbacks that may have been caused by redesigning customizations. Fortunately, each Odoo structure comes with various mechanized test programs. Odoo comes with three basic types of tests included.
Adding Python Test Cases
Test file structure
Structure of test case:
- In your module, define a test subpackage.
- All test files in the Tests subpackage must start with the string test_.
- All tests should be in tests/__init__.py
- All tests must extend the Odoo test case class.
- All test methods must start with def test_
Here, the tests/__init__.py contains
from . import test_book
Extend an Odoo test case class
Odoo provides many utilities and helpers for testing. Odoo mainly contains test case classes. Each of these classes must be extended when writing test cases in Odoo.
● odoo.tests.common.SingleTransactionCase
A test case where all test methods are executed in the same transaction. Transactions are started in the first test method and rolled back at the end of the last test method.
● odoo.tests.common.TransactionCase
This is the most commonly used test case class. A test class where all test methods run in one transaction, but each test method runs in a subtransaction managed by a savepoint. Cursors in a transaction are always closed without committing
● odoo.tests.common.SavepointCase
Transactional HTTP test case with url_open and headless helpers for Chrome.
● odoo.tests.common.HttpCase
Extending one of these classes would result in code like this:
# -*- coding: utf-8 -*-
from odoo.tests import common
class TestBook(common.TransactionCase):
All test files must start with test_
E.g., test_book.py
All test methods must start with test_
E.g.,
from odoo.tests import common
class TestBook(common.TransactionCase):
def test_string_concatenation_and_length(self):
Writing A Test- Example
from odoo.tests import common
class TestBook(common.TransactionCase):
def test_string_concatenation_and_length(self):
book = self.env['book.book'].create({'name': 'War
and Peace',
'author ': 'Leo Tolstoy'})
"""Book name checking"""
self.assertEqual(
# Actual results from function call...
book.name,
# Expected results from the function call...
"War and Peace")
"""Book name length checking"""
self.assertEqual(
# Actual results from function call...
len(book.name),
# Expected results from the function call...
13)
In the example above, I created a test method called test_string_concatenation_and_length() that checks the string concatenation and the length of the concatenated string.
Running Tests
Odoo test cases can be executed 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]