简体   繁体   English

魔术模拟 assert_Called_once 与 assert_Called_once_with 奇怪的行为

[英]Magic mock assert_called_once vs assert_called_once_with weird behaviour

I am noticing a weird behavior with assert_called_once and assert_called_once_with in python.我注意到在 python 中使用assert_called_onceassert_called_once_with有一个奇怪的行为。 This is my real simple test:这是我真正的简单测试:

File module/a.py文件模块/a.py

from .b import B

class A(object):
    def __init__(self):
        self.b = B("hi")

    def call_b_hello(self):
        print(self.b.hello())

File module/b.py文件模块/b.py

class B(object):
    def __init__(self, string):
        print("created B")
        self.string = string;

    def hello(self):
        return self.string

These are my tests:这些是我的测试:

import unittest
from mock import patch
from module.a import A    

class MCVETests(unittest.TestCase):
    @patch('module.a.B')   
    def testAcallBwithMockPassCorrect(self, b1):
        a = A()
        b1.assert_called_once_with("hi")
        a.call_b_hello()
        a.b.hello.assert_called_once()

    @patch('module.a.B')
    def testAcallBwithMockPassCorrectWith(self, b1):
        a = A()
        b1.assert_called_once_with("hi")
        a.call_b_hello()
        a.b.hello.assert_called_once_with()

    @patch('module.a.B')
    def testAcallBwithMockFailCorrectWith(self, b1):
        a = A()
        b1.assert_called_once_with("hi")
        a.b.hello.assert_called_once_with()

    @patch('module.a.B')
    def testAcallBwithMockPassWrong(self, b1):
        a = A()
        b1.assert_called_once_with("hi")
        a.b.hello.assert_called_once()

if __name__ == '__main__':
    unittest.main()

My problem as stated in the name of the function is:我在函数名称中提到的问题是:

  • Test 1 passes correctly测试 1 正确通过
  • Test 2 passes correctly测试 2 正确通过
  • Test 3 fails correctly (I've removed the call to b)测试 3 正确失败(我已删除对 b 的调用)
  • Test 4 passes I am not sure why.测试 4 通过我不知道为什么。

Am I doing something wrong?难道我做错了什么? I am not sure but reading the documentation docs python :我不确定,但阅读文档docs python

assert_called_once(*args, **kwargs) assert_Called_once(*args, **kwargs)

Assert that the mock was called exactly once.断言模拟只被调用了一次。

This is old, but for others landing here...这是旧的,但对于其他登陆这里的人......

For python < 3.6, assert_called_once isn't a thing and so you're actually making a mocked function call which doesn't error对于 python < 3.6, assert_called_once不是一回事,所以你实际上是在做一个不会出错的assert_called_once函数调用

Please see: http://engineroom.trackmaven.com/blog/mocking-mistakes/请参阅: http : //engineroom.trackmaven.com/blog/mocking-mistakes/

You can check the call count instead.您可以改为检查呼叫计数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为模拟 assert_call_once_with() 指定“类 Foo 的任何实例”? - Specifying "any instance of class Foo" for mock assert_called_once_with()? 模拟:assert_called_once_with一个numpy数组作为参数 - Mock: assert_called_once_with a numpy array as argument 如何在python 3.6下获得Mock()。assert_Called_once类似行为? - How to get Mock().assert_called_once like behavior below python 3.6? pytest:unittest错误对象没有属性&#39;assert_called_once_with - pytest: unittest error object has no attribute 'assert_called_once_with Assert_call_once_with 需要检查调用中的实例是否具有相同的信息 - Assert_called_once_with need to check if the instances in the call are with the same info &#39;function&#39;对象没有属性&#39;assert_called_once_with&#39; - 'function' object has no attribute 'assert_called_once_with' Python MagicMock assert_call_once_with不考虑参数? - Python MagicMock assert_called_once_with not taking into account arguments? 在调用参数中使用 datetime.now() 断言调用_once_with()? - assert_called_once_with() with datetime.now() in the call parameter? 我有什么可用于方法assert_Called_once_with的值,该值可以匹配任何内容? - Is there any value I can use for method assert_called_once_with, which would match anything? Python 3.6初学者:为什么为assert_Called_once_with获得一个空白的AssertionError(单元测试和模拟) - Python 3.6 Beginner: Why getting a blank AssertionError for assert_called_once_with (unit test & mocking)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM