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

What are the differences between unittest.mock, mock, mocker and pytest-mock?

I am new to Python development, I am writing test cases using pytest where I need to mock some behavior. Googling best mocking library for pytest, has only confused me. I have seen unittest.mock, mock, mocker and pytest-mock. Not really sure which…
Pritam Bohra
  • 3,912
  • 8
  • 41
  • 72
13
votes
1 answer

Python pytest mock fails with "assert None" for function call assertions

I am trying to mock some calls to boto3 and it looks like the mocked function is returning the correct value, and it look like if I change the assertion so it no longer matches what was passed in the assertion fails because the input parameters do…
berimbolo
  • 3,319
  • 8
  • 43
  • 78
12
votes
2 answers

How to specify return value of mocked function with pytest-mock?

The below prints False. Is this not how mocking works? I tried changing the path to the function, but it errors out, so the path seems correct. What am I missing? import pytest from deals.services.services import is_user_valid class TestApi: …
M3RS
  • 6,720
  • 6
  • 37
  • 47
9
votes
1 answer

Using pytest-mock to mock objects and object methods

I'm trying to use pytest-mock for mocking. This library is essentially a plugin/wrapper for mock and patch. My problem is defined as: I have an application (mymodule.py) that uses SQL Alchemy. Basically, there's a function that defines some objects…
eduardokapp
  • 1,612
  • 1
  • 6
  • 28
9
votes
2 answers

pytest - Mocking constructor within constructor

All, I have a class similar to this. from mod import Bar class Foo: def __init__(self): self.obj = Bar() How do I mock the Bar constructor using pytest / pytest-mock? I have tried the following unsuccessfully. def test(): with…
Dan Grahn
  • 9,044
  • 4
  • 37
  • 74
8
votes
1 answer

Get apply's function input dataframe with mocking

I have the following functions def main(): ( pd.DataFrame({'a': [1, 2, float('NaN')], 'b': [1.0, 2, 3]}) .dropna(subset=['a']) .assign( b=lambda x: x['b'] * 2 ) …
ndclt
  • 2,590
  • 2
  • 12
  • 26
8
votes
1 answer

How to mock raise urllib errors

After reading this in the python docs, I am catching the HTTPError and URLError exceptions in get_response_from_external_api that the make_request_and_get_response (via urllib's urlopen call) can raise: foo.main.py from urllib.request import…
timmy78h
  • 183
  • 1
  • 3
  • 10
7
votes
1 answer

How to mock subprocess.run in pytest?

I have the class InternalProc, defined as following: class InternalProc: @staticmethod def get_data(): try: result = subprocess.run(['bridge-client', '--business-credentials'], …
user8082934
  • 391
  • 1
  • 5
  • 12
7
votes
3 answers

How to mock a imported object with pytest-mock or magicmock

I am trying to understand the mock/monkeypatch/pytest-mock capabilities. Let me know if this is possible. If not could you please suggest how I can test this code. My code…
ozn
  • 1,990
  • 3
  • 26
  • 37
6
votes
1 answer

Pytest lambda handler passing event and context

I am writing a unit test for my lambda function using pytest. I cannot figure out how should I pass my event parameters to the function call. I learnt that it can be achieved using @pytest.fixture. I am very very new to Python and pytest. Believe I…
Richa Sharma
  • 75
  • 1
  • 9
6
votes
1 answer

Python pytest-mock assert_has_calls

I am looking into the excellent pytest plugin called pytest-mock (https://github.com/pytest-dev/pytest-mock), and now I am trying some examples with assert_has_calls. In short I am testing an instance of class B, more specifically how that instance…
sxpy
  • 131
  • 1
  • 7
5
votes
2 answers

Python mock - mocking class method that modifies class attributes

I currently have the following basic Python class that I want to test: class Example: def run_steps(self): self.steps = 0 while self.steps < 4: self.step() def step(self): # some expensive API call …
5
votes
1 answer

How to mock a class instance (not a class function) in Python

I am having a hard time mocking an instance of an object. I would like to write a unit test to test 'my_func' function that uses an instance of a class. I know how to mock class functions, however, I do not know how to mock an instance of the class…
Nodame
  • 1,053
  • 3
  • 12
  • 18
5
votes
1 answer

session scope with pytest-mock

I'm looking for example of how to use the session-scoped "session-mocker" fixture of the pytest-mock plugin. It's fairly clear how to modify the example the docs provide to use it in a particular test: def test_foo(session_mocker): …
Gadzooks34
  • 1,718
  • 2
  • 20
  • 29
5
votes
1 answer

`requests_mock` applies to all requests even if they are not set and throws NoMockAddress exception

I found that requests_mock used as a fixture with pytest applies to all requests even if they are not set. I'm not sure if it's a requests_mock/pytest bug or I'm missing something. Eventually, I don't need to mock 'api-b' call but I can't find out…
1
2 3
16 17