Python 板


LINE

各位高人好,本人碰到一個很難解的問題 我在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://webptt.com/m.aspx?n=bbs/Python/M.1674032490.A.4B7.html
1F:→ lycantrope: 有mock_open可以用 01/18 17:57
2F:→ 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
3F:推 lycantrope: 類似這樣 https://pastebin.com/4r6nF2t2 01/19 22:52
4F:→ 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
5F:推 lycantrope: lambda 改成 *arg, **kwargs 但這樣寫只是邪門歪道 01/20 20:24
6F:→ lycantrope: 拆成多個function來測才是正途吧.. 01/20 20:24
7F:→ s860134: 問個蠢問題, 為何不用sample file? 02/01 01:46







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP