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

Pytest parametrize passing function name as a parameter

I am trying to get function name as a parameter in pytest parametrize but getting the error 'str' object is not callable since it passes as a string. @pytest.fixture(autouse=True) def mock_something(mocker, function_name, value): return…
seaque
  • 43
  • 7
0
votes
1 answer

how to mock patch `today` in pytest python?

Suppose I have a module that checks a date is today (this is a simplified code) # my_module.py import datetime def is_today(input_date): return input_date == datetime.date.today() I want to test this function # test_my_module.py import…
Amin Ba
  • 1,603
  • 1
  • 13
  • 38
0
votes
1 answer

pytest with complex object structure. To patch, mock, monkeypatch, refactor, or give up?

My design has led me into what I think is a complex pytest problem. I'm convinced I don't know the right approach to take. I'm simulating a complex card game, known as 'Liverpool rummy'. The user interface uses Kivy, but the problem I'm having…
0
votes
0 answers

Pytest: Assert nested mock is called

I want to make sure the mkdir function from the Path module is called in a function that I test. I mock the Path module, when I try asserting the mkdir function is called (which is, in the context of the test, a nested mock of the Path module mock),…
Milan
  • 1,547
  • 1
  • 24
  • 47
0
votes
1 answer

Unable to mock the environment variable while testing Fast API code using pytest

I have a simple fast api code as follow main.py from fastapi import FastAPI import os app = FastAPI(root_path=f"/{os.environ['EnvName']}/") @app.get("/getrecord") def root(): return {"message": "Hello World"} Below is the pytest code…
0
votes
1 answer

Use 'pytest' to mock a function that can return multiple exceptions

Using pytest, I wish to mock a function that can raise several exceptions. My app will catch the exception and create a response object, and I need to assert that each response contains the correct message, type, and in reality several other…
David Gard
  • 11,225
  • 36
  • 115
  • 227
0
votes
1 answer

Test mocked instance while leaving its methods executable

I have something similar to the code below where I want to test some flow in ClassToTest using pytest_mock. To do that, I need to pass it an object that implements some specific methods, in this case method1 and method2 in MockClass. After the…
Ilya
  • 730
  • 4
  • 16
0
votes
1 answer

How to get the internal state of a mock object using python

I want to use a mock object to emulate the database on a insert operation. For example, let's suppose I have a method like insert(1) that calls a db connection object (let's call it db_obj) and performs some insert into mytable (col1) values (%s)…
shikida
  • 485
  • 2
  • 10
0
votes
1 answer

Pytest- mock chained call

I'm trying to mock chained call in pytest (and pytest-mock package), but for some unknown (for me) reason it doesn't work which means I don't get mocked return_value from my mock. It looks like it created object of MagicMock, but without defined…
0
votes
1 answer

Mock an internal object call in a Flask view - with pytest

Based on the following example: app = Flask(__name__) @app.route('/users') def get_users(): return UsersAPI().get_users() And the following tests (using pytest and pytest-mock): @pytest.fixture def users(): return…
renatodamas
  • 16,555
  • 8
  • 30
  • 51
0
votes
1 answer

Pytest mock pandas read_excel inside the Class Construction

I am doing some Integration tests using Pytest. I am reading my excel sheet input from the class constructor. I don't know how to mock the read_excel function of pandas. My actual code, import pandas as pd class Sample: def __init__(self,…
Smith Dwayne
  • 2,675
  • 8
  • 46
  • 75
0
votes
0 answers

Mocking socket acitivity in Python

I've just started working with Python about 3 months ago and I'm currently struggling with using pytest-mock for testing sockets. I was trying to write test using mock to check if the number of calls for socket methods is correct, however I can't…
0
votes
2 answers

Mock an external service instantiated in python constructor

I'm working on unit tests for a service I made that uses confluent-kafka. The goal is to test successful function calls, exception errors, etc. The problem I'm running into is since I'm instantiating the client in the constructor of my service the…
0
votes
0 answers

relative path issue with mocking/patching in python

I am trying to mock a method called get_value_from_cache which is being used in main.api.funds file. My project directory is as follows. my_rest_api - python - __init__.py - main - __init__.py - api …
Pritam Bohra
  • 3,912
  • 8
  • 41
  • 72
0
votes
1 answer

How to mock a class methods that return another class objects with pytest?

I have a ClassA that has multiple methods, one of the methods returns an object of ClassB, where class B has it’s own methods myfile1.py: from b import ClassB class ClassA(object): def method_1(self): f = ClassB() return…
Ale
  • 645
  • 4
  • 16
  • 38