python-mock

Mock an entire module in python

Mock an entire module in python Question: I have an application that imports a module from PyPI. I want to write unittests for that application’s source code, but I do not want to use the module from PyPI in those tests. I want to mock it entirely (the testing machine will not contain that PyPI …

Total answers: 3

Mocking async call in python 3.5

Mocking async call in python 3.5 Question: How do I mock async call from one native coroutine to other one using unittest.mock.patch? I currently have quite an awkward solution: class CoroutineMock(MagicMock): def __await__(self, *args, **kwargs): future = Future() future.set_result(self) result = yield from future return result Then class TestCoroutines(TestCase): @patch(‘some.path’, new_callable=CoroutineMock) def test(self, mock): some_action() …

Total answers: 9

Can't catch mocked exception because it doesn't inherit BaseException

Can't catch mocked exception because it doesn't inherit BaseException Question: I’m working on a project that involves connecting to a remote server, waiting for a response, and then performing actions based on that response. We catch a couple of different exceptions, and behave differently depending on which exception is caught. For example: def myMethod(address, timeout=20): …

Total answers: 6

Using python's mock to temporarily delete an object from a dict

Using python's mock to temporarily delete an object from a dict Question: I am writing a test for some code that checks for a value in os.environ (I know this isn’t optimal, but I have to go with it). I would like to remove an entry from os.environ for the duration of the test. I …

Total answers: 3

Mocking a function to raise an Exception to test an except block

Mocking a function to raise an Exception to test an except block Question: I have a function (foo) which calls another function (bar). If invoking bar() raises an HttpError, I want to handle it specially if the status code is 404, otherwise re-raise. I am trying to write some unit tests around this foo function, …

Total answers: 1

Django tests – patch object in all tests

Django tests – patch object in all tests Question: I need to create some kind of MockMixin for my tests. It should include mocks for everything that calls external sources. For example, each time I save model in admin panel I call some remote URLs. It would be good, to have that mocked and use …

Total answers: 3

Python mock multiple return values

Python mock multiple return values Question: I am using pythons mock.patch and would like to change the return value for each call. Here is the caveat: the function being patched has no inputs, so I can not change the return value based on the input. Here is my code for reference. def get_boolean_response(): response = …

Total answers: 4

Customizing unittest.mock.mock_open for iteration

Customizing unittest.mock.mock_open for iteration Question: How should I customize unittest.mock.mock_open to handle this code? file: impexpdemo.py def import_register(register_fn): with open(register_fn) as f: return [line for line in f] My first attempt tried read_data. class TestByteOrderMark1(unittest.TestCase): REGISTER_FN = ‘test_dummy_path’ TEST_TEXT = [‘test text 1n’, ‘test text 2n’] def test_byte_order_mark_absent(self): m = unittest.mock.mock_open(read_data=self.TEST_TEXT) with unittest.mock.patch(‘builtins.open’, m): result …

Total answers: 3

How to mock os.walk in python with a temporary filesystem?

How to mock os.walk in python with a temporary filesystem? Question: I’m trying to test some code that uses os.walk. I want to create a temporary, in-memory filesystem that I can populate with sample (empty) files and directories that os.walk will then return. This should save me the complexity of mocking os.walk calls to simulate …

Total answers: 1

Checking call order across multiple mocks

Checking call order across multiple mocks Question: I have three functions that I’m trying to test the call order of. Let’s say that in module module.py I have the following # module.py def a(*args): # do the first thing def b(*args): # do a second thing def c(*args): # do a third thing def main_routine(): …

Total answers: 3