Skip to main content

Odoo Test Helper

Project description

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

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

Changes

2.1.0

  • [IMP] Allow to ignore Odoo core modules to avoid warning

2.0.5

  • .gitignore added

2.0.4

  • [FIX] AttributeError in Odoo 15+, regression introduced in 2.0.3

2.0.3

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

odoo-test-helper-2.1.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

odoo_test_helper-2.1.0-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file odoo-test-helper-2.1.0.tar.gz.

File metadata

  • Download URL: odoo-test-helper-2.1.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for odoo-test-helper-2.1.0.tar.gz
Algorithm Hash digest
SHA256 2d8fe6a3c95e46a69b1111df159636f7031384987259b4247b602e1911762e90
MD5 38f3a40af940ae249ba330b2940bdf94
BLAKE2b-256 64e78b6d1fd6329145e16704e27bd6aa16f52b22e5c8a4eaec4a0387ea10e6af

See more details on using hashes here.

Provenance

File details

Details for the file odoo_test_helper-2.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for odoo_test_helper-2.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 07fdbbb12d455d2bf860c73493fcbcd5f60c8204a6a8dfd9238b25f0273c2036
MD5 327afd0d53e3301497dd4158641e4890
BLAKE2b-256 f5b5fe4115cfce4f8ca9bbdbacc4015c441c89b4fecfac47e97292d13e816158

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page