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
3
votes
1 answer

How does mock testing REST APIs test the API when the actual API is not called?

I am learning to use mock tests for my FastAPI endpoints. And I am puzzled by this very basic question: how does mock test actually test an API response if the actual HTTP call is not made? I understand that by mimicking the expected response, we…
sjd
  • 1,329
  • 4
  • 28
  • 48
3
votes
1 answer

Generate temporary Directory with files in Python for unittesting

I want to create a temporary folder with a directory and some files: import os import tempfile from pathlib import Path with tempfile.TemporaryDirectory() as tmp_dir: # generate some random files in it Path('file.txt').touch() …
DSGym
  • 2,807
  • 1
  • 6
  • 18
3
votes
0 answers

How to use mock and parametrize on the same function

I'm trying to mock 3 functions in my code and parameterize the two other variables. Here an example: @pytest.mark.parametrize('a, b', [ (5,…
3
votes
2 answers

Check if pytest fixture is called once during testing

Does pytest provides functionality like unittest.mock to check if the mock was actually called once(or once with some parameter)? Sample Source code: my_package/my_module.py from com.abc.validation import Validation class MyModule: def…
Sandesh Wani
  • 39
  • 1
  • 8
3
votes
1 answer

pytest parametrize an autouse fixture

I have a large amount of tests (high 100s) that use pytest and rely on a fixture that is set for autouse. I need to run those same 100s of tests with a slight variation that's controlled by the fixture. Consider the following setup that demonstrates…
theG
  • 1,001
  • 1
  • 8
  • 9
3
votes
1 answer

How to mock class attributes using pytest

I have a class something like this. class Upgrade: def __init__(self, ssh_client): self.ssh_client = ssh_client self.channel = self.ssh_client.invoke_shell(width=1000, height=1000) self.stdin =…
Shirantha Madusanka
  • 1,461
  • 1
  • 11
  • 16
3
votes
1 answer

pytest-mock how to patch nested function

I have the following code: my_module.py def my_func(number): def nested_func(value): ''' Doing some calculation ''' return result output = [] for i in range(number): res = nested_func(i) …
Dipas
  • 294
  • 2
  • 9
  • 21
3
votes
0 answers

Mocking a Flask POST request with json arguments using pytest and pytest-mock

I want to test the input/output for a POST request with flask, and possibly mock both outcomes if possible. For simplicity, I've stripped down most of the logic. # extract.py import app # This is the main flask app. Just a basic setup. from flask…
3
votes
1 answer

Using pytest, how do I mock pathlib's Path.isdir() function together with os.listdir

I am trying to write a pytest test for this function. It looks for folders named like a day. from settings import SCHEDULE from pathlib import Path import os import datetime def get_schedule_dates(): schedule_dates = [] for dir in…
576i
  • 7,579
  • 12
  • 55
  • 92
2
votes
0 answers

Pytest patch function used by global variable

I need to test a code that has a global variable which is populated by a function that returns a value after making a connection to external server. For the pytest, I need to figure out a way so that the function does not get called. I've tried…
Kevin Tianyu Xu
  • 646
  • 2
  • 8
  • 15
2
votes
1 answer

How to get call_count after using pytest mocker.patch

I'm using pytest-mocker to patch a function to mock away what it's doing. But I also want to know how many times it was called and its call args. Some script: def do_something(): x = 42 return calculate(x) def calculate(num): return num…
rawr rang
  • 763
  • 2
  • 7
  • 24
2
votes
2 answers

pytest mock method result

I have this class method def _copy_blob(self, source_blob: str, target_file_path: str) -> None: """ Copy blob to a new location :param source_blob: :param target_file_path: """ copied_blob =…
Blue Moon
  • 4,421
  • 20
  • 52
  • 91
2
votes
1 answer

Pytest mock file write for arbitrary files

I'm trying to mock the below code snippet out, but have been hitting a wall. with open(file_path, 'wb') as f: f.write(b''.join(byte_data)) In the test where I'm trying to mock this out, I don't actually want a file to be written too. I actually…
James
  • 1,158
  • 4
  • 13
  • 23
2
votes
2 answers

How to assert member calls?

Since all members of a MagicMock instance are also MagicMock instances, I figured I could just mock the top-level object without having to mock every single member along a chain of calls. Then I could simply ask my original mock if any of its…
Haterind
  • 1,095
  • 1
  • 8
  • 16
2
votes
1 answer

Mock instance attributes without instance

I would like to test by mocking an instance's attribute, but I do not have access to the instance beforehand. How can I mock the attribute without it? Here is my minimum reproducible code. # test.py class Foo: def __init__(self, x): …
spagh-eddie
  • 124
  • 9
1 2
3
16 17