pytest 常用命令
測驗資訊輸出
# 設定pytest的執行引數 "-q":安靜模式, 不輸出環境資訊
pytest.main(["-q"])
# 設定pytest的執行引數 "-s":顯示程式中的print/logging輸出
pytest.main(["-s"])
# 設定pytest的執行引數 "-v":豐富資訊模式, 輸出更詳細的用例執行資訊
pytest.main(["-v"])
指定用例執行
- 執行全部用例;
# 直接執行pytest.main():自動查找當前目錄下,以`test_`開頭或者以`_test`結尾的 .py 檔案
pytest.main()
- 執行指定py檔案的用例;
# main函式中填寫py檔案,會運行指定檔案內的測驗任務;
pytest.main(["test_login.py"])
- 執行指定方法的測驗用例;
# main函式中填寫 `py檔案::類::方法` (例如:test_mod.py::TestClass::test_method)
# 會運行指定檔案內的、指定類的、指定測驗任務;
pytest.main(["test_login.py::Test_Login::test_login"])
- 執行被標記的測驗任務;
# 設定pytest的執行引數 "-m slow":會執行被裝飾器 `@pytest.mark.marker` 裝飾的所有測驗用例;
# @pytest.mark.marker中,`marker` 字符名稱可以自定義;例如定義為 `level1`
@pytest.mark.level1
def test_a(self):
pass
pytest.main(['-m=level1'])
測驗報告輸出
- allure 測驗報告輸出,詳細使用方法見《Allure測驗報告》
# 需要安裝 allure-pytest
# 設定pytest的執行引數 "--alluredir=./report"
## --alluredir : 生成報告型別
## ./report : 生成報告存放路徑及名稱
pytest.main(['--alluredir=./report'])
- html 靜態報告
# 需要安裝 pytest-html
# 設定pytest的執行引數"--html=./report.html":執行測驗檔案并生成html格式的報告
## --html : 生成報告型別
## ./report.html : 生成報告存放路徑,及報告名稱
pytest.main(['--html=./report.html'])
# '--self-contained-html':將html的css樣式合并到測驗報告中
pytest.main(['--html=./report.html' , '--self-contained-html'])
- xml 格式報告(沒用過)
# 設定pytest的執行引數 "--junitxml=./report.xml":執行測驗檔案并生成xml格式的報告;
# 可以與jenkins做集成時使用
## --junitxml : 生成報告型別
## ./report.xml : 生成報告存放路徑,及報告名稱
pytest.main(["--junitxml=./report.xml"])
- log 格式報告(沒用過)
# 設定pytest的執行引數 "--resultlog=./report.txt":執行測驗檔案并生成log格式的報告(4.0以上版本被移除)
## --resultlog : 生成報告型別
## ./report.txt : 生成報告存放路徑,及報告名稱
pytest.main(["--resultlog=./report.txt"])
注:輸出測驗報告的時候,如果運行的檔案不再代碼目錄的 根目錄 下,是不會生成測驗報告的,
可在根目錄下寫一個run.py檔案,然后運行即可生產測驗報告
run.py
import pytest pytest.main(['-q','./AAS_v65/testcase/test_secadmin.py','--alluredir=./report'])
測驗執行策略
- 失敗 1 次停止測驗
# 設定pytest的執行引數 -x":第01次失敗就停止測驗
pytest.main(["-x"])
- 指定最大失敗次數后,停止測驗
# 設定pytest的執行引數 "--maxfail=2":第02次失敗就停止測驗
pytest.main(["--maxfail=2"])
- 指定失敗重試次數后,停止測驗
# 需要安裝 pytest-rerunfailures
# 設定pytest的執行引數 "--reruns=NUM"(NUM是重試次數):測驗用例運行失敗時,重新運行用例
pytest.main(["--reruns=2"])
- 指定用例并發行程數(還沒用過)
# 需要安裝 pytest-xdist
# 設定pytest的執行引數 "-n NUM"(NUM是需要并發的行程數):支持多執行緒運行測驗用例
pytest.main(["-n=2"])
本文來自博客園,作者:粥雨,轉載請注明原文鏈接:https://www.cnblogs.com/mzline/p/17419526.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/553100.html
標籤:其他
下一篇:返回列表
