monkeypatching

Avoiding running top-level module code in unit test

Avoiding running top-level module code in unit test Question: I am attempting to unit test some Python 3 code that imports a module. Unfortunately, the way the module is written, simply importing it has unpleasant side effects, which are not important for the tests. I’m trying to use unitest.mock.patch to get around it, but not …

Total answers: 3

Pytest: mocking / monkey patching builtin input() and print() functions in python

Pytest: mocking / monkey patching builtin input() and print() functions in python Question: How can I monkey patch the builtin input and print functions using Pytest so as to capture the output of someone else’s code and test it with pytest before refactoring? For example, I have acquired some code similar to this: class QueryProcessor: …

Total answers: 2

Getting monkeypatching in pytest to work

Getting monkeypatching in pytest to work Question: I’m trying to develop a test using pytest for a class method that randomly selects a string from a list of strings. It looks essentially like the givemeanumber method below: import os.path from random import choice class Bob(object): def getssh(): return os.path.join(os.path.expanduser(“~admin”), ‘.ssh’) def givemeanumber(): nos = [1, …

Total answers: 2

What is the difference between mocking and monkey patching?

What is the difference between mocking and monkey patching? Question: I work with python and I’m a bit new to testing. I often see tests replacing an external dependency with a local method like so: import some_module def get_file_data(): return "here is the pretend file data" some_module.get_file_data = get_file_data # proceed to test I see …

Total answers: 1

Why use monkeypatch.setenv instead of os.putenv

Why use monkeypatch.setenv instead of os.putenv Question: pytest’s monkeypatch module provides a setenv function which will Set environment variable name to value. Why does monkeypatch provide this? It sounds the same as os.putenv? It provides prepend argument as a convenience, but that seems like a weak reason to have a new function for setting an …

Total answers: 1

Advantages of Using MethodType in Python

Advantages of Using MethodType in Python Question: What are the advantages of using MethodType from the types module? You can use it to add methods to an object. But we can do that easily without it: def func(): print 1 class A: pass obj = A() obj.func = func It works even if we delete …

Total answers: 2

Python mock.patch.object with functool.partial bound arguments possible?

Python mock.patch.object with functool.partial bound arguments possible? Question: How to solve this? Patch a objects method with another signature (eg. an additional argument. I’ve tried to bound the optional argument, but this does not seem to work. I can not use plain monkey patching here, since the the patched class is called under in a …

Total answers: 3

Monkey patching a @property

Monkey patching a @property Question: Is it at all possible to monkey patch the value of a @property of an instance of a class that I do not control? class Foo: @property def bar(self): return here().be[‘dragons’] f = Foo() print(f.bar) # baz f.bar = 42 # MAGIC! print(f.bar) # 42 Obviously the above would produce …

Total answers: 7

How to multiply functions in python?

How to multiply functions in python? Question: def sub3(n): return n – 3 def square(n): return n * n It’s easy to compose functions in Python: >>> my_list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> [square(sub3(n)) for n in my_list] [9, 4, 1, 0, 1, 4, 9, 16, 25, 36] Unfortunately, …

Total answers: 4