python-mock

Better way to mock class attribute in python unit test

Better way to mock class attribute in python unit test Question: I have a base class that defines a class attribute and some child classes that depend on it, e.g. class Base(object): assignment = dict(a=1, b=2, c=3) I want to unittest this class with different assignments, e.g. empty dictionary, single item, etc. This is extremely …

Total answers: 6

Mocking only a single method on an object

Mocking only a single method on an object Question: I’m familiar with other mocking libraries in other languages such as Mockito in Java, but Python’s mock library confuses the life out of me. I have the following class which I would like to test. class MyClassUnderTest(object): def submethod(self, *args): do_dangerous_things() def main_method(self): self.submethod(“Nothing.”) In my …

Total answers: 1

Mocking default=timezone.now for unit tests

Mocking default=timezone.now for unit tests Question: I’m trying to write unit tests for a django app that does a lot of datetime operations. I have installed mock to monkey patch django’s timezone.now for my tests. While I am able to successfully mock timezone.now when it is called normally (actually calling timezone.now() in my code, I …

Total answers: 4

Mock attributes in Python mock?

Mock attributes in Python mock? Question: I’m having a fairly difficult time using mock in Python: def method_under_test(): r = requests.post(“http://localhost/post”) print r.ok # prints “<MagicMock name=’post().ok’ id=’11111111′>” if r.ok: return StartResult() else: raise Exception() class MethodUnderTestTest(TestCase): def test_method_under_test(self): with patch(‘requests.post’) as patched_post: patched_post.return_value.ok = True result = method_under_test() self.assertEqual(type(result), StartResult, “Failed to return a …

Total answers: 3

Python Mocking a function from an imported module

Python Mocking a function from an imported module Question: I want to understand how to @patch a function from an imported module. This is where I am so far. app/mocking.py: from app.my_module import get_user_name def test_method(): return get_user_name() if __name__ == “__main__”: print “Starting Program…” test_method() app/my_module/__init__.py: def get_user_name(): return “Unmocked User” test/mock-test.py: import unittest …

Total answers: 3

Assert a function/method was not called using Mock

Assert a function/method was not called using Mock Question: I’m using the Mock library to test my application, but I want to assert that some function was not called. Mock docs talk about methods like mock.assert_called_with and mock.assert_called_once_with, but I didn’t find anything like mock.assert_not_called or something related to verify mock was NOT called. I …

Total answers: 7

How to exclude mock package from python coverage report using nosetests

How to exclude mock package from python coverage report using nosetests Question: I currently try to use the mock library to write some basic nose unittests in python. After finishing some basic example I now tried to use nosetests –with-coverage and now I have the mock package and the package I tried to ‘mock away’ …

Total answers: 3

Any way to reset a mocked method to its original state? – Python Mock – mock 1.0b1

Any way to reset a mocked method to its original state? – Python Mock – mock 1.0b1 Question: I have the following simplified class I’m mocking: class myClass(object): @staticmethod def A(): #… def check(self): #code… value = self.A() #more code… In my first test I mock only the method A from django.test import TestCase from …

Total answers: 3

Mock patching from/import statement in Python

Mock patching from/import statement in Python Question: I am trying to get mock.patch to work on the following piece of sample code: from mock import patch from collections import defaultdict with patch(‘collections.defaultdict’): d = defaultdict() print ‘d:’, d This outputs the following: d: defaultdict(None, {}) Which means that defaultdict was not patched. If I replace …

Total answers: 3

How do you mock patch a python class and get a new Mock object for each instantiation?

How do you mock patch a python class and get a new Mock object for each instantiation? Question: OK, I know this is mentioned in the manual, and probably has to do with side_effect and/or return_value, but a simple, direct example will help me immensely. I have: class ClassToPatch(): def __init__(self, *args): _do_some_init_stuff() def some_func(): …

Total answers: 2