Questions tagged [python-unittest.mock]

mock object library (part of the Python Standard Library)

unittest.mock allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.

It provides a core Mock class removing the need to create a host of stubs throughout your test suite.

330 questions
5
votes
0 answers

Unittesting Mock/Overwriting socket's sendto

[EDIT: The problem only exists with Python 2.7. With 3.7 it works as expected. Any idea what the difference could be and if it's possible to make it work also with 2.7, which I have to use for some reasons] I want to overwrite socket's method sendto…
5
votes
3 answers

How to mock a protected/private method in a tested method?

I have a Python Clas with 2 methods. The first, _getTemperature_() is protected and the second one is a public method. I have to write a unitTest but I have no clue, how to mock the protected method? I just found tutorials to mock a public method…
5
votes
2 answers

Why does unittest.mock fail when the production class constructor takes extra arguments?

I have run into a problem which I think might be a bug with the libraries I am using. However, I am fairly new to python, unittest, and unittest.mock libraries so this may just be a hole in my understanding. While adding tests to some production…
stk_sfr
  • 619
  • 5
  • 18
4
votes
1 answer

How to mock a function which makes a mutation on an argument that is necessary for the caller fuction logic

I want to be able to mock a function that mutates an argument, and that it's mutation is relevant in order for the code to continue executing correctly. Consider the following code: def mutate_my_dict(mutable_dict): if os.path.exists("a.txt"): …
4
votes
1 answer

Mock external API POST call in view from test view Python

I have an external API POST call that is being made from within my views.py as such: class MyView(APIView): def post(self, request): my_headers = { "Content-Type": "application/json" } response =…
4
votes
1 answer

Mocking a class method and passing self argument to the Mock's side effect

I am trying to patch a single method in an existing class within a unit test. The class to be patched is: class Example: def __init__(self: "Example", id: int) -> None: self.id : int = id self._loaded : bool = False …
MT0
  • 143,790
  • 11
  • 59
  • 117
4
votes
0 answers

Using mock.patch to disable a function behavior

I have a function adding subscription for my users. At the end of this function, an instruction check the subscriptions and do some stuff with patron if necessary. For my testing, I need to skip this last instruction ; so I tried to use…
4
votes
1 answer

PicklingError when using mock to check function is called

I'm having trouble testing that a method is called using mock -- as a simple example, let's say that the method is os.getcwd. I want to test that my own function, pickle_wdir, is calling os.getcwd as intended. However, the function I am testing…
Dan McCabe
  • 188
  • 1
  • 3
  • 11
4
votes
1 answer

Mocking a file containing JSON data with mock.patch and mock_open

I'm trying to test a method that requires the use of json.load in Python 3.6. And after several attempts, I tried running the test "normally" (with the usual unittest.main() from the CLI), and in the iPython REPL. Having the following function…
Wakaru44
  • 423
  • 4
  • 14
4
votes
5 answers

mocking snowflake connection

I have a SnowflakeApi class in python which just works as a wrapper on top of the SnowflakeConnection class. My SnowflakeApi is import logging import os from snowflake.connector import connect class SnowflakeApi(object): """ Wrapper to…
4
votes
1 answer

Python mock: AssertionError: Expected and actual call not same

I am new to unittest.mock library and unable to solve the issue I am experiencing. I have a class called ‘function.py’ in the below folder structure src _ init.py function.py tests init.py test_function.py In test_function.py I have some…
4
votes
1 answer

Patch SMTP client in python with unittest.mock

I want to mock out the generation of an SMTP client form smtplib. The following code: from smtplib import SMTP from unittest.mock import patch with patch('smtplib.SMTP') as smtp: print(SMTP, smtp) returns
Benjamin
  • 1,348
  • 2
  • 12
  • 25
4
votes
1 answer

Python Mock.Patch a function used at several places

This works: I got my program: # module/core_functions.py def new_input(question): print(question) value = input(">").strip() return value def main_function(): #do things new_input("question1") #do other things …
Romn
  • 174
  • 2
  • 14
4
votes
2 answers

How can I use unittest.mock to remove side effects from code?

I have a function with several points of failure: def setup_foo(creds): """ Creates a foo instance with which we can leverage the Foo virtualization platform. :param creds: A dictionary containing the authorization url, username, …
erip
  • 16,374
  • 11
  • 66
  • 121
3
votes
1 answer

Python mock testing and API call locally using it's JSON pre-generated file

I'm currently facing a problem with unit testing a specific function in my code. The function makes an API call, but I want to test it using a locally stored JSON file that represents the response from an older API call. My goal is to ensure that…
1 2
3
21 22