擅长:python、mysql、java
<p>其中一种方法是模拟整个类进行特定的测试,如下所示:</p>
<p>示例:
我有一门课,我想嘲笑。我的模拟类名是MockSomeClass,它将模拟类SomeClass。你知道吗</p>
<pre><code>class MockSomeClass(SomeClass):
'''
Mock Class
'''
def __init__(overiden_parameter):
self.overiden_parameter = overiden_parameter
</code></pre>
<p>因此,在测试期间,您将使用具有重写功能的mock类,而其他函数的行为将保持不变(继承)。你知道吗</p>
<h2>修补</h2>
<pre><code>mock_some_class_obj = MockSomeClass()
@mock.patch('SomeClass', return_value=mock_some_class_obj)
def test1(self, mock_some_class_obj):
'''
Test 1
'''
obj = SomeClass()
</code></pre>
<p><strong>捕获</strong>因此,在代码中,无论何时创建SomeClass的对象,都将返回mock类的对象。在mock类中,您可以添加自己的功能。你知道吗</p>