Pytest簡介
- 入門簡單,檔案豐富
- 支持單元測驗、功能測驗
- 支持引數化
- 重復執行,部分執行,測驗跳過
- 兼容其他測驗框架(nose,unittest等)
- 支持生成html報告
- 可集成CI環境(Jenkins 等)
2、Pytest安裝
import pytest
class Test_class:
def test_001(self):
print('用例001')
assert 8 == 8
def test_002(self):
print('用例002')
assert 6 == 6
def test_003(self):
print('用例003')
assert 3 == 2
if __name__ == "__main__":
# 里面引數需要傳list,多個引數放list就不會有警告了
# pytest.main('-q test_class.py')
pytest.main(['-q', 'test_class.py'])
3、創建一個簡單的test案例
import pytest
class Test_class:
def test_001(self):
print('用例001')
assert 8 == 8
def test_002(self):
print('用例002')
assert 6 == 6
def test_003(self):
print('用例003')
assert 3 == 2
if __name__ == "__main__":
# 里面引數需要傳list,多個引數放list就不會有警告了
# pytest.main('-q test_class.py')
pytest.main(['-q', 'test_class.py'])
執行結果
pytest 中用例的檢查點 直接用 Python 的 assert 斷言,
assert 后面的運算式結果 為 True ,就是檢查點通過,結果為False ,就是檢查點不通過,
執行測驗的時候,我們只需要在測驗檔案test_class所在的目錄下,運行py.test即可,pytest會在當前的目錄下,尋找以test開頭的檔案(即測驗檔案),找到測驗檔案之后,進入到測驗檔案中尋找test_開頭的測驗函式并執行,
4、Pycharm設定Pytest
#file->Setting->Tools->Python Integrated Tools->專案名稱->Default test runner->選擇py.test
#右鍵選擇pytest運行或者直接運行.py檔案
執行結果
由上可見:Pytest是可以兼容UnitTest腳本的,之前寫的UnitTest用例也能用Pytest框架去運行,
5、Pytest自動生成報告
# 需預先裝上pytest-html
>>>pip install pytest_html
# 生成html格式的報告
>>>pytest -v test_1.py --html=Path
# 生成xml格式的報告
>>>pytest -v test_1.py --junitxml=Path
# 生成txt格式的報告
>>>pytest -v test_1.py --resultlog=Path
注意:檢查運行指令時,路徑(根目錄)是否正確
參考地址:
https://docs.pytest.org/en/latest/warnings.html
生成報告
.test_class.py::Test_class::test_001
.test_class.py::Test_class::test_002
F
test_class.py::Test_class::test_003
self = < test_class.Test_class object at 0x000001582B868978 >
def test_003(self):
print('用例003')
> assert 3 == 2
E
assert 3 == 2
E + 3
E - 2
test_class.py: 24: AssertionError
PS:如有需要Python學習資料的小伙伴可以加下方的群去找免費管理員領取
可以免費領取原始碼、專案實戰視頻、PDF檔案等
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理,
作者:顧小魚
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/33673.html
標籤:Python
下一篇:03_多行程
