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
2
votes
1 answer

Check if dict has entry when mock is called

One of my classes needs to do something like this: class A: def __init__(self, someDict, someOtherObject): self._someDict = someDict self._someOtherObject = someOtherObject def func(self): self._someDict["key"] =…
2
votes
2 answers

python mocking a function call during module import in test file

I'm trying to mock a function call that executes when a module is imported, below is the sample of the issue that I'm having app/module.py from util import get_param PARAM = get_param('param_name') class sample(): def…
findissuefixit
  • 121
  • 2
  • 6
2
votes
1 answer

How do I mock a function in Django?

I expect the following call to which_user to return self.user no matter what is passed into it but it's behaving as if it is not mocked at all. def test_user_can_retrieve_favs_using_impersonation(self): with…
2
votes
1 answer

Mock same method with different return value

I want to patch get_age() method I have, based on the argument passed. Let's say I have get_age.py where I am using multiple times the same get_age() method. get_age("Anna") ... get_age("Ben") and then in test I want to do:…
Anemone
  • 197
  • 2
  • 7
2
votes
2 answers

Python 3 how to write unit tests for try except outside functions in modules

I would like to know how to write Python 3 unittest for try exceptblocks that are defined outside of function definitions in Python's module. Imagine that in package/module.py I have a block of code like: import os try: CONSTANT =…
lucacerone
  • 9,859
  • 13
  • 52
  • 80
2
votes
2 answers

Mocking BigQuery Connection in python

I have the following code in a python file. I have to unit test this file. But in order to do that I need to instantiate the object of the class class BigQuery(metaclass=singleton.Singleton): """ Big Query Class for operations on big query …
2
votes
1 answer

Python unittest mock running program twice

Trying to understand unittest.mock more, but not sure why its running the program twice. For simplicity, consider the code below in a file test.py: from unittest.mock import patch class T(): def __init__(self): self.setup() def…
user1179317
  • 2,693
  • 3
  • 34
  • 62
2
votes
1 answer

Python mock "nested" constructor call

I'm struggling with mocking a call of a constructor in an object method that is called by a "procedural" function. For better understanding here is my code as a short version: File ./src/b/lambda_function.py: from src.b.oee_retriever import…
Johnny90
  • 465
  • 1
  • 4
  • 14
2
votes
1 answer

Python mocking open and check for close

I am trying to mock open and want to check if close gets called at least once class MyObject(): def __init__(self,path): fp = open(path) self.file_list = [] for line in fp: self.file_list.append(line.strip()) …
TM90
  • 680
  • 7
  • 21
2
votes
1 answer

how to unit test the database connection pymysql in python?

I am trying to write unit test case to test the following method by mocking the database. How to mock the database connection without actually connecting to the real database server.I tried with sample test case. I am not sure if that is right way…
user2301
  • 1,857
  • 6
  • 32
  • 63
2
votes
1 answer

pytest: how do I get the (mock) instances returned from a mocked class?

I must be tired, because surely there is an easy way to do this. But I've read over the pytest docs and can't figure out this simple use case. I have a little package I want to test: class MyClass: def __init__(self): pass def…
jwd
  • 10,837
  • 3
  • 43
  • 67
2
votes
1 answer

Mocking Enum using unittest.mock does not work as expected

I'm trying to Mock an enum in Python so that I can assert whether a method of the class tested calls a method on the enum. This does not work as expected. The method in question is never called because an identity comparison of the mock with the…
edwardmp
  • 6,339
  • 5
  • 50
  • 77
1
vote
0 answers

Async def functions are not natively supported in pytest for telegram bot python

I am trying to create a pytest file testing multiple functions (start and name) of a telegram bot. I managed to test each function individually and it works, but when I try to test multiple functions, it shows error: PytestUnhandledCoroutineWarning:…
1
vote
1 answer

Unpacking kwargs in python unittest Mock where the keyword argument has 'dot' in name

According to unittest doc (https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.configure_mock) and verified personally, mock = Mock(some_attribute='eggs', **{'method.return_value': 3, 'other.side_effect': KeyError}) works. Also…
Nifall
  • 13
  • 3
1
vote
1 answer

Python test to mock/patch to change internal function arguments, while still running function

I'm looking to mock (or patch) a function so that I can replace the arguments it receives. An example of what I want to do: # my_module.my_submodule from some_library import some_module as x def do_thing(a, b=None): return a +…
Brendan
  • 1,905
  • 2
  • 19
  • 25