我无法模拟接受** kwargs的函数。场景是我有ClassA(在我的具体情况下是我没写的东西),它有一个** kwargs的功能。 ClassB,它有一个ClassA实例,并调用** kwargs函数。我想通过模拟对A类功能的调用来测试ClassB。
这是我到目前为止所尝试的内容,在我的两次尝试中,我最终得到了一个TypeError。有没有办法做到这一点?我应该重新考虑另一个方面吗?
import unittest
#a class i have no control over. Has a function accepting **kwargs
class ClassA(object):
def classFunctionAcceptingKwargs(self, **kwargs):
return kwargs["a"] + kwargs["b"]
# a mock of the above class
class Mock_ClassA(object):
def __init__(self):
self.mockclassFunctionAcceptingKwargs = lambda **kwargs: None
def classFunctionAcceptingKwargs(self, **kwargs):
#FAILS: TypeError: mockFunctionAcceptingKwargs() takes exactly 0 arguments (1 given)
return self.mockclassFunctionAcceptingKwargs(kwargs)
#ALSO FAILS: TypeError: mockFunctionAcceptingKwargs() argument after ** must be a mapping, not set
#return self.mockclassFunctionAcceptingKwargs(**{kwargs["a"] + kwargs["b"]})
#class B calls the class A kwargs but exposes a function with a dict
class ClassB(object):
def __init__(self, classA):
self.classA = classA
def doSomething(self, dict):
return self.classA.classFunctionAcceptingKwargs(**dict)
class TestClassA(unittest.TestCase):
def runTest(self):
a = ClassA()
result = a.classFunctionAcceptingKwargs(**{"a":1, "b": 2})
self.assertEqual(result, 3)
class TestClassB(unittest.TestCase):
def runTest(self):
mock = Mock_ClassA()
def mockFunctionAcceptingKwargs(**kwargs):
self.assertEqual(kwargs["a"], 1)
self.assertEqual(kwargs["b"], 2)
mock.mockclassFunctionAcceptingKwargs = mockFunctionAcceptingKwargs
b = ClassB(mock)
b.doSomething({"a": 1, "b": 2})
堆栈追踪:
Test Name: TestClassB
Test Outcome: Failed
Result StandardError:
======================================================================
ERROR: runTest (module1.TestClassB)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\test\module1.py", line 49, in runTest
b.doSomething({"a": 1, "b": 2})
File "C:\test\module1.py", line 28, in doSomething
return self.classA.classFunctionAcceptingKwargs(**dict)
File "C:\test\module1.py", line 18, in classFunctionAcceptingKwargs
return self.mockclassFunctionAcceptingKwargs(kwargs)
TypeError: mockFunctionAcceptingKwargs() takes exactly 0 arguments (1 given)
----------------------------------------------------------------------
Ran 1 test in 12.873s
FAILED (errors=1)
答案 0 :(得分:2)
我不确定你为什么要打另一个功能。但是,如果必须具有实例属性mockclassFunctionAcceptingKwargs
功能,那么只需使用kwargs
传递**kwargs
字典:
class Mock_ClassA(object):
# ...
def classFunctionAcceptingKwargs(self, **kwargs):
return self.mockclassFunctionAcceptingKwargs(**kwargs)
如果你需要classFunctionAcceptingKwargs
存在,你根本不需要调用那个lambda:
class Mock_ClassA(object):
def __init__(self, mock_result):
self.mock_result = mock_result
def classFunctionAcceptingKwargs(self, **kwargs):
self.called_with = kwargs
return self.mock_result
然后只传递模拟的返回值,无论你需要传递回ClassB
进行测试,然后你也可以检查是否传递了正确的值:
mock = Mock_ClassA(3) # to return 3 back to the caller
b = ClassB(mock)
b.doSomething({"a": 1, "b": 2})
self.assertEqual(mock.called_with, {'a': 1, 'b': 2})
您可能希望使用unittest.mock
library来构建模拟对象以进行传入(在Python 3中可用,并且可以安装用于Python 2的backport)。它会让你创建一个模拟的ClassA
,然后使用API来测试模拟是否以预期的方式使用:
try:
# Python 3
from unittest import mock
except ImportError:
# Python 2, backport
import mock
class TestClassB(unittest.TestCase):
def runTest(self):
mockA = mock.Mock(spec=ClassA) # only accept attributes ClassA also has
mockA.classFunctionAcceptingKwargs.return_value = 3 # or whatever else you want it to return
b = ClassB(mockA)
b.doSomething({"a": 1, "b": 2})
mockA.classFunctionAcceptingKwargs.assert_called_once_with(a=1, b=2)
使用unittest.mock
作为模拟层进行演示:
>>> from unittest import mock
>>> class ClassA(object):
... def classFunctionAcceptingKwargs(self, **kwargs):
... return kwargs["a"] + kwargs["b"]
...
>>> class ClassB(object):
... def __init__(self, classA):
... self.classA = classA
... def doSomething(self, dict):
... return self.classA.classFunctionAcceptingKwargs(**dict)
...
>>> mockA = mock.Mock(spec=ClassA)
>>> mockA.classFunctionAcceptingKwargs.return_value = 3
>>> b = ClassB(mockA)
>>> b.doSomething({"a": 1, "b": 2})
3
>>> mockA.classFunctionAcceptingKwargs.assert_called_once_with(a=1, b=2) # passes, no exception raised
>>> mockA.mock_calls
[call.classFunctionAcceptingKwargs(a=1, b=2)]