我正在使用pythons mock.patch,并希望更改每个调用的返回值.以下是警告:正在修补的函数没有输入,因此我无法根据输入更改返回值.
这是我的代码供参考.
def get_boolean_response():
response = io.prompt('y/n').lower()
while response not in ('y', 'n', 'yes', 'no'):
io.echo('Not a valid input. Try again'])
response = io.prompt('y/n').lower()
return response in ('y', 'yes')
Run Code Online (Sandbox Code Playgroud)
我的测试代码:
@mock.patch('io')
def test_get_boolean_response(self, mock_io):
#setup
mock_io.prompt.return_value = ['x','y']
result = operations.get_boolean_response()
#test
self.assertTrue(result)
self.assertEqual(mock_io.prompt.call_count, 2)
Run Code Online (Sandbox Code Playgroud)
io.prompt只是一个独立于平台(python 2和3)版本的"输入".所以最终我试图模拟用户输入.我已经尝试使用列表作为返回值,但这并不能解决问题.
你可以看到,如果返回值是无效的,我将在这里得到一个无限循环.所以我需要一种方法来最终改变返回值,以便我的测试实际完成.
(回答这个问题的另一种可能的方法是解释我如何在单元测试中模仿用户输入)
不是这个问题的重复,主要是因为我没有能力改变输入.
答案中关于这个问题的评论之一是相同的,但没有提供答案/评论.
我正在使用Mock库来测试我的应用程序,但我想断言某些函数没有被调用.模拟文档讨论像mock.assert_called_with和的方法mock.assert_called_once_with,但我没有发现任何类似mock.assert_not_called或与确认模拟相关的东西没有调用.
我可以使用类似下面的东西,虽然它看起来不酷也不像pythonic:
def test_something:
# some actions
with patch('something') as my_var:
try:
# args are not important. func should never be called in this test
my_var.assert_called_with(some, args)
except AssertionError:
pass # this error being raised means it's ok
# other stuff
Run Code Online (Sandbox Code Playgroud)
任何想法如何实现这一目标?
谢谢你的帮助 :)
我有一个函数(foo)调用另一个函数(bar).如果调用bar()引发一个HttpError,我想特别处理它,如果状态代码是404,否则重新加注.
我试图围绕这个foo函数编写一些单元测试,嘲笑调用bar().不幸的是,我无法通过模拟调用bar()来引发由我的except块捕获的异常.
这是我的代码,说明了我的问题:
import unittest
import mock
from apiclient.errors import HttpError
class FooTests(unittest.TestCase):
@mock.patch('my_tests.bar')
def test_foo_shouldReturnResultOfBar_whenBarSucceeds(self, barMock):
barMock.return_value = True
result = foo()
self.assertTrue(result) # passes
@mock.patch('my_tests.bar')
def test_foo_shouldReturnNone_whenBarRaiseHttpError404(self, barMock):
barMock.side_effect = HttpError(mock.Mock(return_value={'status': 404}), 'not found')
result = foo()
self.assertIsNone(result) # fails, test raises HttpError
@mock.patch('my_tests.bar')
def test_foo_shouldRaiseHttpError_whenBarRaiseHttpErrorNot404(self, barMock):
barMock.side_effect = HttpError(mock.Mock(return_value={'status': 500}), 'error')
with self.assertRaises(HttpError): # passes
foo()
def foo():
try:
result = …Run Code Online (Sandbox Code Playgroud) 我想了解如何@patch从导入的模块中获取函数.
这是我到目前为止的地方.
应用程序/ 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()
Run Code Online (Sandbox Code Playgroud)
应用程序/ my_module/__ init__.py:
def get_user_name():
return "Unmocked User"
Run Code Online (Sandbox Code Playgroud)
测试/ mock-test.py:
import unittest
from app.mocking import test_method
def mock_get_user():
return "Mocked This Silly"
@patch('app.my_module.get_user_name')
class MockingTestTestCase(unittest.TestCase):
def test_mock_stubs(self, mock_method):
mock_method.return_value = 'Mocked This Silly')
ret = test_method()
self.assertEqual(ret, 'Mocked This Silly')
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
这并没有工作,我期望的那样."patched"模块只返回unmocked值get_user_name.如何从我导入到测试命名空间的其他包中模拟方法?
我mock在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 StartResult.")
Run Code Online (Sandbox Code Playgroud)
测试实际上返回正确的值,但是r.ok是Mock对象,而不是True.你如何在Python的mock库中模拟属性?
我有一个我正在测试的类,它具有另一个类的依赖(其实例被传递给CUT的init方法).我想使用Python Mock库来模拟这个类.
我所拥有的是:
mockobj = Mock(spec=MyDependencyClass)
mockobj.methodfromdepclass.return_value = "the value I want the mock to return"
assertTrue(mockobj.methodfromdepclass(42), "the value I want the mock to return")
cutobj = ClassUnderTest(mockobj)
Run Code Online (Sandbox Code Playgroud)
这很好,但是"methodfromdepclass"是一个参数化方法,因此我想创建一个模拟对象,根据传递给methodfromdepclass的参数,它返回不同的值.
我想要这个参数化行为的原因是我想创建包含不同值的ClassUnderTest的多个实例(其值由mockobj返回的值生成).
有点我在想什么(这当然不起作用):
mockobj = Mock(spec=MyDependencyClass)
mockobj.methodfromdepclass.ifcalledwith(42).return_value = "you called me with arg 42"
mockobj.methodfromdepclass.ifcalledwith(99).return_value = "you called me with arg 99"
assertTrue(mockobj.methodfromdepclass(42), "you called me with arg 42")
assertTrue(mockobj.methodfromdepclass(99), "you called me with arg 99")
cutinst1 = ClassUnderTest(mockobj, 42)
cutinst2 = ClassUnderTest(mockobj, 99)
# now cutinst1 & cutinst2 contain different values …Run Code Online (Sandbox Code Playgroud) 如何模拟从一个本地协程到另一个使用的异步调用unittest.mock.patch?
我目前有一个很尴尬的解决方案:
class CoroutineMock(MagicMock):
def __await__(self, *args, **kwargs):
future = Future()
future.set_result(self)
result = yield from future
return result
Run Code Online (Sandbox Code Playgroud)
然后
class TestCoroutines(TestCase):
@patch('some.path', new_callable=CoroutineMock)
def test(self, mock):
some_action()
mock.assert_called_with(1,2,3)
Run Code Online (Sandbox Code Playgroud)
这有效,但看起来很难看.是否有更多的pythonic方式来做到这一点?
我有以下简化的类我在嘲笑:
class myClass(object):
@staticmethod
def A():
#...
def check(self):
#code...
value = self.A()
#more code...
Run Code Online (Sandbox Code Playgroud)
在我的第一次测试中,我只模拟方法A.
from django.test import TestCase
from mock import MagicMock
import myClass
class FirstTest(TestCase):
def setUp(self):
myClass.A = MagicMock(return_value = 'CPU')
def test(self):
#some tests
myClassObj = myClass()
myClassObj.check()
Run Code Online (Sandbox Code Playgroud)
在我的第二次测试中,我模拟了整个检查方法:
from django.test import TestCase
from mock import MagicMock
import myClass
class SecondTest(TestCase):
def setUp(self):
myClass.check = MagicMock(return_value = someObject)
def test(self):
#some tests
myClassObj = myClass()
myClassObj.check()
Run Code Online (Sandbox Code Playgroud)
现在我的第一次测试中的断言失败了,因为它不是在内部调用check()和嘲笑,而是从第二次测试中调用完全嘲笑.A()check()check()
有没有办法在测试后清除并将方法设置为"正常"?我myClass.check.reset_mock()已经尝试了,但它似乎没有做任何事情.移动我的测试顺序也没有做任何事情. …
我目前尝试使用模拟库在python中编写一些基本的鼻子单元测试.
在完成了一些基本的例子后,我现在尝试使用nosetests --with-coverage,现在我有了模拟包,我试图'模拟掉'的包显示在覆盖率报告中.是否有可能排除这些?
这是我要测试的类:
from imaplib import IMAP4
class ImapProxy:
def __init__(self, host):
self._client = IMAP4(host)
Run Code Online (Sandbox Code Playgroud)
和测试用例:来自模拟导入补丁
from ImapProxy import ImapProxy
class TestImap:
def test_connect(self):
with patch('ImapProxy.IMAP4') as imapMock:
proxy = ImapProxy("testhost")
imapMock.assert_called_once_with("testhost")
Run Code Online (Sandbox Code Playgroud)
我现在得到以下输出 nosetests --with-coverage
.
Name Stmts Miss Cover Missing
------------------------------------------
ImapProxy 4 0 100%
imaplib 675 675 0% 23-1519
mock 1240 810 35% [ a lot of lines]
Run Code Online (Sandbox Code Playgroud)
有没有办法排除模拟包和imaplib包,而不必手动将除了那些包之外的所有包列入白名单--cover-package=PACKAGE
感谢Ned Batchelder,我现在知道.coveragerc文件了,谢谢!
我创建了一个包含以下内容的.coveragerc文件:
[report]
omit = *mock*
Run Code Online (Sandbox Code Playgroud)
现在我在覆盖率报告中的模拟输出是:
mock 1240 1240 0% 16-2356 …Run Code Online (Sandbox Code Playgroud) 我有三个函数,我正在尝试测试它的调用顺序.
假设在模块module.py中我有以下内容
# 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():
a_args = ('a')
b_args = ('b')
c_args = ('c')
a(*a_args)
b(*b_args)
c(*c_args)
Run Code Online (Sandbox Code Playgroud)
我想检查b在a之后和c之前被调用.因此,对a,b和c中的每一个进行模拟很容易:
# tests.py
@mock.patch('module.a')
@mock.patch('module.b')
@mock.patch('module.c')
def test_main_routine(c_mock, b_mock, a_mock):
# test all the things here
Run Code Online (Sandbox Code Playgroud)
检查每个单独的模拟被调用也很容易.如何检查呼叫相对于彼此的顺序?
call_args_list 不会工作,因为它是为每个模拟单独维护.
我尝试使用副作用来记录每个调用:
calls = []
def register_call(*args):
calls.append(mock.call(*args))
return mock.DEFAULT
a_mock.side_effect = register_call
b_mock.side_effect = register_call
c_mock.side_effect = register_call
Run Code Online (Sandbox Code Playgroud)
但是这只能给我一些嘲笑的诅咒,而不是实际的嘲笑.我可以添加更多逻辑:
# tests.py
from …Run Code Online (Sandbox Code Playgroud) python ×10
python-mock ×10
mocking ×8
unit-testing ×6
function ×1
nose ×1
python-2.7 ×1
testing ×1