Monkey patch - odoo-ps/psbe-process GitHub Wiki
Standard modules in odoo almost always contain a test folder. This folder is composed of several tests that ensure the good behaviour of the module (see the part about tests). However, it may happen that a custom development, by changing the behaviour of the modules, breaks some of those tests. You should then get a nice red branch in odoo.sh with a message telling you that a test has failed. Tests are written for a reason, so it's always best to fix the faulting ones. And if you're not sure of what to do, do not hesitate to ask your coach or an elder first. However, it may sometimes happen that the best solution at a given moment is to bypass the test. It's called monkey patching.
Let's say you get an error in file addons/sale_stock/tests/test_sale_stock. The test test_01_sale_stock_order failed and you need to patch it. Here is what to do:
- First, create a folder tests in the root of your module.
- Inside this folder, create a python file called monkey.py. (do not forget to set the _init_.py files in the folder and at the root)
- In your file you will need to import two things. The module unittest and the class containing the faulty test. Ex:
Side note : Sometimes, you have to import several classes and those classes may have the same name. To avoid confusion, you can give an alias to your class:
import unittest from odoo.addons.sale_stock.tests.test_sale_stock import TestSaleStock
from odoo.addons.sale_stock.tests.test_sale_stock import TestSaleStock as TestSaleStock1
- Once this is done, create a dummy test and replace the old method :
It is a good practice to add the tag on top of the method, to tell future developpers that this is a monkey patch. It is also a good practice to specify one dummy method per faulting test, even though a single generic dummy method could be linked to all tests.
@unittest.skip('Need to adapt') def test_01_sale_stock_order(self): pass TestSaleStock.test_01_sale_stock_order = test_01_sale_stock_order
It may sometimes happen that all the tests in a class fail at the same time. In this case, it might be too long or error prone to patch every single test one after the other. The solution is to patch the whole class at once.
To do so, you first need to import not the class but the file containing the class. Then you create a dummy class and replace the old one. Just as you would do with methods. Ex:
from odoo.addons.website_sale.tests import test_customize
@unittest.skip('Need to adapt')
class TestUi():
def void_func(self):
pass
test_customize.TestUi = TestUi
Once all of this is done, your odoo.sh branch should become green.