[問題] 如何用unittest.mock 測試多個 with open 的結果

看板Python作者 (薇薇安安)時間1年前 (2023/01/18 17:01), 1年前編輯推噓2(204)
留言6則, 2人參與, 1年前最新討論串1/1
各位高人好,本人碰到一個很難解的問題 我在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

01/18 17:57, 1年前 , 1F
有mock_open可以用
01/18 17:57, 1F

01/19 16:03, 1年前 , 2F
我的code就有用mock_open,但試不出想要的功能
01/19 16:03, 2F
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

01/19 22:52, 1年前 , 3F
01/19 22:52, 3F

01/20 16:56, 1年前 , 4F
非常感謝,不過本人還有幾個比較...難搞的問題
01/20 16:56, 4F
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

01/20 20:24, 1年前 , 5F
lambda 改成 *arg, **kwargs 但這樣寫只是邪門歪道
01/20 20:24, 5F

01/20 20:24, 1年前 , 6F
拆成多個function來測才是正途吧..
01/20 20:24, 6F
文章代碼(AID): #1ZnxLgIt (Python)
文章代碼(AID): #1ZnxLgIt (Python)