Skip to main content
License: LGPL-3 https://badge.fury.io/py/odoo-test-helper.svg

odoo-test-helper is toolbox for writing odoo test

Loading Fake models

Sometime you build an abstract module that can be use by many modules. In such case, if you want to test it with real records you need to register real models.

One solution is to create a module_test module with a little implementation that use your abstract model.

One other solution is define test only models and load them in tests. This lib makes this possible and easy.

Example

There is an example of test here:

This example load the class ResPartner from the file:

Real implementation case can be found in the following module

How to import

Be carefull importing fake class must be done in the right way. Importing a file will automatically add all the class in the “module_to_models” variable. The import must be done after the backup !

Wrong way

from odoo.tests import SavepointCase

from odoo_test_helper import FakeModelLoader

# The fake class is imported here !! It's wrong
# And be carefull even if you only import ResPartner
# all class in the file models will be proceded by odoo
# so no **direct import** of a file that contain fake model
from .models import ResPartner





class FakeModel(SavepointCase):
    @classmethod
    def setUpClass(cls):
        super(FakeModel, cls).setUpClass()
        cls.loader = FakeModelLoader(cls.env, cls.__module__)
        cls.loader.backup_registry()

        cls.loader.update_registry((ResPartner,))

    @classmethod
    def tearDownClass(cls):
        cls.loader.restore_registry()
        super(FakeModel, cls).tearDownClass()

    def test_create(self):
        partner = self.env["res.partner"].create({"name": "BAR", "test_char": "youhou"})
        self.assertEqual(partner.name, "FOO-BAR")
        self.assertEqual(partner.test_char, "youhou")

Right Way

from odoo.tests import SavepointCase

from odoo_test_helper import FakeModelLoader


class FakeModel(SavepointCase):
    @classmethod
    def setUpClass(cls):
        super(FakeModel, cls).setUpClass()
        cls.loader = FakeModelLoader(cls.env, cls.__module__)
        cls.loader.backup_registry()

        # The fake class is imported here !! After the backup_registry
        from .models import ResPartner

        cls.loader.update_registry((ResPartner,))

    @classmethod
    def tearDownClass(cls):
        cls.loader.restore_registry()
        super(FakeModel, cls).tearDownClass()

    def test_create(self):
        partner = self.env["res.partner"].create({"name": "BAR", "test_char": "youhou"})
        self.assertEqual(partner.name, "FOO-BAR")
        self.assertEqual(partner.test_char, "youhou")

Contributor

History

This module is inspired of the following mixin code that can be found in OCA and shopinvader repository

Intial Authors are

Refactor/extraction have been done by

This refactor try to load all class correctly like Odoo does with the exact same syntax

Note this refactor/extraction have been done to fix the test of the following issue

https://github.com/shopinvader/odoo-shopinvader/pull/607

Release files for odoo-test-helper 2.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for odoo-test-helper 2.1.3
File Size Uploaded
odoo_test_helper-2.1.3.tar.gz 11.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for odoo-test-helper 2.1.3
File Interpreter ABI Platform
odoo_test_helper-2.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 25.8 kB

Release files / odoo_test_helper-2.1.3.tar.gz

Download URL odoo_test_helper-2.1.3.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
58ab156fb4d4f10dce680a0043e4d0c86d60bbfaaaa53f425e30564d2f15f043
BLAKE2b-256 checksum
How to use checksums
f01b7ca965615b44711cefe2e6d6311f35bb27ee8b60d4bccad1ef40d6863683
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / odoo_test_helper-2.1.3-py3-none-any.whl

Download URL odoo_test_helper-2.1.3-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8c40c8a01c7fcb59d8fbfcdbee83412bdaac3e24700f70daefa43ddd4b4146a0
BLAKE2b-256 checksum
How to use checksums
f3707102b14aac1afed2ede8f90374bf747b8525a80b629d321395b94e24987d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

2.1.3 This release

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1.0

1 release file

1.0.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page