擅长:python、mysql、java
<p>你应该嘲笑你测试的地方。所以,如果您测试的是<code>cleaner</code>类(我建议您在这里使用<a href="https://www.python.org/dev/peps/pep-0008/" rel="nofollow">PEP8</a>标准,并使其成为<code>Cleaner</code>),那么您需要模拟测试的位置。所以,你的补丁实际上应该是沿着以下几行的<em>东西</em>:</p>
<pre><code>class SomeTest(Unittest.TestCase):
@mock.patch('path.to.Cleaner.boto3.client', return_value=Mock())
def setUp(self, boto_client_mock):
self.cleaner_client = boto_client_mock.return_value
def your_test(self):
# call the method you are looking to test here
# simple test to check that the method you are looking to mock was called
self.cleaner_client.desribe_tags.assert_called_with()
</code></pre>
<p>我建议你通读一下<a href="http://www.voidspace.org.uk/python/mock/index.html" rel="nofollow">mocking documentation</a>,里面有很多例子可以做你想做的事情</p>