Questions tagged [pytest-mock]

pytest-mock provides the functionality of unittest.mock.patch as a pytest fixture. It allows for more convenient patching (avoids nested context managers or unused arguments). It provides the same API as unitest.mock.patch and unitest.mock.patch.object, and additionally provides patcher.spy, which allows spying on function without changing them.

Reference:

249 questions
0
votes
1 answer

Why mocked method with pytest-mock does not return_value?

I have found here a more concise way to mock methods. But, when I run pytest, I get orig_width, orig_height = image_reader.getSize() ValueError: not enough values to unpack (expected 2, got 0) This is test_tdd.py def test_get_image_x1(mocker): …
agossino
  • 21
  • 3
0
votes
0 answers

Using python unittest.mock how can I mock an object coming from an external library created inside one of my libraries?

I'm creating some test for my python project but I have a problem creating a unittest.patch my_project.py from service.my_service import Service def main(): token = '123456' my_service = Service(token) # calls to my…
0
votes
0 answers

Mocking a function called within a function called within a function

Let's say I have a file called src/sample.py which contains: def c(): return np.random.rand(10) def b(): return c() + 2 def a(): return b() + 1 Then, in my sample_test.py, suppose in test_a, I want to mock the internal call to c inside…
user2578330
  • 57
  • 1
  • 2
  • 6
0
votes
1 answer

Unable to mock method using pytest-mock

I have a class 'MyClass' with code as below. class MyClass: def __init__(update_type_id='1') self.update_type_id = update_type_id self._cursor =
Adarsh
  • 177
  • 1
  • 13
0
votes
0 answers

Multiples mock.patch in one test

I want to know others ways to implement a test with multiple patch.mocks like this: class TestFunction(TestCase): @mock.patch('path.to.value3', side_effect=value3) # Value3 -> Response object @mock.patch('path.to.value2',…
Risker
  • 358
  • 3
  • 15
0
votes
0 answers

Why is my assert_called_once_with giving different ids when calling a mocked async method?

I am struggling with an assert_called_once_with call when mocking an async method in a class. I have the following code that needs to be tested : In class_under_test.py : class ClassUnderTest(): def func_to_be_tested(self, x, y): …
0
votes
1 answer

Testing a django `post_save` signal that includes function calls that occur after db transaction is committed

When django tests are running, database transactions are not committed. How do I test an event triggered by object creation but that happens after the db transaction has been committed? I have a Campaign model and the below post_save signal. Using…
John
  • 949
  • 1
  • 9
  • 20
0
votes
0 answers

Pytest mock local object instance inside a mocked object

Problem Mock and perform assertions on local instance objects that are not accessible from the encompassing object. Minimum Example Executor is the class to be mocked. class Executor: def execute(self, func, *args): …
Najeeb
  • 21
  • 5
0
votes
0 answers

How to create a unit test in Python for specific code in pytest

I have the following function: def update_installation_register( remote: RemoteRegisterData, install_id: str, data: dict, verify: bool ) -> None: """Updates an Installation register""" credentials = (remote.auth_user,…
ndelucca
  • 517
  • 1
  • 8
  • 20
0
votes
2 answers

How to assert that json.dump was called with the correct arguments

I'm attempting to write a unit test using PyTest and Mock that tests that the correct arguments are being passed to json.dump The function that houses the json.dump accepts 1 argument that is a list of ids that is used to make queries to the db. For…
Lenny Gonzalez
  • 123
  • 1
  • 1
  • 8
0
votes
2 answers

pytest: unittest error object has no attribute 'assert_called_once_with

I am writing unit test in pytest and getting error on assert_called_once_with. I tired to use same way as been shown in the pytest documentation but seems I am missing something. # Class which I am trying to mock. (./src/Trading.py) class…
CrazyC
  • 1,840
  • 6
  • 39
  • 60
0
votes
1 answer

assert_called_with always picking up the arguments from the last call

I am very new to python and this is probably something trivial. I have the following test: import pytest from pytest_mock import MockerFixture, call # Create environment before importing anything from app/. import makeenv from data_f import…
Shurik Agulyansky
  • 2,607
  • 2
  • 34
  • 76
0
votes
2 answers

How to mock Requests Response using monkeypatch

I am trying to mock the response of api call with pytest in using monkeypatch but without success. In a file functions.py, I have a function call an external API to get data in json format and I want to test this function def api_call(url,…
Gon
  • 19
  • 6
0
votes
0 answers

How to return calling object from mocked pandas function

I am attempting to write a test (using pytest-mock) for some code that uses pandas for I/O. Ideally instead of writing the contents to a file using pandas.to_excel() function, I would rather return the dataframe to my test function for…
drowningincode
  • 1,115
  • 1
  • 12
  • 19
0
votes
1 answer

Custom mocking for testing with pytest

I have a code base (shown below), with a src directory containing the code, that I have developed, alongside with some supporting Third Party libs in tp directory. . ├── src │   ├── f.py │   └── __init__.py ├── tp │ ├── __init__.py │ └──…
DOOM
  • 1,170
  • 6
  • 20