Python 断言调用的\u失败,错误消息为空
我正在尝试学习python的测试工具,并设置了我认为非常简单的Python 断言调用的\u失败,错误消息为空,python,mocking,python-unittest,Python,Mocking,Python Unittest,我正在尝试学习python的测试工具,并设置了我认为非常简单的@patch() 我制作了一个非常简单的函数,它什么都不做(但也不会引发错误): 然后,我修补urlopen,并使用以下命令调用我的函数: @patch('urllib.request.urlopen') def test(MockClass1): getURL() assert MockClass1.assert_called_with('test') test() 正如我所期望的那样,由于断言错误,该操作失败:
@patch()
我制作了一个非常简单的函数,它什么都不做(但也不会引发错误):
然后,我修补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:
我有点不确定我该怎么做,所以我不确定这里发生了什么。为什么这个测试仍然失败,为什么我得到一个空错误?删除初始的
断言,只需写:
MockClass1.assert_called_with('https://example.com')
assert\u调用_with
返回错误的内容,可能是None
,而assert None
会引发AssertionError
删除首字母assert
,只需写下:
MockClass1.assert_called_with('https://example.com')
assert\u调用_with
返回错误的内容,可能是None
,而assert None
会引发AssertionError