pytest是一個非常成熟的全功能的Python測試架構,主要有以下幾個特點:
1.簡單靈活,容易上手
2.支援參數化
3.能夠支援簡單的單元測試和複雜的功能測試,還可以用來做selenium/appnium等自動化測試、接口自動化測試(pytest+requests)
4.pytest具有很多第三方插件,并且可以自定義擴充,比較好用的如pytest-selenium(內建selenium)、pytest-html(完美html測試報告生成)、pytest-rerunfailures(失敗case重複執行)、pytest-xdist(多CPU分發)等
5.測試用例的skip和xfail處理
6.可以很好地和jenkins內建
7.report架構----allure 也支援了pytest
1、測試用例檔案命名應該以 test_開頭或者以_test結尾。
2、測試函數命名,測試類的方法命名應該以test_開頭。
3、測試類命名應當以Test開頭。
測試類的不應該有構造函數
1、主函數模式
(1運作所有:pytest.main()
(2指定子產品:pytest.main([’-vs’,'test_login.py])
(3 指定目錄:pytes.main([’-vs’,’./interface_testcase’])
(4 通過nodeid指定用例運作:nodeid由子產品名,分割符,類名,方法名,函數名組成
pytest.main([’-vs’,’./interface_testcase/test_interface.py::test_04_func’])
pytest.main([’-vs’,’./interface_testcase/test_interface.py::Testinterface::test_04_func’])
2、指令行模式
(1)運作所有:pytest
(2)指定子產品:pytest -vs test_login.py
(3)指定目錄:pytes -vs ./interface_testcase
(4)通過nodeid指定用例運作:nodeid由子產品名,分割符,類名,方法名,函數名組成
pytest -vs ./interface_testcase/test_interface.py::test_04_func
pytest -vs ./interface_testcase/test_interface.py::Testinterface::test_04_func
參數詳解:
-s: 表示輸出調試資訊,包括print列印的資訊
-v: 顯示更詳細的資訊
-vs:兩個參數一起用
-n:支援多線程或分布式運作用例
如:pytest -vs ./testcase/test_login.py -n 2
-rerun NUM:失敗用例重跑,num失敗後重跑的次數
-x:表示隻要有一個用例報錯,那麼測試停止
–maxfail=2 :出現兩個用例失敗就停止
-k:根據測試用例的步伐字元串指定測試用例
如:pytest -vs ./testcase -k “ao”
3、通過讀取pytest.ini配置檔案運作
pytest.ini這個檔案它是pytest單元測試架構的核心配置檔案
1、位置:一般放在項目的跟目錄下
2、編碼:必須是ANSI,可以使用notpad++修改編碼格式
3、作用:改變pytest預設的行為
4、運作規則:不管是主函數的模式運作,指令模式運作,都會去讀取這個配置檔案
pytest執行用例的順序:
unittes :ASCII的大小來決定執行的順序
pytest:預設從上到下
改變預設的執行順序,使用mark标記
@pytest.mark.run(order=2)