Odoo Test Helper

2.1.3 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up and use a fake Odoo model within a test case. It shows the correct placement for importing fake models (after `backup_registry`) and how to clean up the registry afterwards.

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')

view raw JSON →