mocking

Accessing Calls to Mocked Class functions

Accessing Calls to Mocked Class functions Question: I have written a custom class to mock a general API client in a codebase so that I can centrally and easily mock all of the class methods for unit testing. This is working great so far, however I am looking for a way to track individual calls …

Total answers: 1

Is it possible to mock SimpleUploadedFile?

Is it possible to mock SimpleUploadedFile? Question: I have a function like this: def my_func(): […] with open(full_path, "rb") as file: image.image = SimpleUploadedFile( name=image.external_url, content=file.read(), content_type="image/jpeg", ) image.save() print(f"{file_name} – Saved!") I would like mock the "SimpleUploadedFile" for a test (calling print…). Is it possible? A little bit of context: the function download and …

Total answers: 1

Mocking an external API call in a Django view test

Mocking an external API call in a Django view test Question: I have written a view in Django that receives text from the user and calls an external API for that text to be translated. View function: def translate_view(request): if request.method == ‘POST’: form = InputForm(request.POST) if form.is_valid(): source_lang = form.cleaned_data["source_lang"] target_lang = form.cleaned_data["target_lang"] source_text …

Total answers: 1

Mocking datetime.now using pytest-mock

Mocking datetime.now using pytest-mock Question: I have a function func_to_mock in module module_to_mock.py. My unit test is located in test_func_to_mock.py I am trying to mock datetime.datetime.now, however, I am struggling. I get the error TypeError: cannot set ‘now’ attribute of immutable type ‘datetime.datetime’. What am I missing here? module_to_mock.py: # module_to_mock.py import datetime def func_to_mock() …

Total answers: 1

Test for instantiation of a mocked class in Python?

Test for instantiation of a mocked class in Python? Question: I want to make my tests "real" unittests in a strict meaning. There shouldn’t be any dependencies. All dependencies should be mocked. The class Bar in module bar need to be tested. But in some situations it will have a member of type Foo from …

Total answers: 1

How to mock stdin when using fileinput module?

How to mock stdin when using fileinput module? Question: I have a program that uses the Python fileinput module, and I am trying to write unittests for the main() function. They work find when using an actual file, but raise OSError: reading from stdin while output is captured when I try to pass data via …

Total answers: 2

How do i mock an external libraries' classes/functions such as yaml.load() or Box() in python

How do i mock an external libraries' classes/functions such as yaml.load() or Box() in python Question: How would i go about testing the following class and its functions? import yaml from box import Box from yaml import SafeLoader class Config: def set_config_path(self): self.path = r"./config/datasets.yaml" return self.path def create_config(self): with open(r"./config/datasets.yaml") as f: self.config = …

Total answers: 1

patch a class to instance attributes to access its methods

Patch a class – that is an attribute of an other class – to access its methods Question: I am trying to mock an attribute of a class Bar upon creation of its instance object test_obj. The attribute is an instance of another class called Foo. Afterwards, I want to test a bar method from …

Total answers: 1

Python unit tests – mocking imported class methods

Python unit tests – mocking imported class methods Question: I would like to mock some imported class methods and module functions for my unit tests. I tried several ways to define the mocked values but I don’t understand why they are not taken into account. I wrote some tests following the advices in Python Mocking …

Total answers: 1