Search code examples
odooodoo-12

Monkey patch post install test


I have a problem with the post-install test and I want to Monkey Patch it like this.

import addons.product.tests.test_product_attribute_value_config
from addons.product.tests.test_product_attribute_value_config import TestProductAttributeValueSetup
from odoo.tests import tagged

print(TestProductAttributeValueSetup)
print(addons.product.tests.test_product_attribute_value_config.TestProductAttributeValueConfig.test_clear_caches)
print("START")
@tagged('post_install', '-at_install')
class TestProductAttributeValueConfig(TestProductAttributeValueSetup):
    print("IN Class")
    def test_clear_caches(self):
        print("IN method")
        return

print("OUT")
addons.product.tests.test_product_attribute_value_config.TestProductAttributeValueConfig.test_clear_caches\
    = TestProductAttributeValueConfig.test_clear_caches
print("STOP")

Here is the OUTPUT of my prints.

<class 'addons.product.tests.test_product_attribute_value_config.TestProductAttributeValueSetup'>
<function TestProductAttributeValueConfig.test_clear_caches at 0x7f680f0c6d90>
START
IN Class
OUT
STOP

The method in my monkey patch is never called and the test fails.

2020-02-14 09:20:23,851 840 ERROR test_60 odoo.addons.product.tests.test_product_attribute_value_config: ERROR: test_clear_caches (odoo.addons.product.tests.test_product_attribute_value_config.TestProductAttributeValueConfig) 
2020-02-14 09:20:23,851 840 ERROR test_60 odoo.addons.product.tests.test_product_attribute_value_config: ` The goal of this test is to make sure the cache is invalidated when it should be. 
2020-02-14 09:20:23,851 840 ERROR test_60 odoo.addons.product.tests.test_product_attribute_value_config: Traceback (most recent call last): 
2020-02-14 09:20:23,851 840 ERROR test_60 odoo.addons.product.tests.test_product_attribute_value_config: `   File "/home/antonp/workspace/Odoo12/addons/product/tests/test_product_attribute_value_config.py", line 494, in test_clear_caches 
2020-02-14 09:20:23,851 840 ERROR test_60 odoo.addons.product.tests.test_product_attribute_value_config: `     self.env['product.product'].browse(variant_id).unlink() 
2020-02-14 09:20:23,851 840 ERROR test_60 odoo.addons.product.tests.test_product_attribute_value_config: `   File "/home/antonp/workspace/Odoo12/addons/product/tests/test_variants.py", line 781, in unlink 
2020-02-14 09:20:23,851 840 ERROR test_60 odoo.addons.product.tests.test_product_attribute_value_config: `     raise Exception('just') 
2020-02-14 09:20:23,851 840 ERROR test_60 odoo.addons.product.tests.test_product_attribute_value_config: ` Exception: just 

Solution

  • You need to patch the absolute reference to test_clear_caches:

    odoo.addons.product.tests.test_product_attribute_value_config.TestProductAttributeValueConfig.test_clear_caches
    

    Try with the following code:

    import odoo
    
    
    @odoo.tests.tagged('post_install', '-at_install')
    class TestProductAttributeValueConfig(
        odoo.addons.product.tests.test_product_attribute_value_config.TestProductAttributeValueConfig):
    
        def test_clear_caches(self):
            print("IN method")
    
    
    odoo.addons.product.tests.test_product_attribute_value_config.TestProductAttributeValueConfig.test_clear_caches = \
        TestProductAttributeValueConfig.test_clear_caches