在代碼中執行pytest可以通過main函數
加參數來指定運行規則時,參數需要放在列表或者元祖中
# pytest.main(["--html=report.html"]) # pytest.main(["--collect-only"])#展示所有測試用例 # pytest.main(["-k","11"])#使用指定表達式運行希望運行的用例 # pytest.main(["-v","-k","11"])# 增加-v查看詳細信息 # pytest.main(["-v","-m","run_first"]) """ 使用-m對用例進行標記,用例需注釋@pytest.mark.xxx,將xxx作為參數傳入 使用-m "mark1 and mark2"可以同時選中帶有這兩個標記的所有測試用例。 使用-m "mark1 and not mark2"選中帶喲與mark1的測試用例,而過濾掉帶有mark2的測試用例 使用-m "mark1 or mark2"則選中帶有mark1或者mark2的所有測試用例 """ # pytest.main(["-v","-x"])#-x 遇到錯誤即停止 # pytest.main(["-v","--maxfail=2","--tb=no"])#--maxfail=n 設定最多失敗 n 次即停止 # pytest.main(["-s"])#允許終端運行時輸出某些結果,例如print # pytest.main(["--lf"])#定位失敗的用例 # pytest.main(["--ff"])#定位失敗的用例首先執行,但是正常的用例也會執行 # pytest.main(["-q"])#簡化輸出信息 # pytest.main(["-l"])#打印失敗用例的變量值 # pytest.main(["--tb=short"]) """ --tb=style,選擇失敗回溯信息 short:僅輸出assert一行以及系統判定內容(不顯示上下文) no:不展示回溯信息 line:只是用一行輸出顯示所有的信息錯誤,展示異常代碼位置 auto:只展示第一個和最后一個錯誤 long:展示全部信息 native:只展示puthon標准庫信息,不展示額外信息 """ # pytest.main(["--duration=1"])#只關心哪些部分是最慢的 # pytest.main(["-h"])
