我的django应用程序中有几个TestCase类.在其中一些中,我模拟了一个函数,它通过使用@ mock.patch修改类来调用外部资源,这非常有用.我的测试套件中的一个TestCase,我们称之为B(),依赖于外部资源,因此我不希望它被模拟,我不添加装饰器.它看起来像这样:
@mock.patch("myapp.external_resource_function", new=mock.MagicMock)
class A(TestCase):
# tests here
class B(TestBase):
# tests here which depend on external_resource_function
Run Code Online (Sandbox Code Playgroud)
当我独立测试B时,事情按预期工作.但是,当我同时运行两个测试时,A首先运行,但该功能仍然在B中模拟出来.我怎么能取消该调用?我已经尝试重新加载模块,但它没有帮助.
补丁有启动和停止方法.根据我从您提供的代码中看到的内容,我将删除装饰器并使用类中链接中的setUp和tearDown方法.
class A(TestCase):
def setUp(self):
self.patcher1 = patch('myapp.external_resource_function', new=mock.MagicMock)
self.MockClass1 = self.patcher1.start()
def tearDown(self):
self.patcher1.stop()
def test_something(self):
...
>>> A('test_something').run()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
970 次 |
最近记录: |