Odoo Test Helper
Project description
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 TestMixin(SavepointCase):
@classmethod
def setUpClass(cls):
super(TestMixin, 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(TestMixin, 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 TestMixin(SavepointCase):
@classmethod
def setUpClass(cls):
super(TestMixin, 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(TestMixin, 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
Sébastien BEAU <sebastien.beau@akretion.com>
Laurent Mignon <laurent.mignon@acsone.eu>
Simone Orsi <simone.orsi@camptocamp.com>
History
This module is inspired of the following mixin code that can be found in OCA and shopinvader repository
Mixin in Shopinvader: https://github.com/shopinvader/odoo-shopinvader/blob/b81b921ea52c911e5b33afac88adb8f9a1c02626/base_url/tests/models_mixin.py
Intial Authors are
Laurent Mignon <laurent.mignon@acsone.eu>
Simone Orsi <simone.orsi@camptocamp.com>
Refactor/extraction have been done by
Sébastien BEAU <sebastien.beau@akretion.com>
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
Changes
2.0.3
[FIX] restore_registry in Odoo 16
This new attribute is the source of truth for the base classes and in setup_models (called further down in the modified code in this PR), the model’s base classes are reset from it:
The test failure fixed by this PR can be inspected in https://app.travis-ci.com/github/OCA/odoo-test-helper/builds/258453331
2.0.2
Fix mock import for v15
2.0.1
Fix support for Odoo 15.0
2.0.0
Move to OCA
Re-license to LGPL
1.1.0
Refactoring (misc imp/fix)
1.0.0
Initial release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file odoo-test-helper-2.0.3.tar.gz
.
File metadata
- Download URL: odoo-test-helper-2.0.3.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8886560124c683d6494a7dc074a1e0d1d36ddc8738f4a33c3a5f037547905167 |
|
MD5 | e8ae057c66a0243592348fb7f968a969 |
|
BLAKE2b-256 | f0c6124fb3a26c5b035a37c10dbb2d9ad0799eecdcd5a75a66aa0da6091a0b78 |
Provenance
File details
Details for the file odoo_test_helper-2.0.3-py2.py3-none-any.whl
.
File metadata
- Download URL: odoo_test_helper-2.0.3-py2.py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b0f09e28103ea5777d08ec73babe80e3022d560f23f87cda5412aca739cfa17 |
|
MD5 | 0ab50a13429a913f6aabc79636f97cec |
|
BLAKE2b-256 | f9367869bf8a5ea59ccb6d77e35d6c41c5fe3d2c27d4642890b6678a69a33e5d |