Skip to content
Advertisement

Integration test for decorated function

I have a problem in writing integration test for some function that calls other function which is decorated. Suppose the following definitions:

JavaScript

I need to write test to ensure that any exception raised inside problematic_func will not be propagated to func_under_test. For this reason, I used mocking like below:

JavaScript

Problem is that I can’t pass this test. Patching problematic_func caused removal of decorator applied to that function and exceptions are not catched. For applying decorator manually, I tried:

JavaScript

This also doesn’t result with successful test passing. Exception is still raised when calling func_under_test in my test case. How should I test that any exception raised inside problematic_func doesn’t cause failure in func_under_test?

Note: Please don’t suggest writing test for catch_every_error decorator. I am trying to fulfill integration test for func_under_test.

Advertisement

Answer

Working version of the test above is like below:

JavaScript

Previously, I tried (with no successful result):

JavaScript

For some reason (I don’t know exactly), importing function with from statement and decorating manually didn’t work. Although importing whole module (import myproj) and manually decorating with resetting attribute (myproj.problematic_func =) worked.

Advertisement