【问题标题】:Monkey patch post install test猴子补丁安装后测试
【发布时间】:2020-02-14 09:27:57
【问题描述】:

我的安装后测试有问题,我想像这样对它进行 Monkey Patch。

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

这是我打印的输出。

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

我的猴子补丁中的方法永远不会被调用并且测试失败。

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 

【问题讨论】:

    标签: odoo odoo-12


    【解决方案1】:

    你需要修补对test_clear_caches的绝对引用:

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

    试试下面的代码:

    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
    

    【讨论】:

      猜你喜欢
      • 2016-09-01
      • 2012-09-16
      • 2012-12-18
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 2010-09-29
      • 2021-04-04
      相关资源
      最近更新 更多