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
5
votes
2 answers

Checking call order with pytest-mock

In the code snippet shown below I would like to test the function call order in the run() function, i.e, f_3 is called after f_2 that is called after f_1: class TestMock: def f_1(self) -> None: pass def f_2(self) -> None: …
Alexey Abramov
  • 435
  • 1
  • 3
  • 16
5
votes
1 answer

pytest mocker fixture mock module from where it is defined and not where it is used

I have some utils function at src/utils/helper.py Imagine I have a function called func_a in utils/helper.py and it is used at multiple places in my project. And every time I use it, I import it like this from src.utils.helper import func_a Now I…
Rahul Kumar
  • 2,184
  • 3
  • 24
  • 46
5
votes
1 answer

pytest-mock patch context manager not restoring object on exit

We've recently switched from unittest to pytest. I've encountered a strange problem when using mocker.patch as a context manager. Consider the following example. module_a.py class MyClass: def value(self): return 10 module_b.py import…
jgrant
  • 1,273
  • 1
  • 14
  • 22
4
votes
2 answers

How do you use patch() as a context manager?

I have a class that mocks database functionality which does not subclass Mock or MagicMock because it defines its own __init__() method: class DatabaseMock(): def __init__(self, host=None): self.host = host self.x = {} #…
dev
  • 61
  • 1
  • 5
4
votes
1 answer

How to patch a property with mocker in pytest

I have a project that I need to mock a property using the mocker fixture. Its using pytest and pytest-mock: pip install pytest pytest-mock A simple example of the problem: I have the foo class in the foo.py file: class Foo: @property def…
Cristiano Araujo
  • 1,632
  • 2
  • 21
  • 32
4
votes
1 answer

Pytest-mock not patching imported function in class module

The pytest-mock patching does not work as expected. My code: utils.py: def my_func(): return 42 classes.py: from utils import my_func class MyClass: def class_method(self): return my_func() test_classes.py: import pytest from…
felice
  • 1,185
  • 1
  • 13
  • 27
4
votes
1 answer

TypeError: super() argument 1 must be type, not MagicMock - azure-devops python package

I am using the azure-devOps python module to access the azure APIs to retrieve some information. I have used the below code to set up the authentication required to make a connection. def retrieve_projects_from_organization(self): credentials =…
4
votes
1 answer

Mock class method that is called within another function

I want to mock a class method's output, which is called by a function defined in a different module. For example: class_module.py class my_class: def __init__(self, user): self.user = user def my_method(self): return…
Miguel Wang
  • 183
  • 1
  • 12
4
votes
1 answer

How to remove a library with monkeypatch or mock in pytest?

If my library has a contrib extra that has dependencies in it (say requests) that I want users to have to install to have access to a CLI API, but I install the contrib extra during my tests in CI how do I use pytest's MonkeyPatch to remove the…
Matthew Feickert
  • 786
  • 6
  • 26
4
votes
0 answers

Python asynctest mock patch decorator spilling into subsequent tests

I am trying to test an async function. For this i am using pytest-asyncio and asynctest. I need to check how many times a function that is being used inside the function that i am testing is called. For this i am mocking the internal function using…
4
votes
1 answer

How to mock third party static method

In order to simplify my question, consider the following code: How do you write test for function foo1 with patching\mocking the S3Downloader.read_file part? I prefer you will show me example usage of pytest-mock or even unitest.mock from…
JavaSa
  • 5,813
  • 16
  • 71
  • 121
3
votes
3 answers

Python Mocking - How to obtain call arguments from a mock that is passed to another mock as a function argument?

I am not sure about the title of this question, as it is not easy to describe the issue with a single sentence. If anyone can suggest a better title, I'll edit it. Consider this code that uses smbus2 to communicate with an I2C device: #…
davidA
  • 12,528
  • 9
  • 64
  • 96
3
votes
1 answer

Python patched Class - method return_value returns MagicMock

I have a function that determines if a token can be pulled from environment variables, or from Google Cloud Secret Manager (via a custom class). I am mocking the secret manager connection class so it doesn't actually connect to GCP, however the…
Dom.MB
  • 73
  • 1
  • 6
3
votes
1 answer

NameError: name 'mocker' is not defined using pytest

I'm learning to use mock when testing with pytest. I got a simple test function: import pytest import smtplib def test_send_email(): with mocker.patch('smtplib.SMTP') as mock: assert True But when I try to run this test I got the…
APianist
  • 291
  • 1
  • 4
  • 14
3
votes
1 answer

Where does `pytest`'s `mocker` come from?

I am following this mini-tutorial/blog on pytest-mock. I can not understand how the mocker is working since there is no import for it - in particular the function declaration def test_mocking_constant_a(mocker): import mock_examples.functions from…
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
1
2
3
16 17