[問題] pytest多層呼叫時,missing 1 required p

看板Python作者 (Talent)時間3年前 (2021/03/19 14:47), 編輯推噓0(0027)
留言27則, 2人參與, 3年前最新討論串1/1
在pytest中,自定義的傳入參數想在其他python檔案使用,需要將conftest.py中接收參數的method name當作參數傳入,如程式碼中的common_method(self, test_var) 但如果common_method又會被其他python檔案中的function呼叫,該怎麼處理第一個參數呢? EX: test_demo.py檔案中想要使用CommonModule().common_method(),會需要傳入參數test_var,但common_method中的test_var是為了接收自訂傳入參數 在類似這樣兩層呼叫的方式下,怎麼使用接收進來的參數,才能解決 TypeError: common_module() missing 1 required positional argument: 'test_var' 附上程式碼圖檔,先感謝各位高手大大 https://i.imgur.com/aqymMDO.jpg
----- Sent from JPTT on my iPhone -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.83.131.117 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1616136435.A.E7B.html

03/19 20:21, 3年前 , 1F
你確定問題不是 method 少了個 self?
03/19 20:21, 1F

03/19 20:42, 3年前 , 2F
抱歉,我範例漏打了,但確定補上後,test_case1(self)是
03/19 20:42, 2F

03/19 20:42, 3年前 , 3F
不行的!
03/19 20:42, 3F

03/19 20:42, 3年前 , 4F
抱錯的原因會顯示最後一行的common_module呼叫時缺少參
03/19 20:42, 4F

03/19 20:42, 3年前 , 5F
03/19 20:42, 5F

03/21 17:52, 3年前 , 6F
??? 所以你是想把class CM 當成 fixture?
03/21 17:52, 6F

03/21 22:23, 3年前 , 7F
我是想將pytest指令接收的參數在A method中被使用,但其
03/21 22:23, 7F

03/21 22:23, 3年前 , 8F
他的B method也可以呼叫A method!
03/21 22:23, 8F

03/21 22:23, 3年前 , 9F
(A method是一個共用的方法,可以讓其他method呼叫使用
03/21 22:23, 9F

03/21 22:23, 3年前 , 10F
03/21 22:23, 10F

03/21 22:31, 3年前 , 11F
以例子來說,是將test_var當作fixture, 提供給Common_Mo
03/21 22:31, 11F

03/21 22:31, 3年前 , 12F
dule cls的method使用,然後TestDemo cls的method會需要
03/21 22:31, 12F

03/21 22:31, 3年前 , 13F
呼叫Common_Module cls的method
03/21 22:31, 13F

03/22 19:03, 3年前 , 14F
...如果你 CM 是 test fixture, pytest 沒有也不建議你把 f
03/22 19:03, 14F

03/22 19:03, 3年前 , 15F
ixture 綁在 class 上;如果 CM 是 test cases 的內容,放
03/22 19:03, 15F

03/22 19:03, 3年前 , 16F
在 setup 每次 re-initial(or maybe u are looking: unitte
03/22 19:03, 16F

03/22 19:03, 3年前 , 17F
st?); 要是 CM 是待測物,那顯然你上面應該都看不懂...要
03/22 19:03, 17F

03/22 19:03, 3年前 , 18F
不先去研究看看 test doubles?
03/22 19:03, 18F

03/22 19:29, 3年前 , 19F
CM不是待測物,CM是我拿來做login的動作(例:輸入帳號
03/22 19:29, 19F

03/22 19:29, 3年前 , 20F
密碼,點擊登入按鈕、檢查登入是否成功動作),但不將他
03/22 19:29, 20F

03/22 19:29, 3年前 , 21F
當作一個test case,因為很多test case都會呼叫CM做前置
03/22 19:29, 21F

03/22 19:29, 3年前 , 22F
動作!
03/22 19:29, 22F

03/22 19:29, 3年前 , 23F
但是開啟的url是由pytest命令參數決定,所以有點不知道
03/22 19:29, 23F

03/22 19:29, 3年前 , 24F
該怎麼把這些東西串接起來!
03/22 19:29, 24F

03/22 19:29, 3年前 , 25F
目前想到另一個方式是將url參數接收進來後放到os的環境
03/22 19:29, 25F

03/22 19:29, 3年前 , 26F
變數,在login的時候去取用!
03/22 19:29, 26F

03/22 19:29, 3年前 , 27F
謝謝大大提供的資訊,double test我也會去研究!
03/22 19:29, 27F
文章代碼(AID): #1WL4Zpvx (Python)
文章代碼(AID): #1WL4Zpvx (Python)