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
1
vote
2 answers

How do I mock out file writing to multiple files in python

I'm trying to test a function in which one call results in multiple files being written: def pull_files(output_files=[]): for output_file in output_files: content = get_content_from_server(output_file) with open('/output/' +…
Andy
  • 3,228
  • 8
  • 40
  • 65
1
vote
1 answer

Mocking open() on pathlib.Path to return a mock file handle with unittest.mock

Say I have a method that gets passed some paths, reads the files at each of these paths, then returns a dictionary from file name to file content, like so: from contextlib import ExitStack from pathlib import Path class ClassToTest: def…
UtterlyConfused
  • 983
  • 1
  • 10
  • 18
1
vote
1 answer

Python mocking function from imported module

How to write unit test case for following code. The code just returns a db connection. I can't create a real db connection. So, i am dependent on object mocking. File : code.py import snowflake.connector import logging import simplejson as…
SunilS
  • 2,030
  • 5
  • 34
  • 62
1
vote
1 answer

mocked function is called but fails assert_called test

I have a test like: import somemodule import somemodule2 class SomeTestCase(unittest.TestCase): def setUp(self): super().setUp() self.ft_mock = mock.MagicMock(spec=somemodule.SomeClass, name='mock_it') …
user2290820
  • 2,709
  • 5
  • 34
  • 62
1
vote
2 answers

Mocking dbconnection fetchall using python

I am trying to mock the fetchall() from dbconnection cursor object. I am trying the following code with expected return value. However, it was not returning the value. I have the answer now and edited the unit test to include the answer…
SunilS
  • 2,030
  • 5
  • 34
  • 62
1
vote
1 answer

python unittest - How to preserve a function's side effects which has been converted into a Mock object?

I want to count the number of times that a function is called, python patch has been recommended allowing me to call call_count and things like assert_not_called to verify just that. My problem is that I want the function to perform just as it did,…
hi im Bacon
  • 374
  • 1
  • 12
1
vote
0 answers

Python unittest.mock.patch not working as expected

I am trying to test a function I created to send a welcome e-mail to a user. But, to do this, I have to mock the function that actually sends it (inside the welcome e-mail function). I have the following folder structure: app/ __init__.py …
leodaher
  • 11
  • 4
1
vote
1 answer

Mock response with 500 status code in flask and pytest

* EDITED* I would like to test what will happen if external API will return 500 status code. main.py @app.route("//", methods=["GET"]) def repo_info(a: str, b: str) -> Union[Response, str]: info = some_func(a, b) result =…
1
vote
0 answers

How to mock an object/method for certain parameters only

I'm writing a test that asserts that a certain method is called with certain parameters. I have a method that does some pandas.Timestamp() calls and some of them include pandas.Timestamp("now"). And this is where the problem arises. class…
Ivar Stange
  • 303
  • 2
  • 12
1
vote
2 answers

How do I mock Python properties at an instance level?

As the unittest.mock documentation points out: Because of the way mock attributes are stored you can’t directly attach a PropertyMock to a mock object. Instead you can attach it to the mock type object However, I'm encountering cases where I want…
1
vote
0 answers

How should I test a method of a mocked object

I have a question about how to mock a nested method and test what it was called with. I'm having a hard time getting my head around: https://docs.python.org/3/library/unittest.mock-examples.html#mocking-chained-calls. I'd like to test that the…
Utegrad
  • 73
  • 7
1
vote
2 answers

Python unittest.mock: patched class method called but assertion fails

I'm trying to mock several components of a utility class. While assert_called() is ok for one method it fails for an other but I am sure that both are called. I'm running Python 3.7.3 on Windows 10. I have stripped down my scenario to the…
Kai Roesner
  • 429
  • 3
  • 17
1
vote
1 answer

Use methods on Mock object

I have an object that is used for fetching information from another service which is very simple. Since the object is simple and the initialization method could be easily patched I thought I would try to write my code to be super reusable and…
1
vote
2 answers

Connection unit testing in Python

I am new to Python unit testing and I am not sure how i can create a unit test of this function that returns a connection? def connection(self): connection = mysql.connector.connect(host='localhost', database='test', …
1
vote
0 answers

Patch method not working at import time

I am trying to mock the beginning of the file of the module dog.py. It seems that the patch method does not work at the import time but it works at later invocations. test.py import unittest from unittest.mock import patch class…
poiuytrez
  • 21,330
  • 35
  • 113
  • 172