python-mock

Mocking file objects or iterables in python

醉酒当歌 提交于 2019-12-01 02:47:49
Which way is proper for mocking and testing code that iters object returned by open() , using mock library? whitelist_data.py : WHITELIST_FILE = "testdata.txt" format_str = lambda s: s.rstrip().lstrip('www.') whitelist = None with open(WHITELIST_FILE) as whitelist_data: whitelist = set(format_str(line) for line in whitelist_data) if not whitelist: raise RuntimeError("Can't read data from %s file" % WHITELIST_FILE) def is_whitelisted(substr): return 1 if format_str(substr) in whitelist else 0 Here's how I try to test it. import unittest import mock TEST_DATA = """ domain1.com domain2.com

Django ORM - mock values().filter() chain

我是研究僧i 提交于 2019-11-30 17:39:43
I am trying to mock a chained call on the Djangos model.Manager() class. For now I want to mock the values() and filter() method. To test that I created a little test project: Create a virtual environment Run pip install django mock mock-django nose django-nose Create a project django-admin.py startproject mocktest Create an app manage.py startapp mockme Add django_nose and mocktest.mockme to INSTALLED_APPS (settings.py) Add TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' to settings.py To verfiy that everything is setup correctly I ran manage.py test . One test is run, the standard test

Better way to mock class attribute in python unit test

Deadly 提交于 2019-11-30 11:24:31
问题 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 simplified of course, it's not a matter of refactoring my classes or tests The (pytest) tests I have come up with, eventually, that work are from .base import Base def test_empty(self): with mock.patch("base.Base.assignment") as

Django tests - patch object in all tests

点点圈 提交于 2019-11-30 07:57:06
问题 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 like that: class ExampleTestCase(MockedTestCase): # tests So each time I save model in admin, for example in functional tests, this mock is applied instead of calling remote URLs. Is that actually possible? I'm able to do that for 1 particular test,

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

霸气de小男生 提交于 2019-11-30 06:21:45
问题 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, mocking out the call to bar() . Unfortunately, I am unable to get the mocked call to bar() to raise an Exception which is caught by my except block. Here is my code which illustrates my problem: import unittest import mock from apiclient.errors import

Django ORM - mock values().filter() chain

荒凉一梦 提交于 2019-11-30 02:05:04
问题 I am trying to mock a chained call on the Djangos model.Manager() class. For now I want to mock the values() and filter() method. To test that I created a little test project: Create a virtual environment Run pip install django mock mock-django nose django-nose Create a project django-admin.py startproject mocktest Create an app manage.py startapp mockme Add django_nose and mocktest.mockme to INSTALLED_APPS (settings.py) Add TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' to settings.py To

How to exclude mock package from python coverage report using nosetests

六月ゝ 毕业季﹏ 提交于 2019-11-30 01:27:11
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' are shown in the coverage report. Is there a possibility to exclude these? Here is the class I want to test: from imaplib import IMAP4 class ImapProxy: def __init__(self, host): self._client = IMAP4(host) And the testcase: from mock import patch from ImapProxy import ImapProxy class TestImap: def test_connect(self): with patch('ImapProxy.IMAP4') as imapMock:

Better way to mock class attribute in python unit test

牧云@^-^@ 提交于 2019-11-30 00:11:42
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 simplified of course, it's not a matter of refactoring my classes or tests The (pytest) tests I have come up with, eventually, that work are from .base import Base def test_empty(self): with mock.patch("base.Base.assignment") as a: a.__get__ = mock.Mock(return_value={}) assert len(Base().assignment.values()) == 0 def test_single

How to exclude mock package from python coverage report using nosetests

寵の児 提交于 2019-11-28 22:19:47
问题 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' are shown in the coverage report. Is there a possibility to exclude these? Here is the class I want to test: from imaplib import IMAP4 class ImapProxy: def __init__(self, host): self._client = IMAP4(host) And the testcase: from mock import patch from ImapProxy

Mocking async call in python 3.5

99封情书 提交于 2019-11-28 20:15:19
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() mock.assert_called_with(1,2,3) This works but looks ugly. Is there more pythonic way to do this? Subclassing MagicMock will propagate your custom class for all the mocks generated from your