How to write python test cases in Odoo 18

CelineGeorge1 14 views 13 slides Oct 24, 2025
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

Python test cases in Odoo 18 ensure code stability and validate business logic using the `unittest` framework, typically inheriting from `common.TransactionCase`. Tests are organized in a `tests` directory, utilize methods starting with `test_`, and are run via the command line using the `--test-ena...


Slide Content

How to write python test cases in Odoo 18 Enterprise

Enterprise Introduction Writing Python test cases in Odoo 18 ensures that our custom modules functions as intended and maintain stability during development. Odoo leverages Python unittest framework, providing additional utilities to facilitate effective testing. In Odoo 18, writing test cases in Python is similar to previous versions like Odoo 16 and 17. Odoo uses Python’s unittest framework, and the test classes inherit from TransactionCase, SavepointCase, or HttpCase, depending on the type of test.

Enterprise Essential requirements for Odoo test framework Python Environment Odoo Test Classes Directory and File Naming Ensure __init__.py files exist in both your_module/ and tests/. Test Class Requirements Inherit from TransactionCase , SavepointCase , or HttpCase Be named like TestXyz Methods starting with test_ will be auto-detected Running Tests

Enterprise How to write a test? For writing test cases, we have to create a subfolder ‘tests’ inside our module and import it inside ‘ module/__init__ ’. Then the name of the test files inside the test folder should be started with ‘test_ ‘ and also it should be imported inside ‘ tests/__init__ ’.

Enterprise

Enterprise The __init__.py contains: Test will be run only if it is imported in ‘ your_module/tests/__init__.py ’.

Enterprise Example: from odoo.tests.common import TransactionCase class TestStudentRecord(TransactionCase): @classmethod def setUpClass(cls): super(TestStudentRecord, cls).setUpClass() # Create a common record for all test methods in the class cls.student = cls.env['student.record'].create({ 'name': 'John Doe', 'gender': 'male', 'birthdate': '2001-03-29' }) def test_field_values(self): # Test the values of fields self.assertEqual(self.student.name, 'John Doe', "Name field value is incorrect") self.assertEqual(self.student.gender, 'male', "Gender field value is incorrect")

Enterprise def test_function_output(self): # Test the output of a function result = self.student.calculate_age() self.assertEqual(result, expected_value, "Function output is incorrect") def test_other_function(self): # Another test method # ... @classmethod def tearDownClass(cls): # Clean up any resources if needed super(TestStudentRecord, cls).tearDownClass()

Enterprise setUp() Method in Odoo Run before every test_... method in the class. Use to create fresh test data or setup test-specific context. Automatically rolls back changes after each test (when using SavepointCase or TransactionCase).

Enterprise Most Common assert Functions in Odoo Tests assertEqual(a, b) - Check if a == b assertNotEqual(a, b) - Check if a != b assertTrue(x) - Check if x is True assertFalse(x) - Check if x is False assertIs(a, b) - Check if a is b (same object) assertIsNot(a, b) - Check if a is not b

Enterprise assertIsNone(x) - Check if x is None assertIsNotNone(x) - Check if x is not None assertIn(a, b) - Check if a in b assertNotIn(a, b) - Check if a not in b assertGreater(a, b) - Check if a > b assertLess(a, b) - Check if a < b assertRaises(ErrorType) - Check that an error is raised

Enterprise How to run tests in Odoo? Launch Odoo with the "--test-enable" parameter. Use the "-d [my_database]" flag to specify the database you want to test. Utilize the "-i [modules_to_install]" flag to indicate the modules you want to test. To execute a test on your local Odoo installation, use the following command as an example: ./odoo-bin -i {module_to_install} --test-enable -c {conf path} You can use these commands in the terminal if you want to run the tests in a particular module. ./odoo-bin -c /path/to/odoo.conf -d your_database_name -u your_module_name --test-enable

For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com