python-unittest

Python Mock: mock calls counted by call_count

Python Mock: mock calls counted by call_count Question: Is there a way to get the mock calls counted by call_count in this example: from mock import MagicMock mock = MagicMock() mock.get_query(arg=1).execute() mock.get_query(arg=2).execute() print(‘count:’, mock.get_query.call_count) print(‘calls:’, mock.get_query.mock_calls) Actual result: count: 2 calls: [call(arg=1), call().execute(), call(arg=2), call().execute()] Desired result: count: 2 calls: [call(arg=1), call(arg=2)] I understand mock_calls. …

Total answers: 1

Re-use Patch in Python Test

Re-use Patch in Python Test Question: Not an expert. If I patch a module’s method, is it possible to re-use the same patch in other methods of the TestCase? def load(**kwargs): return 1 def load2(**kwargs): return2 @patch.multiple(‘module’, get_data=MagicMock(side_effect=load), headers=MagicMock(return_value="")) def test_get_some_method(self): # here is ok @patch.multiple(‘module’, get_data=MagicMock(side_effect=load2), headers=MagicMock(return_value="")) def test_get_other_method(self): # here I get an …

Total answers: 2

Python mock patch not mocking the object

Python mock patch not mocking the object Question: I am using the python mocking library and I am not sure why I get this result. Why only the second one got mocked and not the first one? What’s the best way to do it? import unittest from unittest.mock import patch, Mock import requests from requests …

Total answers: 1

How to test with parametrize that something is None or not None?

How to test with parametrize that something is None or not None? Question: I have a method that shall return None in some exceptional situations and otherwise something "complicated". In my dummy MWE below (not my real method!) there are two situations (x==0 and x==10) where None is returned. def foo(x: int) -> typing.Optional[float]: if …

Total answers: 2

Run a specific unittest with loadTestsFromName

Run a specific unittest with loadTestsFromName Question: I want to run a single test and output the result to a txt file. I understood that I can use loadTestsFromName to specify the test. However, I’m getting an error. test.py import unittest import sys import os def main(out=sys.stderr, verbosity=2): loader = unittest.TestLoader() suite = loader.loadTestsFromName(sys.modules[__name__]==’test1′) unittest.TextTestRunner(out, …

Total answers: 1

Patching a method from other file is not working

Patching a method from other file is not working Question: I have a method say _select_warehouse_for_order in api/controllers/orders.py file. The method is not part of any class. Now, I have a new file say api/controllers/dispatchers.py where i need to know which warehouse was selected. I am calling _select_warehouse_for_order from this file to get this information. …

Total answers: 1

Writing unit test for a dependent function

Writing unit test for a dependent function Question: I have this function that asks the user for the gender of the voice and then reads the text in that voice. If the user input is not either male or female it raises a value error. I want to write a unit test for this function, …

Total answers: 1

Argparse and unittest.main()

Argparse and unittest.main() Question: I just added a graph utility to a unittest — basically, the fully automatic version of the test just does a numerical compare, but I want a human to be able to ask for plots. Just using argparse, unittest.main() was choking if I used my new argument. What I’m currently doing …

Total answers: 1

Running unittest with modules that must import other modules

Running unittest with modules that must import other modules Question: Our Python 3.10 unit tests are breaking when the modules being tested need to import other modules. When we use the packaging techniques recommended by other posts and articles, either the unit tests fail to import modules, or the direct calls to run the app …

Total answers: 3