首页 > 解决方案 > 如何正确使用 django 测试用例的 addCleanup 方法?

问题描述

我有一个 APITestCase 的测试子类,我使用类方法 setUpTestData 为我的测试和一些模拟创建数据。基本上我想做的是运行 mock.patch.stopall (如下所示),但它不起作用。

我的实现基于这个答案,我正在使用:Django v2.2.4 和 djangorestframework v3.10.2

import mock
from rest_framework.test import APITestCase


class FooTest(APITestCase):

    @classmethod
    def setUpTestData(cls):
        patcher_one = mock.patch('route.one')
        mock_route_one = patcher_one.start()

        patcher_two = mock.patch('route.two')
        mock_route_one = patcher_two.start()

        cls.addCleanup(mock.patch.stopall)

        # etc

        super(FooTest, cls).setUpTestData()

使用此代码运行测试时,我得到:

TypeError: addCleanup() missing 1 required positional argument: 'function'

所以我将 addCleanup 调用编辑为:

cls.addCleanup(function=mock.patch.stopall)

但我得到以下信息:

TypeError: addCleanup() missing 1 required positional argument: 'self'

编辑到:

cls.addCleanup(cls, function=mock.patch.stopall)

我明白了

AttributeError: type object 'FooTest' has no attribute '_cleanups'

在这一点上,我有点失落。

我正在使用的解决方法是在 tearDownClass 方法中执行此操作:

@classmethod
def tearDownClass(cls):
    mock.patch.stopall()

但我想将所有测试逻辑集中在 setUpTestData 方法中。

有人看到我在哪里搞砸了吗?

标签: djangounit-testingtestingdjango-rest-frameworkmocking

解决方案


没有实例就不能调用实例方法。Django 的 setUpTestData 是一个类方法。

addCleanUp 的代码(Django 子类 Unittest.TestCase):

def addCleanup(self, function, *args, **kwargs):
    """Add a function, with arguments, to be called when the test is
    completed. Functions added are called on a LIFO basis and are
    called after tearDown on test failure or success.

    Cleanup items are called even if setUp fails (unlike tearDown)."""
    self._cleanups.append((function, args, kwargs))

你应该做的是将你的模拟移动到设置方法。首先,您应该调用 self.addcleanup(patch.stopall) 以确保即使 setup 方法中发生错误(有时可能)也会停止模拟,然后开始模拟。这在 python doc's here中有解释。

以下代码应与此类似:

class FooTestCase(TestCase):
    def setUp(self):
        super().setUp()
        self.addCleanup(patch.stopall)
        patch('route.one').start()

推荐阅读