代码之家  ›  专栏  ›  技术社区  ›  Mark

断言\调用了\失败,错误消息为空

  •  -1
  • Mark  · 技术社区  · 6 年前

    我正在尝试学习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: 
    

    我对我该怎么做有点不确定,所以我不知道这是怎么回事。为什么这个测试仍然失败,为什么我得到一个空错误?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Alex Hall    6 年前

    删除首字母 assert 只写:

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

    assert_called_with 可能是返回一些不稳定的东西 None assert None 提出一个 AssertionError .