一、pytest 測試用例的運行方式
假設目錄結構如下,run為測試執行入口
pytest 默認命名規則:
- 文件名以 test_*.py 文件和*_test.py
- 以 test_ 開頭的函數
- 以 Test 開頭的類,且不能包含 __init__ 方法
- 以 test_ 開頭的類里面的方法
- 所有的包 package 必須要有__init__.py 文件
執行模式:
- 主函數模式
- pytest.main() ----運行所有模塊
- pytest.main(['-vs','./test_1.py']) ----運行指定模塊
- pytest.main(['-vs', './other_test']) ----運行指定目錄
- pytest.main(['-vs','./test_1.py::test_2']) ----運行指定的測試方法
- pytest.main(['-vs', './test_Class.py::TestMycase1']) ----運行指定的測試類
- pytest.main(['-vs', './test_Class.py::TestMycase1::test_method3']) ----運行類中指定的測試方法
- 命令行模式
- pytest ----運行所有模塊
- pytest -vs test_1.py ----運行指定模塊
- pytest -vs other_test ----運行指定目錄
- pytest -vs test_1.py::test_2 ----運行指定的測試方法
- pytest -vs test_Class.py::TestMycase1 ----運行指定的測試類
- pytest -vs test_Class.py::TestMycase1::test_method3 ----運行類中指定的測試方法
執行參數
-v 輸出更詳細的執行結果
pytest.main(['-v','./test_1.py'])
-s 輸出更詳細的打印信息(比如print 打印)
pytest.main(['-s', './test_1.py'])
還可以組合使用
pytest.main(['-vs', './test_1.py'])
-k 匹配用例名詞(當前路徑下的 文件名、類名、方法名,備注可以結合or,not 等關鍵字 ,不區分大小寫)
pytest.main(['-vs', '-k', 'up', './test_2.py'])
-x 遇到錯誤時停止
pytest.main(['-vs','-x', './test_stop.py'])
--maxfail=num 失敗數量達到num個時停止
pytest.main(['-vs', '--maxfail=2', './test_stop.py'])
-n=num 開啟num個線程去執行用例 (需要安裝 pytest-xdist)
pytest.main(['-vs','./test_thread.py','-n=2'])
--reruns=num 設置失敗重跑次數 如果存在用例失敗,用例失敗后重跑的次數num
pytest.main(['-vs', './test_fail.py', '--reruns=2'])
按執行的范圍去設置重跑次數(比如某個類中或者某個方法失敗了重跑幾次):
pytest.main(['-vs', './test_fail2.py::TestMycase2', '--reruns=2']
執行順序
pytest 默認允許是從上到下的順序去執行
但是凡事總有例外
pytest 支持使用裝飾器 去設置測試用例的執行順序
@pytest.mark.run(order=num)
import pytest ''' pytest時按從上到下的順序去執行的 可以通過mark標記 改變 模塊中的測試方法的執行順序 ''' @pytest.mark.run(order=3) def test_3(): print('test_3') @pytest.mark.run(order=2) def test_1(): print('test_1') @pytest.mark.run(order=1) def test_2(): print('test_2') if __name__ == '__main__': pytest.main(['./test_順序.py'])
運行結果
============================= test session starts ============================= platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.10.0, pluggy-0.13.1 -- D:\Python\python.exe cachedir: .pytest_cache metadata: {'Python': '3.8.5', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.2', 'xdist': '2.4.0'}} rootdir: D:\pytest_demo, configfile: pytest.ini plugins: forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.4.0 collecting ... collected 3 items test_順序.py::test_2 test_2 PASSED test_順序.py::test_1 test_1 PASSED test_順序.py::test_3 test_3 PASSED ============================== warnings summary =============================== ..\..\Python\lib\site-packages\_pytest\config\__init__.py:1233 D:\Python\lib\site-packages\_pytest\config\__init__.py:1233: PytestConfigWarning: Unknown config option: marks self._warn_or_fail_if_strict(f"Unknown config option: {key}\n") -- Docs: https://docs.pytest.org/en/stable/warnings.html ======================== 3 passed, 1 warning in 0.02s =========================
跳過用例
skip
skipif
通過skip關鍵字,我們可以跳過指定用例
import pytest class Test_1: def test_1(self): print('test_1') def test_2(self): print('test_2') def test_3(self): print('test_3') class Test_2: num = 10 @pytest.mark.skipif(num>=10,reason='大於10,所以跳過') def test_21(self): print('test_21') @pytest.mark.skip(reason='無條件跳過') def test_22(self): assert 1==2 def test_23(self): print('test_23') if __name__ == '__main__': pytest.main(['-vs','./test_跳過.py'])
執行結果:
============================= test session starts ============================= platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.10.0, pluggy-0.13.1 -- D:\Python\python.exe cachedir: .pytest_cache metadata: {'Python': '3.8.5', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.2', 'xdist': '2.4.0'}} rootdir: D:\pytest_demo\pytest_demo66\pytest_跳過 plugins: forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.4.0 collecting ... collected 6 items test_跳過.py::Test_1::test_1 PASSED [ 16%]test_1 test_跳過.py::Test_1::test_2 PASSED [ 33%]test_2 test_跳過.py::Test_1::test_3 PASSED [ 50%]test_3 test_跳過.py::Test_2::test_21 SKIPPED (大於10,所以跳過) [ 66%] Skipped: 大於10,所以跳過 test_跳過.py::Test_2::test_22 SKIPPED (無條件跳過) [ 83%] Skipped: 無條件跳過 test_跳過.py::Test_2::test_23 PASSED [100%]test_23 ======================== 4 passed, 2 skipped in 0.03s ========================= Process finished with exit code 0