【问题标题】:How to set "return_value" once for TestCase. Python. Django如何为 TestCase 设置一次“return_value”。 Python。姜戈
【发布时间】:2019-01-13 14:12:54
【问题描述】:

这是示例测试:

import a
import b
import c

import mock
from django.test import TestCase

@mock.patch.object(a, "method_a")
@mock.patch.object(b, "method_b")
@mock.patch.object(c, "method_c")
class SomeTestCase(TestCase):

    def setUp(self):
        # I want to set mock_method_a.return_value = 1 once here (or not here, but once)
        pass

    def test_one(self, mock_method_a, mock_method_b, mock_method_c):
        mock_method_a.return_value = 1
        mock_method_b.return_value = 2
        pass  # some test stuff

    def test_two(self, mock_method_a, mock_method_b, mock_method_c):
        mock_method_a.return_value = 1
        mock_method_b.return_value = 2
        pass  # some test stuff

    def test_three(self, mock_method_a, mock_method_b, mock_method_c):
        mock_method_a.return_value = 1
        mock_method_b.return_value = 2
        pass  # some test stuff


问题
如何避免在 TestCase 的每个测试中设置“return_value”的重复代码?

我期待“setUp”方法或类似的东西。
可能吗?

PS:mock版mock==1.3.0,django版Django==1.8.4

【问题讨论】:

  • 为什么在setUp?实际上,您也可以在创建补丁时设置返回值。

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


【解决方案1】:

您可以在@mock.patch.object() 装饰器中设置return_value

@mock.patch.object(c, "method_c", return_value=3)
@mock.patch.object(b, "method_b", return_value=2)
@mock.patch.object(a, "method_a", return_value=1)
class SomeTestCase(TestCase):
    def test_one(self, mock_method_a, mock_method_b, mock_method_c):
        # do test stuff, return values have been set

    def test_two(self, mock_method_a, mock_method_b, mock_method_c):
        # do test stuff, return values have been set

    def test_three(self, mock_method_a, mock_method_b, mock_method_c):
        # do test stuff, return values have been set

(注意:当使用@mock.patch 进行装饰时,装饰器是从下往上应用的,因此要将mock_method_a 作为第一个参数传入,您需要将装饰器放在最接近类定义的位置)。

mock.patch.object()return_value 关键字参数被传递给 MagicMock() 构造函数。见mock.patch.object() documentation

patch() 一样,patch.object() 采用任意关键字参数来配置它创建的模拟对象。

还有mock.Mock documentation

Mock 采用几个可选参数来指定Mock 对象的行为:

  • [...]

  • return_value:调用mock时返回的值。默认情况下,这是一个新的Mock(在首次访问时创建)。请参阅return_value 属性。

如果您还想避免在测试用例之外设置模拟,或者不喜欢每个测试函数的附加参数,那么您也可以创建 patchers em> 在setUp 方法中,然后在测试结束时通过unittest.TestCase.addCleanup() method 注册回调再次将其删除。

通过调用the patcher.start() methods 为每个测试应用修补程序,这将返回新的模拟对象:

class SomeTestCase(TestCase):    
    def setUp(self):
        patcher_method_a = mock.patch.object(a, "method_a")
        self.mock_method_a = patcher_method_a.start()
        self.mock_method_a.return_value = 1

        patcher_method_b = mock.patch.object(b, "method_b")
        self.mock_method_b = patcher_method_b.start()
        self.mock_method_b.return_value = 2

        patcher_method_c = mock.patch.object(c, "method_c")
        self.mock_method_c = patcher_method_c.start()
        self.mock_method_c.return_value = 3

        # when the test is done, stop **all** patchers
        self.addCleanup(mock.patch.stopall)

    def test_one(self):
        # use self.mock_method_a, etc.

    def test_two(self, mock_method_a, mock_method_b, mock_method_c):
        # use self.mock_method_a, etc.

    def test_three(self, mock_method_a, mock_method_b, mock_method_c):
        # use self.mock_method_a, etc.

请注意,mock.patch.stopall() 方法将停止所有已启动的模拟修补程序。您还可以传递每个修补程序的.stop 属性:

self.addCleanup(patcher_method_a.stop)
self.addCleanup(patcher_method_b.stop)
self.addCleanup(patcher_method_c.stop)

如果你必须创建很多这样的设置,你可以创建一个辅助函数来处理重复的部分:

def setup_object_patch(testcase, object, target, return_value, attrname=None):
    patcher = mock.patch.object(object, target)
    mock = patcher.start()
    mock.return_value = return_value
    setattr(testcase, attrname or f'mock_{target}', mock)
    testcase.addCleanup(patcher.stop)

也许在一个映射的循环中使用它:

def setUp(self):
    mocks = {
        # attribute name on test -> object, target, return_value
        'mock_method_a': (a, 'method_a', 1),
        'mock_method_b': (b, 'method_b', 2),
        'mock_method_c': (c, 'method_c', 3),
    }
    for attrname, params in mocks.items():
        setup_object_patch(*params, attrname=attrname)

TestCase.setUp() 方法中的patcher.start() 方法使继承 更容易使用,其中一个基本测试用例用作多个测试用例的基础,这些测试用例都使用相同的共享模拟设置.

【讨论】:

  • 补丁装饰器的顺序是a b c,mock方法理论上应该是mock_c, mock_b, mock_a的顺序吧?
  • @Gang:是的,请注意 OP 在他们的示例中也犯了这个错误。我会纠正的。
猜你喜欢
  • 2021-12-23
  • 2020-08-17
  • 2015-07-15
  • 2020-01-16
  • 2022-06-24
  • 2018-01-23
  • 2011-01-27
  • 2016-01-06
  • 2014-06-02
相关资源
最近更新 更多