簡體   English   中英

python mock assert_call_with

[英]python mock assert_called_with

我試圖理解模擬中的assert_called_with但我編寫的代碼引發了一些錯誤。

import os
import twitter

URL = "http://test.com"

def tweet(api, message):
    if len(message) > 40:
        message = message.strip("?.,.,.")

    status = api.PostUpdate(message)
    return status

def main():
    api = twitter.Api(consumer_key=''
                    ,consumer_secret='')
    msg = 'This is test message'
    tweet(api, msg)

if __name__ == '__main__':
    main()

單元測試

import unittest
from mock import Mock
import test

class TweetTest(unittest.TestCase):
    def test_example(self):
        mock_twitter = Mock()        
        test.tweet(mock_twitter,'msg')        
        mock_twitter.PostUpdate.assert_called_with('message')        

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

我想了解assert_called_with在這里做什么?

根據 python 文檔https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.assert_Called_with

'此方法是斷言以特定方式進行調用的便捷方式'

所以它測試參數是否以正確的方式使用。

關於您收到的錯誤,我認為您傳遞的參數是錯誤的。 它必須是這樣的:

mock_twitter.PostUpdate.assert_called_with(message='msg')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM