1. 程式人生 > 其它 >pytest基礎

pytest基礎

pytest斷言 1. == 直接對兩端的值進行判斷是否一致 1==1 2.assert in 判斷值是否在正確範圍 def test_jia(self): a='hello' b='a' assert b in a 3.assert 判斷值是否正確 def test_jia(self): a='hello' b='a' assert 1==1,'值不正確' pytest 引數 普通執行用例時通過是無法打印出print的,這個時候需要新增引數打印出日誌 -v 列印執行日誌資訊 -q 列印執行日誌(簡略) -S 控制檯輸入結果 -k 執行包含關鍵字的用例 pytest -v-k'one' 執行用列名稱包含one的用例 pytest -v-k' not one' 執行用列不名稱包含one的用例 pytest mark標記 自定義標記 在要標記的方法前輸入 @pytest.mark.jia @pytest.mark.jia def test_jia(self): a='hello' b='e' assert b in a 在執行的時候,pytest -m'jia' 然而在使用自定義標籤時,執行會有警告 這是因為我麼你自定義的標記不被知道是用於什麼,我們需要自己新建一個檔案 pytest.ini 在專案下新建一個pytest.ini的配置檔案,裡面如下配置即可。markers=後面是標籤名,如有多個,第二個開始要換行,且要縮排,不然還是會warning [pytest] markers=jia pytest用例執行後的幾種狀態 passed 用例通過 failed 用例不通過 error 用列編寫問題 xfail [email protected] 預期失敗 def test_xfailed(): pytest.xfail(reason='該功能正在開發') print('改功能正在除錯') skip 跳過用例不執行 @pytest.mark.skip(reason='此階段不執行') def test_skip(self): print('此用例跳過') skipif 有條件的跳過 a=0 @pytest.mark.skipif(a<1,reason='當前a小於1不執行用例') def test_skipif(self): print('滿足條件不執行') 測試報告 pytest-html 安裝外掛 pip install pytest-html 執行命令 pytest --html=生成的路徑