是否可以编写将模拟装饰器应用于函数的函数级 pytest 夹具?

Is it possible to write a function-level pytest fixture that applies a mock decorator to the function?

在我们的 repo 中,我们有一些对 s3 的调用。我们从不希望这些在测试期间执行,所以我们在每个单独的测试中模拟它们,这很烦人。这是很多重复的代码,如果开发人员忘记在 运行 测试之前编写模拟,可能会很危险。

我想编写一个 pytest fixture,自动将 mock 应用于每个测试函数。也就是说,我想将我的代码更改为如下所示:

test_file.py:

@mock.patch.object(S3Hook, 'write_to_s3')
def test1(_):
    # test some stuff without writing to s3

@mock.patch.object(S3Hook, 'write_to_s3')
def test2(_):
    # test some more stuff without writing to s3

对此:

conftest.py:

@pytest.fixture(scope='function', autouse=True)
def mock_out_s3(request):
    # somehow apply the mock.patch.object decorator to request.function here


test_file.py:

def test1():
    # test some stuff; the mock is automatically applied, so we won't write to s3

def test2():
    # ditto

这可能吗?

一边写这些Unittests。你可以这样做:


Class TestClass(TestCase):

    @classmethod
    def setUpTestData(cls):
        pass

    def tearDown(self):
        self.patcher.stop()

    def setup(self):
       self.patcher = mock.patch(S3Hook, 'write_to_s3')
       mock_apply = self.patcher.start()

    def test1(self):
    # test some stuff; the mock is automatically applied, so we won't write to s3

    def test2(self):
    # ditto


您可以在此处找到有关补丁程序的更多详细信息:https://docs.python.org/3/library/unittest.mock.html#the-patchers

发布我如何让它工作的详细信息(基于 ParthS007 的回答)以帮助将来尝试做同样事情的其他人:

@pytest.fixture(scope='function', autouse=True)
def mock_out_s3(request):
    patcher = mock.patch.object(S3Hook, 'write_to_s3')
    patcher.start()
    request.addfinalizer(patcher.stop)