執行指定case:
- pytest.main() #執行所有case
- pytest.main(['-vs', 'TestCalc.py::TestCalc']) #執行TestCalc.py文件內TestCalc類下所有case
- pytest.main(['-vs', 'TestCalc.py::TestCalc::test_division_seven']) #執行TestCalc.py文件內TestCalc類下名字為test_division_seven的case
- pytest.main(['參數','xxx']) pytest.main()內必須是list格式,在低版本已不支持str
具體參數:
- -v: #打印詳細運行的日志信息
- -s: pytest -s xxx.py #輸出case中print的內容
- -m: pytest -m “tag名稱” #運行指定tag名稱的用例,也就是運行有@pytest.mark.[標記名]這個標記的case
- -k: pytest -k “類名、方法名、類名 and not 方法名” #運行指定case的用例
- -x: #遇到失敗的case就是停止執行
- --lf: #只重新運行上次運行失敗的用例(或如果沒有失敗的話會全部跑)
- --ff: #運行所有測試,但首先運行上次運行失敗的測試(這可能會重新測試,從而導致重復的fixture setup/teardown)
- --maxfail=num: #當用例失敗個數達到num時,停止運行
- --collect-only: 收集測試用例,展示出哪些用例會被執行(只是展示不會執行case)
- --junit-xml:--junit-xml=path/name.xml #在指定目錄或當前目錄下生成xml格式的報告(需要在pytest.ini文件內聲明格式:junit_family=xunit2)
- --steup-show #完整展示每個用例的fixture調用順序
注意:必須在pytest.ini文件內聲明標簽,不然則會告警PytestUnknownMarkWarning
1 #demo: 2 pytest.ini 3 markers = 4 div 5 tag
命令行執行:
pytest test_quick_start.py --junit-xml=report.xml main執行: pytest.main(["-s", "TestCalc.py", "-m", "div", "--junit-xml=report.xml"]) pytest.main(["-vsx", "TestCalc.py", "-m", "div"])
語法:
@pytest.mark.smoke @pytest.mark.get $pytest -m 'smoke' 僅運行標記smoke的函數 $pytest -m 'smoke and get' 運行標記smoke和get的函數 $pytest -m 'smoke or get' 運行標記smoke或get的函數 $pytest -m 'smoke and not get' 運行標記smoke和標記不是get的函數