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

How to reuse requests_mock in many tests

There are different testing functions that read from the same api. Eg.: GET: https://my.api/people I want to mock the response of this request. Hence I'm using requests_mock library. Imagine the following tests: def test_get_people_names(): names…
Tiago Duque
  • 1,956
  • 1
  • 12
  • 31
0
votes
0 answers

Why pytest runner hammett not support pytest_mock?

Pytest version = 6.0.1 Pytest-mock version = 3.6.1 Hammett version = 0.9.2 Python version = 3.9.2 When I try to run the test cases by using hammett getting the below error missing 1 required positional argument: 'mocker' After this I added some…
Lavanya
  • 65
  • 1
  • 12
0
votes
0 answers

Implement assert_called_once_with() method with default parameters

def function(mocker): mock_search = mocker.patch.object(Class, "mock_function", return_value={}) if Class.function("param1", "param2"): mock_search.assert_called_once_with("param1", "param2", url=default_url) elif…
NewToCoding
  • 199
  • 1
  • 2
  • 15
0
votes
1 answer

How to mock chained calls in pytest-mock

I am trying to use pytest-mock to mock chained function calls. @app.route('/application', methods=['PUT']) def update_application(): a = json.loads(request.data)['application'] application =…
Setu Kumar Basak
  • 11,460
  • 9
  • 53
  • 85
0
votes
1 answer

How to mock a class method that is called from another class with pytest_mock

In the below files I have InternalDogWebhookResource which calls VisitOrchestrator.fetch_visit. I am attempting to write a test for InternalDogWebhookResource but mock VisitOrchestrator.fetch_visit since it is a network call. I have tried the mock…
metersk
  • 11,803
  • 21
  • 63
  • 100
0
votes
0 answers

How to mock class level parameter

This is a very stripped back version of what I want to do. It may not seem totally useful, but that is because I have stripped away all elements not pertinent to this question; dummy.py from another_mod import some_val class Dummy: A_DICT = { …
wstk
  • 1,040
  • 8
  • 14
0
votes
2 answers

mocker.patch uses data from the previous parametrized run

I am new to pytest so I might use some pytest semantics incorrectly. In general, I am having the following issue: I am using mark.parametrize to do the mocking at a test, and when I use the same variable in an argument, mocking is using the data of…
somberlain
  • 69
  • 6
0
votes
1 answer

How to test methods of an class that don't have parameters or returns in python using pytest

Let's say that I have a class, this class have some attributes and some methods. This methods don't have any parameters or any return, they only work by changing the attributes of the class using the 'self.' property. How can I test these types of…
0
votes
1 answer

Pytest on return value of a function that makes an API call

I have a function that makes an API call, and I would like to make the return value (response) of that API call to be {"message":"NoPong"} so that the ValueError would be raised. So far, I have # root_folder/subfolder/includes/abc/functions.py from…
Rayne
  • 14,247
  • 16
  • 42
  • 59
0
votes
1 answer

Mocking API call embedded in some objects and changing behavior based on inputs within object

This is a continuation of the SO question asked here but with a more complicated pattern than originally requested. My main intention is to try to mock an API call based on values passed to its caller. The API call has no idea of the values passed…
LeanMan
  • 474
  • 1
  • 4
  • 18
0
votes
2 answers

Mocking an API call within a function based on inputs

Say one has a function that, among other tasks, makes a few api calls. Is there a way, when testing this function, to mock all the api calls and specify return values from the calls depending on the input. For example, say the function you want to…
LeanMan
  • 474
  • 1
  • 4
  • 18
0
votes
1 answer

Mocking a Python object without method calls

Imagine a class like so: class Foo(): def method_1(self): bar = Bar() bazz = Bazz(bar) return bazz.method_2() For unit testing, how can we mock the Bar object when we never call any methods on it, we're just passing it…
Boon
  • 1,073
  • 1
  • 16
  • 42
0
votes
0 answers

How to mock a function that returns an object?

I am having trouble mocking a certain function I can't get the asserts to work, here is my setup in some_package.some_file.py import SomeClass def init_some_class(credentials) return SomeClass( username=credentials['username'], …
0
votes
1 answer

Pytest BDD - select stubbed or live API calls

I'm working on developing some Behavior Driven Development i.e style tests using pytest-bdd. We want to re-use the same features and more or less the same step definitions to having both stubbed and live calls to a third party API i.e. we want to…
0
votes
1 answer

How do i mock patch a nested method call

So this is quite a 'complex' test case i want to tackle. I have a Django Model, named Store and that store has many methods and fields. I want to test a specific method of the Store Model, beging get_orders(self) That get_orders(self) method calls…