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

Testing postgresql in unit tests - mocking and unit test libraries

I am going to start a project in Python which will deal with a lot of database operations and this means I have to write tons of unit tests. I was reading about mocking database connections, cursors and executing sql queries for my unit tests (that…
krish7919
  • 892
  • 2
  • 13
  • 30
2
votes
2 answers

Patching a method of a class that is already patched

I'm trying to patch a class and its method, this is an example that what I'm trying to do: class Car: def __init__(self, color): self.color = color def show_color(self): return self.color I have this class and I want to…
israteneda
  • 705
  • 1
  • 14
  • 26
2
votes
2 answers

Python unittest: Mock an external library function called from a class object

Hello I have the following code; I am trying to test the load function inside file_a; download is function in an external module I imported file_a.py from foo import download class Bar() __init__(self, arg_1): self.var = arg_1 def…
2
votes
1 answer

Pytest Cov Report Missing Mock Exception Returns

I'm a Network Engineer who is trying to write some Python, so very much still learning each day. I have an issue with Unit Testing and Pytest coverage reports. I think I understand the concept of unit tests and pytest. I have a function that does a…
2
votes
2 answers

mocking environment variables during testing

I have a very simple fastapi application which i want to test , the code for dummy_api.py is as follows : import os from fastapi import FastAPI app = FastAPI() @app.get(os.getenv("ENDPOINT", "/get")) def func(): return { "message":…
Subhayan Bhattacharya
  • 5,407
  • 7
  • 42
  • 60
2
votes
2 answers

Mock default parameter value in inner function

I have the following (simplified) piece of code: def get_redis() return redis_instance def bar(key, value, redis=get_redis()) redis.set(key, value) def foo() bar("key", value) In my test I want to mock the function get_redis to return an…
Rodrigo
  • 135
  • 4
  • 45
  • 107
2
votes
1 answer

How to create a unit test for a function that uses Python's http.client library using pytest and mocks?

How do I write a mock test for the following function using pytest? import http.client def get_response(req_type, host, sub_domain, payload=None, headers=None, body=None): conn = http.client.HTTPSConnection(host) …
stormfield
  • 1,696
  • 1
  • 14
  • 26
2
votes
1 answer

How to check if method is called with the expected objects

How can I test in pytest-mock whether a method has been called with a corresponding object or not? My object is the following: class Obj: def __init__(self): self.__param = [] self.__test = [] @property def…
user5580578
  • 1,134
  • 1
  • 12
  • 28
2
votes
1 answer

Patch attribute of mocked class

I'm attempting to mock a class. The class I'm attempting to mock looks like the following (lines have been removed for brevity): class Connection(object): """Connection. """ def __init__(self, base_url=None, creds=None,…
a11smiles
  • 1,190
  • 1
  • 9
  • 21
2
votes
1 answer

pytest-mock pathlib.Path.open

I need to mock pathlib.Path.open using pytest-mock. The real open_func opens a yaml-file. The return value is a regular dict. How can I mock Path.open to just load another yaml-file called test-config.yaml? My code is not working properly as conf…
Andi
  • 3,196
  • 2
  • 24
  • 44
2
votes
1 answer

Python Mock Patch From Another File/Module

I have a mock that works fine as expected. from mock import patch def second(arg): return 3 def first(): return second('arg') @patch('test.second') def test_test(second_mock): second_mock.return_value = 47 # We decide this …
doedotdev
  • 555
  • 4
  • 17
2
votes
1 answer

Cannot use attach_mock with an autospec function mock

Library module: # mod.py def foo(): bar1("arg1") bar2("arg2x", "arg2y") def bar1(x): pass def bar2(x, y): pass Test module: # test_mod.py from mod import foo def test_foo(mocker): mock = mocker.MagicMock() …
wim
  • 338,267
  • 99
  • 616
  • 750
2
votes
1 answer

How do I use PropertyMock to return requests response attributes in a pytest unit test?

I'm using the pytest-mock wrapper with pytest. I can't get a PropertyMock to work with the requests package. Here's an example of a function I'm trying to unit test: def get(): url = f'http://foo.bar/' response = requests.get(url) if…
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
1
vote
1 answer

"MockerFixture" has no attribute "assert_called_once" [attr-defined]

In module.py, I have: def func(x: int) -> str | None: if x > 9: return "OK" return None def main(x: int) -> str | None: return func(x) In test_module.py, I have: import pytest from pytest_mock import MockerFixture from module…
Laurent
  • 12,287
  • 7
  • 21
  • 37
1
vote
1 answer

Error in the patch of a function by mockers and pytest

I am trying to use pytest and mocker to test a python package that I am writing. This is the outline of my repo (assuming package is called hoopla) hoopla |- library |- __init__.py |- general |- exceptions |- bourhaha |- tests |-…
Danny
  • 23
  • 5