自動化測試Pytest單元測試框架的基本介紹
一、Pytest概念
Pytest 是 Python 的一種單元測試框架,與 Python 自帶的 unittest 測試框架類似,但是比 unittest 框架使用起來更簡潔,效率更高。
二、Pytest特點
Pytest是一個非常成熟的Python測試框架,主要特點有以下幾點:
- 非常容易上手,入門簡單,文檔豐富,文檔中有很多實例可以參考;
- 能夠支持簡單的單元測試和復(fù)雜的功能測試;
- 支持參數(shù)化;
- 執(zhí)行測試過程中可以將某些測試用例跳過(skip),或者對某些預(yù)期失敗的case標(biāo)記成失??;
- 支持重復(fù)執(zhí)行(rerun)失敗的 case;
- 支持運行由 nose, unittest 編寫的測試 case;
- 可生成html 報告;
- 方便jenkins持續(xù)集成;
- 可支持執(zhí)行部分用例;
- 具有很多第三方插件,并且可以自定義擴展。
三、Pytest安裝
安裝pytest命令:
pip install pytest
查看pytest版本:
pytest --version
安裝生成測試結(jié)果的HTML報告pytest-html
pip install pytest-html
這里已經(jīng)安裝過,所以輸出信息和第一次安裝不一樣。
四、Pycharm配置Pytest
pycharm依次選擇
File->Settings->Tools->Python Integrated Tools
配置用例腳本運行模式。
菜單欄
點擊Edit Configurations。
依次點擊"+" --》 Python tests --》pytest
配置項目路徑
如下:
五、Pytest用例運行規(guī)則
用Pytest寫用例時候,一定要按照下面的規(guī)則去寫,否則不符合規(guī)則的測試用例是不會執(zhí)行的。
文件名以 test_*.py 文件或*_test.py;
以 test_ 開頭的函數(shù);
以 Test 開頭的類,不能包含 __init__ 方法;
以 test_ 開頭的類里面的方法;
所有的包(package)必項要有__init__.py 文件。
六、 Pytest簡單使用
環(huán)境都準(zhǔn)備好了,嘗試下使用pytest運行用例。
新建py文件
寫兩條測試用例
import pytest def test_demo1(): assert 3 == 3 def test_demo2(): assert 3 == 5 if __name__ == '__main__': pytest.main()
運行之后,結(jié)果如下:
Testing started at 12:37 ... C:\Users\96984\Desktop\code\learn_pytest\venv\Scripts\python.exe "C:\ruanjian\pycharm2019.3\PyCharm 2019.3.1\plugins\python\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/96984/Desktop/code/learn_pytest/demo/demo_pytest.py Launching pytest with arguments C:/Users/96984/Desktop/code/learn_pytest/demo/demo_pytest.py in C:\Users\96984\Desktop\code\learn_pytest\demo ============================= test session starts ============================= platform win32 -- Python 3.6.8, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- C:\Users\96984\Desktop\code\learn_pytest\venv\Scripts\python.exe cachedir: .pytest_cache metadata: {'Python': '3.6.8', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'pytest': '5.4.3', 'py': '1.9.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '2.1.1', 'metadata': '1.10.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_77'} rootdir: C:\Users\96984\Desktop\code\learn_pytest\demo plugins: html-2.1.1, metadata-1.10.0 collecting ... collected 2 items demo_pytest.py::test_demo1 PASSED [ 50%] demo_pytest.py::test_demo2 FAILED [100%] demo_pytest.py:8 (test_demo2) def test_demo2(): > assert 3 == 5 E AssertionError demo_pytest.py:10: AssertionError ================================== FAILURES =================================== _________________________________ test_demo2 __________________________________ def test_demo2(): > assert 3 == 5 E AssertionError demo_pytest.py:10: AssertionError =========================== short test summary info =========================== FAILED demo_pytest.py::test_demo2 - AssertionError ========================= 1 failed, 1 passed in 0.05s ========================= Process finished with exit code 0
相關(guān)文章
Python selenium爬取微博數(shù)據(jù)代碼實例
這篇文章主要介紹了Python selenium爬取微博數(shù)據(jù)代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05Python爬取當(dāng)網(wǎng)書籍?dāng)?shù)據(jù)并數(shù)據(jù)可視化展示
這篇文章主要介紹了Python爬取當(dāng)網(wǎng)書籍?dāng)?shù)據(jù)并數(shù)據(jù)可視化展示,下面文章圍繞Python爬蟲的相關(guān)資料展開對爬取當(dāng)網(wǎng)書籍?dāng)?shù)據(jù)的詳細(xì)介紹,需要的小伙伴可以參考一下,希望對你有所幫助2022-01-01?cmd輸入python打開微軟應(yīng)用商店的解決方法
在命令控制行中輸入python想使用Python環(huán)境,卻意外打開了微軟自帶的應(yīng)用商店,十分苦惱,下面這篇文章主要給大家介紹了關(guān)于?cmd輸入python打開微軟應(yīng)用商店的解決方法,需要的朋友可以參考下2024-03-03pytorch中 gpu與gpu、gpu與cpu 在load時相互轉(zhuǎn)化操作
這篇文章主要介紹了pytorch模型載入之gpu和cpu互轉(zhuǎn)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Pandas剔除混合數(shù)據(jù)中非數(shù)字的數(shù)據(jù)操作
這篇文章主要介紹了Pandas剔除混合數(shù)據(jù)中非數(shù)字的數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03Python 計算機視覺編程進階之OpenCV 進行霍夫變換
霍夫變換(Hough)是一個非常重要的檢測間斷點邊界形狀的方法。它通過將圖像坐標(biāo)空間變換到參數(shù)空間,來實現(xiàn)直線與曲線的擬合,通過本篇文章我們來詳細(xì)了解它2021-11-11