作者Talent14 (Talent)
看板Python
標題[問題] Pytest問題
時間Sat Feb 20 18:05:21 2021
請問使用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
→ djshen: def pytest_markeval_namespace(config): 02/20 21:15
→ djshen: return {'par': config.getoption('par')} 02/20 21:15
→ djshen: @pytest.mark.skipif('par == 1') 02/20 21:15
→ Talent14: 謝謝d大,我會再研究試試看! 02/20 23:36
→ Talent14: 使用pytest_markeval_namespace已成功,也在pytest6.2.0 02/20 23:48
→ Talent14: 官方文件中查到相關使用方式與說明!非常感謝大大! 02/20 23:48
→ djshen: 也可以參考_pytest.skipping.evaluate_condition 02/21 00:31
→ djshen: 可以看到他eval str的時候用了哪些global variable 02/21 00:32
→ Talent14: 好的,我會再研究研究,謝謝提供建議與資訊! 02/21 08:56