Incorporating automated tests is an effective way to enhance the
reliability and maintainability of your module, especially when
working on large-scale applications. These tests make your module
more robust and responsive to changes. Since Odoo frequently
releases updated versions of its platform, automated testing plays a
key role in catching potential regressions introduced during
upgrades.
Odoo provides built-in support for several types of automated
testing, including:
- Python Unit Tests – Ideal for validating the business
logic within models.
- JavaScript Unit Tests – Useful for testing frontend logic
in isolation.
- Tours – Simulated walkthroughs that ensure seamless
interaction between frontend (JavaScript) and backend (Python)
components, mimicking real user behavior.
Adding Python Test Cases
your_module
|
|---- …
|---- python_test_cases
|----- __init__.py
|------ test_purchase.py
|----- test_sale
When composing examinations
When creating tests for your module, you should follow certain
conventions to ensure that Odoo's testing framework can detect and
execute them correctly:
- A dedicated tests subpackage must be created inside your module.
- All test files within this subpackage must be prefixed with
test_.
- The tests/__init__.py file must import all test modules to make
them discoverable.
- Each test class should inherit from an appropriate Odoo test
case class, such as TransactionCase or HttpCase.
- Each test method must begin with the prefix test_.
Here is an example of what your tests/__init__.py might look like:
from . import test_sale
from . import test_purchase
Extend an Odoo test case class
Odoo provides a set of built-in test case classes designed to support
different testing scenarios. When writing tests, you must inherit
from one of these classes based on your specific requirements:
-
odoo.tests.common.TransactionCase
This is the most commonly used test class. Each test method
runs inside a sub-transaction managed with a savepoint. All
test methods share the same main transaction, but the
transaction is never committed—it is simply closed after the
tests finish.
-
odoo.tests.common.SingleTransactionCase
In this class, the entire test suite runs within a single
transaction. The transaction starts with the first test
method and is rolled back only after the last test method
finishes.
-
odoo.tests.common.HttpCase
This test case is used for HTTP testing. It includes helpers
like url_open and supports Chrome Headless to simulate
frontend interactions.
-
odoo.tests.common.SavepointCase
A variant of TransactionCase, where each test method runs
independently in its own savepoint, offering better
isolation between test cases.
Here’s how your code would look after extending one of Odoo’s test
case classes—specifically for an HTTP test using Chrome Headless and
url_open:
# -*- coding: utf-8 -*-
from odoo.tests import TransactionCase
class MyTest(TransactionCase):
All test files in your module should begin with the prefix test_ to
ensure Odoo’s test runner can automatically detect and execute them.
Similarly, each test method within those files must also start with
test_. This naming convention is essential for the test framework to
properly recognize and execute the test cases during automated
testing.
# -*- 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 provided, we created a test method named
test_string_concatenation_and_length(). This method validates both
the string concatenation and ensures that the resulting string's
length is as expected.
Running Tests in Odoo
Odoo offers multiple methods to execute test cases:
-
Using --test-enable when starting the server
Launching the Odoo server with the --test-enable flag will
automatically execute tests during the installation or
update of a module.
-
Running a specific test file
You can target a particular test file using the --test-file
option.
-
Running tests with tags using --test-tags.
This option provides more granular control over which tests
to run