[問題] Pytest問題

看板Python作者 (Talent)時間3年前 (2021/02/20 18:05), 編輯推噓0(009)
留言9則, 2人參與, 3年前最新討論串1/1
請問使用python的pytest測試框架, 如何將自訂的pytest指令參數值傳到每個test case檔案中pytest.mark.skipif的條件判斷使用? EX: pytest --par=1, par的值用來判斷所有Test Case是否要Skip的依據 @pytest.mark.skipif(par > '1', reason='pass test1') --> 若par值大於 1 才skip測試案例 PS: 查了一些資料,看到用pytest.config方式取得值,但pytest版本目前使用6.X,pytest沒有config這個attribute (ERROR MSG: AttributeError: module 'pytest' has no attribute 'config') 附上測試程式碼圖片 https://i.imgur.com/kqZmsc2.jpg
https://i.imgur.com/YWcClDR.jpg
先感謝各位大大! ----- Sent from JPTT on my iPhone -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.38.29.74 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1613815523.A.6E2.html

02/20 21:15, 3年前 , 1F
def pytest_markeval_namespace(config):
02/20 21:15, 1F

02/20 21:15, 3年前 , 2F
return {'par': config.getoption('par')}
02/20 21:15, 2F

02/20 21:15, 3年前 , 3F
@pytest.mark.skipif('par == 1')
02/20 21:15, 3F

02/20 23:36, 3年前 , 4F
謝謝d大,我會再研究試試看!
02/20 23:36, 4F

02/20 23:48, 3年前 , 5F
使用pytest_markeval_namespace已成功,也在pytest6.2.0
02/20 23:48, 5F

02/20 23:48, 3年前 , 6F
官方文件中查到相關使用方式與說明!非常感謝大大!
02/20 23:48, 6F

02/21 00:31, 3年前 , 7F
也可以參考_pytest.skipping.evaluate_condition
02/21 00:31, 7F

02/21 00:32, 3年前 , 8F
可以看到他eval str的時候用了哪些global variable
02/21 00:32, 8F

02/21 08:56, 3年前 , 9F
好的,我會再研究研究,謝謝提供建議與資訊!
02/21 08:56, 9F
文章代碼(AID): #1WCDxZRY (Python)