assert_called_with 失败,错误消息为空

assert_called_with failing with empty error message

我正在尝试学习 python 的测试工具,并且已经设置了我认为 @patch() 的非常简单的使用方法。

我做了一个非常简单的函数,它什么都不做(但也不会引发错误):

aULR = "https://example.com"

def getURL():
    with urllib.request.urlopen(aULR) as f:
        pass

然后我修补 urlopen 并调用我的函数:

@patch('urllib.request.urlopen')
def test(MockClass1):
    getURL()
    assert MockClass1.assert_called_with('test')

test()

这如预期的那样失败了,出现了我希望的断言错误:

AssertionError: Expected call: urlopen('test')
Actual call: urlopen('https://example.com')

但是当我在测试中通过正确的 url 时:

@patch('urllib.request.urlopen')
def test(MockClass1):
    getURL()
    assert MockClass1.assert_called_with('https://example.com')

test()

我仍然收到错误,但这次是一个无用的 AssertionError,没有消息:

AssertionError: 

我对我应该如何做这件事有点犹豫,所以我不确定这里发生了什么。为什么这个测试仍然失败,为什么我得到一个空错误?

去掉开头的assert,只写:

MockClass1.assert_called_with('https://example.com')

assert_called_with 正在返回一些虚假的东西,可能是 None,并且 assert None 引发了一个 AssertionError.