Monkey Patching
Monkey Patching is the dynamic replacement of attributes at runtime.
E.g.:
1from SomeOtherProduct.SomeModule import SomeClass
2
3def speak(self):
4 return "ook ook eee eee eee!"
5
6SomeClass.speak = speak
The code for the method speak
of the class SomeClass
has been replaces at runtime.
Consider a class that has a method get_data
. This method does an external lookup, and various methods in the class call it. However, in a unit test, you don’t want to depend on the external data source - so you dynamically replace the get_data
method with a stub that returns some fixed data.
Use caution when monkeypatching:
- If anything else besides your test logic calls
get_data
as well, it will also call your monkey-patched replacement rather than the original. - If some variable or attributes exists that also points to the
get_data
function by the time it gets replaces, this alias will not change its meaning and will continue to point to the originalget_data
.
References
#runtime #python #coding #monkey #computer_science #interpreted #class #on_the_fly #programming #method #patching