New answers tagged

0 votes

How to mock a function called in a class attribute in python

As @jonrsharpe said in the comment, in your case, the function is being called at class definition time when the module is being loaded which is before it is mocked. So, the class variable already ...
defalt's user avatar
  • 242
0 votes

How to mock the existence of a particular file in python unit test?

If your function call (eg. your_function_call) uses Path("/dir_name/file_name.txt").exists() to test for the existence of the file then a simple way is: with patch("pathlib.Path.exists&...
Tomas's user avatar
  • 1,004
0 votes

python unittest.mock assert no properties attributes methods accessed

Yes, a simple mock.Mock object will not provide access to arbitrary attributes (unlike MagicMock). Just tell patch to use it instead: mock.patch('target', new=mock.Mock) For a more granular ...
knaperek's user avatar
  • 2,173
0 votes

Unit testing Asyncio ensure future

I was able to finally resolve it using the below line. # This line waits for all other spawned tasks to complete await asyncio.gather(*[t for t in asyncio.all_tasks() if t is not asyncio....
PSN's user avatar
  • 2,436
1 vote
Accepted

How to write stateful tests in python unittest?

You could have setUpClass() and call it before all the methods in the class are executed: class TestsClass(unittest.TestCase): @classmethod def setUpClass(cls): cls.e = E() ...
MarkBeras's user avatar
  • 542
1 vote
Accepted

Python Unittest - How to properly handle tests in different folders and shared base testcases?

I have recreated your directory structure: $ tree . -I __pycache__ -I my_package.egg-info . ├── pyproject.toml ├── src │   └── my_package │   └── __init__.py └── tests ├── base_test_case.py ...
ukBaz's user avatar
  • 7,382
0 votes

Python unittest.TestCase object has no attribute 'runTest'

Adding to the answer of Janne: If the tests included in several TestCase classes need to be run, addTests() will do: suite = unittest.TestSuite() suite.addTests(unittest.defaultTestLoader....
Max Herrmann's user avatar
0 votes

Python Mocking a function from an imported module

Building up on the answers already given, especially the one by Matti which helped solved my patching issues (I was patching replacing the original function, and not the function call in the module), ...
climate-coder's user avatar

Top 50 recent answers are included