pytest:为每个测试功能设置一个模拟

时间:2019-02-28 10:54:17

标签: python unit-testing mocking pytest python-unittest

为python项目创建单元测试,我们正在达到这种“模板”

from unittest import TestCase
from unittest.mock import patch, Mock

@patch('......dependency1')
@patch('......dependency2')
@patch('......dependencyn')
class MyTest(TestCase):

  def test_1(self, mock1, mock2, mockn):
      # setup mock1 & mock2...
      # call the subject case 1
      # assert response and/or interactions with mock1 and mock2...

  def test_2(self, mock1, mock2, mockn):
      # setup mock1 & mock2...
      # call the subject case 2
      # assert response and/or interactions with mock1 and mock2...

问题是,有时“ setup”部分在某些测试案例中是重复的,因此我想将配置提取到setUp()方法中,例如,以下是伪代码:

def setUp(self):
  mock1.foo.return_value = 'xxx'
  mock2.goo.side_effect = [ ... ]

def test_1(self, mock1, mock2, mockn):
  # default setup is perfect for this test

def test_2(self, mock1, mock2, mockn):
  # this time I need...
  mock2.goo.side_effect = [ ... ]

有可能实现这个想法吗?

1 个答案:

答案 0 :(得分:1)

pytestunittest都提供了您所要求的可能性,并且在相应的文档中以示例说明了这两个功能:在{{1}中查找fixture }文档和pytest文档中的setup

但是,实际上,在实践中很快使用这些功能会导致创建无法读取的测试代码的趋势。它有两种形式,一种是共享的夹具设置变得太大(太笼统),这使读者难以理解特定测试用例的实际含义。第二个是,测试代码不再是独立的,魔术似乎在外面发生了。 Meszaros在上述情况下将产生的测试气味称为“模糊测试”,称为“常规装置”和“神秘访客”。

我的建议是,希望使用辅助功能/方法,您应该在每个测试中明确调用它们。您可以使用其中的几种名称,并为其指定描述性名称,从而使您的测试代码可读,而无需读者先搜索整个文件以查找任何“自动的”东西。在您的示例中,测试可能如下所示:

unittest
相关问题