Odoo Test Helper
Odoo Test Helper is a Python library providing a toolbox for writing Odoo tests, primarily by facilitating the loading of fake models. This is particularly useful for testing abstract Odoo modules where real record interaction is needed without actual database persistence. Maintained by the Odoo Community Association (OCA), the library is currently at version 2.1.3 and has an infrequent, as-needed release cadence.
Common errors
-
odoo.exceptions.AccessError: You are not allowed to access this document.
cause When using fake models or testing with a limited test user, the Odoo access rights or record rules might prevent interaction with the model, even if it's a test-only model.fixEnsure the test environment's user (e.g., `self.env.user`) has the necessary access rights for your fake model, or explicitly create and apply record rules within your test setup. -
odoo.exceptions.MissingError: Record does not exist or has been deleted.
cause This typically occurs if a fake model is used without being properly registered with the Odoo environment, or if the `FakeModelLoader`'s `update_registry` was not called or was called incorrectly for the model. It can also happen if `restore_registry()` is called too early.fixVerify that your fake models are correctly passed to `cls.loader.update_registry((YourFakeModel,))` in `setUpClass` and that `restore_registry()` is in `tearDownClass`. -
KeyError: 'module_to_models' or issues related to Odoo's registry in general.
cause This error often indicates that the Odoo registry was not properly backed up or restored, or that fake models were imported in an incorrect order relative to the registry operations, leading to an inconsistent state.fixStrictly follow the `FakeModelLoader` pattern: `backup_registry()` first, then import fake models and `update_registry()`, and finally `restore_registry()` in `tearDownClass`.
Warnings
- breaking This library is explicitly not needed for Odoo 19+ versions. Odoo 19 and newer provide native functionality to achieve the same testing goals, rendering `odoo-test-helper` obsolete for these versions.
- gotcha The PyPI development status for `odoo-test-helper` is classified as '2 - Pre-Alpha'. This indicates that the project is still under active development, may have an unstable API, and could introduce breaking changes or significant modifications without a full deprecation cycle.
- gotcha Incorrect import order of fake Odoo models (e.g., `ResPartner` in the example) can lead to Odoo registry corruption or unexpected behavior. Fake models must be imported *after* `cls.loader.backup_registry()` has been called in `setUpClass`.
Install
-
pip install odoo-test-helper
Imports
- FakeModelLoader
from odoo_test_helper import FakeModelLoader
- Fake Odoo Models (e.g., ResPartner)
from odoo.tests import SavepointCase from odoo_test_helper import FakeModelLoader from .models import ResPartner # Wrong: Import before backup_registry()
# In a test method, AFTER backup_registry() from .models import ResPartner
Quickstart
from odoo.tests import SavepointCase
from odoo_test_helper import FakeModelLoader
# Define your fake models in a file like 'models.py' in the same directory
# Example models.py content:
# from odoo import models, fields
# class ResPartner(models.Model):
# _name = 'res.partner'
# _description = 'Fake Partner'
# name = fields.Char()
class TestFakeModel(SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.loader = FakeModelLoader(cls.env, cls.__module__)
cls.loader.backup_registry()
# IMPORTANT: Import your fake models AFTER backup_registry()
from .models import ResPartner # Assuming 'models.py' contains ResPartner
cls.loader.update_registry((ResPartner,))
# Now you can use your fake model
cls.test_partner = cls.env['res.partner'].create({'name': 'Test Partner'})
@classmethod
def tearDownClass(cls):
cls.loader.restore_registry()
super().tearDownClass()
def test_fake_partner_creation(self):
self.assertEqual(self.test_partner.name, 'Test Partner')