Guest User

Untitled

a guest
Aug 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. Patching: Replacing a method call with another
  2. # module_A.py
  3. def original_func(arg_a,arg_b):
  4. # ...
  5.  
  6. # module_B.py
  7. import module_A
  8.  
  9. def func_under_test():
  10. # ...
  11. module_A.original_func(a,b)
  12. # Some code that depends on the behavior of the patched function
  13. # ...
  14.  
  15. # my test code
  16. def alternative_func(arg_a,arg_b):
  17. # do something essential for the test
  18.  
  19. def the_test():
  20. # patch the original_func with the alternative_func here
  21. func_under_test()
  22. # assertions
  23.  
  24. module_A.original_func = alternative_func
  25.  
  26. import module_a
  27.  
  28. def the_test():
  29. orig_func = module_a.original_func
  30. module_a.original_func = alternative_func
  31.  
  32. # do testing stuff
  33.  
  34. # then restore original func for other tests
  35. module_a.original_func = orig_func
  36.  
  37. original_func = alternative_func
Add Comment
Please, Sign In to add comment