看板 Python 關於我們 聯絡資訊
各位高人好,本人碰到一個很難解的問題 我在mainFunction.py的main()中用with.open分別讀取file1和file2兩個檔 亦即main()當中有以下code: with open("path/to/file1", rb) as f1: print(f1.read()) with open("path/to/file2", rb) as f2: print(f2.read()) sys.exit(0) 我想用mock來給予file1和file2的內容,但沒有測試成功。 但stackoverflow爬了很多文依然無解,以下為我的unit test code: ============================================================================== from mainFunction import main from unittest import mock class MyUnitTest(unittest.TestCase): def test_main(self): mocker = mock.Mock() mocker.side_effect = ["read from f1", "read from f2"] with self.assertRaises(SystemExit) as cm, mock.patch('builtins.open', mock.mock_open(read_data=mocker())) as mock_files: mainFunction.main() assert mock_files.call_arg_list = [mock.call("path/to/file1","rb"), mock.call("path/to/file2", "rb")] self.assertEqual(cm.exception, 0) ============================================================================== 然而我發現 with open("path/to/file2", rb) as f2: print(f2.read()) 所讀取到的值仍舊是"read from f1", 而非"read from f2" 我也嘗試過此頁面中 Chris Collett 的做法,但沒有成功 https://tinyurl.com/dwxbd4pu 不知怎麼進行下一步,先感謝各位願意看完。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 47.187.207.248 (美國) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1674032490.A.4B7.html
lycantrope: 有mock_open可以用 01/18 17:57
VivianAnn: 我的code就有用mock_open,但試不出想要的功能 01/19 16:03
Update一下,我改用side_effect,with open是沒有問題了,但出現了其它問題: with self.assertRaises(SystemExit) as cm, mock.patch("builtins.open", side_effect = [ mock.mock_open(read_data="read from f1").return_value, mock.mock_open(read_data="read from f2").return_value ]) as mock_files: mainFunction.main() mock_file.return_value.__enter__().read.assert_called_once_with("read from f1") self.assertEqual(cm.exception, 0) AssertionError: Expect 'read' to be called once. Called 0 times 我另外嘗試將file1改成用寫入而非讀取: with open("path/to/file1", "w+"): print("message for file1") 並執行 mock_files.return_value.__enter__().write.assert_called_once_with("message for f2") 依然得到類似的錯誤 AssertionError: Expect 'write' to be called once. Called 0 times 求解,拜託了! ※ 編輯: VivianAnn (47.187.207.248 美國), 01/19/2023 16:39:33
lycantrope: 類似這樣 https://pastebin.com/4r6nF2t2 01/19 22:52
VivianAnn: 非常感謝,不過本人還有幾個比較...難搞的問題 01/20 16:56
lycantrope大我問一下,如果with open中有encoding的話要如何mock? 比如: with open("path/to/file1", "w+", encoding="utf-8") as f1: f1.write("message for file1") 我用您提供的方法會出現 TypeError: <lambda>() got an unexpected keyword argument 'encoding' 另外請問要如何用mock_files確認文字是否有寫入或讀取 mock_files.return_value.__enter__().write.assert_called_once_with("message for file1") 依然行不通,實在不能理解 麻煩了,謝謝 另外其實我有點好奇l大你的方法參考哪裡的資料得來的,本人覺得寫unit test有時容易卡關 ※ 編輯: VivianAnn (47.187.207.248 美國), 01/20/2023 17:23:46
lycantrope: lambda 改成 *arg, **kwargs 但這樣寫只是邪門歪道 01/20 20:24
lycantrope: 拆成多個function來測才是正途吧.. 01/20 20:24
s860134: 問個蠢問題, 為何不用sample file? 02/01 01:46