Consider a function: and it’s mock function: How do I mock some_function() function using monkeypath.setattr()? Something similar to: monkeypatch.setattr(<class name>, “some_function”, dummy_function) Not sure what <class name> here should be. Answer obj should be the current mod…
Tag: mocking
How do I mock google cloud storage functions for my unittest ? (Python)
The following is my function that I want to unit test: Since I want to unit test in isolation without any dependencies or internet, I would like to mock the google cloud client object and its functionality of list_blobs. Is that the correct way of going about unittesting this function? If so, how do I mock th…
Python mock – mocking class method that modifies class attributes
I currently have the following basic Python class that I want to test: As you can see, the step() method contains an expensive API call so I want to mock it with another function that avoids the expensive API call but still increments self.steps. I found that this is possible by doing this (as seen from here)…
What does the instance argument in Python’s create_autospec do?
I’m playing around with mock autospecs in Python. Here’s a basic test case where I’m autospecing the Django User class using create_autospec. The test passes when I set both instance=True and instance=False, so what exactly does this parameter do? What’s its purpose? I’ve seen mu…
Unable to mock os name with Python on Windows
I have the following method: I’m trying to test it like this: But then the following error is raised: What is happening exactly? Answer What is happening here is that pytest internally uses a pathlib.Path object, which upon initialization asks for os.name to define which Path implementation to use. Ther…
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: 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…
Pytest: Mock multiple calls of same method with different side_effect
I have a unit test like so below: As part of the foo.run() code run, get_request is called multiple times. I want to have a different side_effect function for each call of get_request method, in this case it is side_effect_func1, side_effect_func2, side_effect_func3. But what I’m noticing is that only m…
mocking/patching the value of a computed attribute from a classmethod
I am attempting to write a test for a function which calls an object’s classmethod — this classmethod goes on to return a new instance of that class. There are plenty of examples of patching class attributes both here on stackoverflow and elsewhere but I am having difficulty understanding how to p…
Pytest + mock: patch does not work without with clause
I’m testing complex logic that require joining of central fact table with 10-20 smaller dimensional tables. I want to mock that 10-20 smaller tables. How to patch methods return values in a for loop? See code below. tables.py: test_logic.py P.S. alternatively I can try to mock the BaseClass.load, but th…
Changing monkeypatch setattr multiple times
I am trying to test code that depends on a third party and would like to use monkeypatch to replicate what I expect a request will return. Here is a minimal example of the code that I have. For my tests, I have something like the following: How can I monkeypatch each of the calls to requests.get using monkeyp…