Python unittest:来模拟.patch()还是只用Mock替换方法?

克里斯蒂安·鲁西尔德

在用Python编写单元测试时嘲笑类或方法时,为什么需要使用@patch装饰器?我可以用没有任何补丁注释的Mock对象替换该方法。

例子:

class TestFoobar(unittest.TestCase):
def setUp(self):
    self.foobar = FooBar()

# 1) With patch decorator:

@patch.object(FooBar, "_get_bar")
@patch.object(FooBar, "_get_foo")
def test_get_foobar_with_patch(self, mock_get_foo, mock_get_bar):
    mock_get_bar.return_value = "bar1"
    mock_get_foo.return_value = "foo1"

    actual = self.foobar.get_foobar()

    self.assertEqual("foo1bar1", actual)

# 2) Just replacing the real methods with Mock with proper return_value:

def test_get_foobar_with_replacement(self):
    self.foobar._get_foo = Mock(return_value="foo2")
    self.foobar._get_bar = Mock(return_value="bar2")

    actual = self.foobar.get_foobar()

    self.assertEqual("foo2bar2", actual)

有人能举例说明补丁装饰器好而替换不好吗?

我们一直与团队一起使用补丁装饰器,但是在阅读了这篇评论后,我想到也许不需要补丁装饰器就可以编写看起来更好的代码。

我知道补丁是临时的,因此在某些情况下,不使用补丁装饰器而用模拟代替方法是危险的吗?难道在一种测试方法中替换对象会影响下一测试方法的结果吗?

我试图证明这一点,但是空了:两个测试都通过了下一个代码:

def test_get_foobar_with_replacement(self):
    self.foobar._get_foo = Mock(return_value="foo2")
    self.foobar._get_bar = Mock(return_value="bar2")

    actual = self.foobar.get_foobar()

    self.assertIsInstance(self.foobar._get_bar, Mock)
    self.assertIsInstance(self.foobar._get_foo, Mock)
    self.assertEqual("foo2bar2", actual)

def test_get_foobar_with_real_methods(self):

    actual = self.foobar.get_foobar()

    self.assertNotIsInstance(self.foobar._get_bar, Mock)
    self.assertNotIsInstance(self.foobar._get_foo, Mock)
    self.assertIsInstance(self.foobar._get_bar, types.MethodType)
    self.assertIsInstance(self.foobar._get_foo, types.MethodType)
    self.assertEqual("foobar", actual)

完整的源代码(Python 3.3):dropbox.com/s/t8bewsdaalzrgke/test_foobar.py?dl=0

切普纳

patch.object测试方法返回后,将修补的项目恢复到原始状态。如果您自己对对象进行猴子修补,则如果该对象将在其他测试中使用,则需要恢复原始值。

在您的两个示例中,您实际上是在修补两个不同的事物。您对呼叫patch.object补丁 FooBar,而你的猴子补丁修补的特定实例FooBar

如果每次都会从头开始创建对象,则还原原始对象并不重要。(您没有显示它,但是我假设self.foobar它是在一个setUp方法中创建的,因此即使您替换了它的_get_foo方法,也不会在多个测试中重用该特定对象。)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python unittest 模拟类和类方法

来自分类Dev

如何在Python 3.5中使用unittest.mock模拟导入的库方法?

来自分类Dev

Python unittest模拟:是否可以在测试时模拟方法的默认参数的值?

来自分类Dev

Python unittest模拟API密钥

来自分类Dev

Python unittest模拟...模拟模块语句

来自分类Dev

Python unittest模拟...模拟模块语句

来自分类Dev

Python:如何模拟SQLAlchemy事件处理程序(使用mock.unittest)

来自分类Dev

Python:如何模拟SQLAlchemy事件处理程序(使用mock.unittest)

来自分类Dev

使用python模拟来计数方法调用的数量

来自分类Dev

我可以在python中使用`mock`来模拟具有指定参数的方法吗?

来自分类Dev

在 python unittest 中创建模拟对象

来自分类Dev

python的unittest.mock.patch是否会改变全局状态?

来自分类Dev

python的`unittest.mock.patch`是否会改变全局状态?

来自分类Dev

在不使用模拟的情况下检查是否已在Python unittest中调用函数的规范方法是什么?

来自分类Dev

在python 2的unittest模块中模拟raw_input()

来自分类Dev

Python unittest模拟运行程序两次

来自分类Dev

模拟选择性文件写入python unittest

来自分类Dev

使用unittest在多输入函数中模拟Python输入

来自分类Dev

python unittest中的Mock.patch可能适用于两条路径

来自分类Dev

有没有一种简单的方法来模拟 python/Django 中的许多静态方法?

来自分类Dev

来自模拟的测试方法时出现UnitTest -issue

来自分类Dev

无法使用Python的mock.patch模拟urllib2.urlopen

来自分类Dev

python如何模拟方法?

来自分类Dev

使用Python模拟来监视对现有对象的调用

来自分类Dev

在python中:如何使用描述符来模拟属性

来自分类Dev

Python unittest和模拟:检查是否调用了函数,然后停止测试

来自分类Dev

在 Python 2.7 中通过 unittest.TestCase 模拟 datetime.now()

来自分类Dev

python mock.mock_reset()返回模拟,而不是重置模拟

来自分类Dev

Pytest可以使用旧的模拟程序,但不能使用unittest.mock

Related 相关文章

  1. 1

    Python unittest 模拟类和类方法

  2. 2

    如何在Python 3.5中使用unittest.mock模拟导入的库方法?

  3. 3

    Python unittest模拟:是否可以在测试时模拟方法的默认参数的值?

  4. 4

    Python unittest模拟API密钥

  5. 5

    Python unittest模拟...模拟模块语句

  6. 6

    Python unittest模拟...模拟模块语句

  7. 7

    Python:如何模拟SQLAlchemy事件处理程序(使用mock.unittest)

  8. 8

    Python:如何模拟SQLAlchemy事件处理程序(使用mock.unittest)

  9. 9

    使用python模拟来计数方法调用的数量

  10. 10

    我可以在python中使用`mock`来模拟具有指定参数的方法吗?

  11. 11

    在 python unittest 中创建模拟对象

  12. 12

    python的unittest.mock.patch是否会改变全局状态?

  13. 13

    python的`unittest.mock.patch`是否会改变全局状态?

  14. 14

    在不使用模拟的情况下检查是否已在Python unittest中调用函数的规范方法是什么?

  15. 15

    在python 2的unittest模块中模拟raw_input()

  16. 16

    Python unittest模拟运行程序两次

  17. 17

    模拟选择性文件写入python unittest

  18. 18

    使用unittest在多输入函数中模拟Python输入

  19. 19

    python unittest中的Mock.patch可能适用于两条路径

  20. 20

    有没有一种简单的方法来模拟 python/Django 中的许多静态方法?

  21. 21

    来自模拟的测试方法时出现UnitTest -issue

  22. 22

    无法使用Python的mock.patch模拟urllib2.urlopen

  23. 23

    python如何模拟方法?

  24. 24

    使用Python模拟来监视对现有对象的调用

  25. 25

    在python中:如何使用描述符来模拟属性

  26. 26

    Python unittest和模拟:检查是否调用了函数,然后停止测试

  27. 27

    在 Python 2.7 中通过 unittest.TestCase 模拟 datetime.now()

  28. 28

    python mock.mock_reset()返回模拟,而不是重置模拟

  29. 29

    Pytest可以使用旧的模拟程序,但不能使用unittest.mock

热门标签

归档