Skip to main content

All Questions

Filter by
Sorted by
Tagged with
1 vote
2 answers
614 views

How to Mock a Function Within a FastAPI Route Using Pytest

I'm working on a FastAPI application and am trying to write some tests for one of my routes. My testing environment cannot make calls to external services. My goal is just to test the parsing of the ...
jakobkub's user avatar
2 votes
2 answers
239 views

Why does unittest's `mock.patch.start` re-run the function in which the patcher is started?

Let's say we have two files: to_patch.py from unittest.mock import patch def patch_a_function(): print("Patching!") patcher = patch("to_be_patched.function") patcher....
AmagicalFishy's user avatar
0 votes
0 answers
466 views

Patched function return value not being used with pytest

I have a test file where I'm mocking a function to return a boolean value. For example, file1.py is in a directory called service. file1.py def is_development(): return is_not_other_env() or ...
Paul's user avatar
  • 1,421
0 votes
1 answer
1k views

Get patch'd object with use of unittest.mock's patch decorator and new

I have the below Python==3.8 code, where I: Use unittest.mock.patch as a decorator Preform a patch(..., new=...): from unittest.mock import patch class Foo: pass class Bar: pass @patch(...
Intrastellar Explorer's user avatar
1 vote
1 answer
1k views

How to mock a class in one script from another test script running the first

I have a class SQLES in a separate script, sqles.py, which is imported in main.py and comprises methods to read SQL data and set the results in the class instance. I am now writing a test script, ...
John R Ramsden's user avatar
1 vote
0 answers
2k views

Python how to patch entire object with patch.object

Python's unittest.mock built-in library provides patch.object, which let's you: patch the named member (attribute) on an object (target) with a mock object. With regular patch, you can easily ...
Intrastellar Explorer's user avatar
0 votes
0 answers
397 views

Patch global variable once in several modules

Imagine that we have a module one.py with some global variable glob_var=object(). Also I have some other modules on different levels that import glob_var using relative import, such as: from .one.py ...
hasam's user avatar
  • 165
1 vote
1 answer
556 views

How to patch method io.RawIOBase.read with unittest?

I've recently learned about unittest.monkey.patch and its variants, and I'd like to use it to unit test for atomicity of a file read function. However, the patch doesn't seem to have any effect. Here'...
Ghostkeeper's user avatar
  • 3,040