python單元測試框架pytest的使用示例
首先祝大家國慶節(jié)日快樂,這個假期因?yàn)槲依掀乓甲?,我也跟著天天去圖書館學(xué)了幾天,學(xué)習(xí)的感覺還是非常不錯的,這是一篇總結(jié)。
這篇博客準(zhǔn)備講解一下pytest測試框架,這個框架是當(dāng)前最流行的python語言最流行的單測框架,不掌握可不行,首先這個框架屬于第三方模塊,需要通過pip安裝即可
pip install pytest
下面我們進(jìn)入正題
一、介紹pytest的運(yùn)行規(guī)則
1、測試文件的名稱必須要以test_*.py的格式,或者*_test.py的格式
2、測試類的名稱必須要以Test開頭,且這個類還不能有構(gòu)造方法(__init__)
3、測試函數(shù)的名稱必須要以test開頭
pytest默認(rèn)的就按照上面的三條規(guī)則來執(zhí)行案例,當(dāng)然我們可以自定義運(yùn)行規(guī)則,這個我們后面在講,這個不重要,看一個最簡單的例子
import os
import pytest
# pytest是python的單元測試框架
def func(x):
return x + 1
def test_a():
print("____test_a____")
assert func(2) == 5
def test_b():
print("____test_b____")
assert func(2) == 3
if __name__ == '__main__':
pytest.main(["-s","pytest1.py"])
二、介紹pytest的前置條件和后置條件,類似unittest的testfixture(測試固件)
如果同學(xué)們之前用過unittest測試框架,對測試固件這個這個名詞就不會陌生了,如果不清楚,可以看下之前我寫的unittest測試框架的博客(http://www.dbjr.com.cn/article/197004.htm)
pytest框架的測試固件有兩種,一種函數(shù)級別的,一種是類級別,執(zhí)行的順序如下
a、執(zhí)行類的前置條件
b、執(zhí)行函數(shù)的前置條件
c、執(zhí)行函數(shù)的后置條件
d、執(zhí)行類的后置條件
使用也非常簡單,當(dāng)時函數(shù)的命名一定要和我下面的備注保持完全一致
# pytest的前置和后置條件 # 1、函數(shù)級別 setup teardown # 運(yùn)行于測試方法的開始和結(jié)束 # 運(yùn)行一個測試用例,會運(yùn)行一次setup和teardown # 2、類級 setup_class teardown_class # 運(yùn)行于測試類的開始和結(jié)束 # 一個測試類只運(yùn)行一次setup_class teardown_class
1、函數(shù)式的案例--函數(shù)級別的前置條件&后置條件
import os
import pytest
def func(x):
return x + 1
def test_a():
print("____test_a____")
assert func(2) == 5
def test_b():
print("____test_b____")
assert func(2) == 3
def setup():
print("函數(shù)級別的前置")
def teardown():
print("函數(shù)級別的后置")
執(zhí)行結(jié)果如下

2、類式的案例--函數(shù)級別的前置條件&后置條件
class Testclass:
def test_a(self):
print("____test_a____")
assert func(2) == 5
def test_b(self):
print("____test_b____")
assert func(2) == 3
def setup(self):
print("函數(shù)級別的前置")
def teardown(self):
print("函數(shù)級別的后置")
if __name__ == '__main__':
pytest.main(["-s","pytest2.py"])
執(zhí)行結(jié)果如下

3、類級別的前置條件&后臺置條件
import pytest
def func(x):
return x + 1
class Testclass:
def test_a(self):
print("____test_a____")
assert func(2) == 5
def test_b(self):
print("____test_b____")
assert func(2) == 3
def setup(self):
print("函數(shù)級別的前置")
def teardown(self):
print("函數(shù)級別的后置")
def setup_class(self):
print("類級別的前置")
def teardown_class(self):
print("類級別的后置")
if __name__ == '__main__':
pytest.main(["-s","pytest3.py"])
結(jié)果如下

三、介紹如何修改pytest的配置文件
我們在博客的第一部分介紹了pytest框架的運(yùn)行規(guī)則,這里我們可以修改pytest的配置文件,改變框架運(yùn)行規(guī)則
首先我們要在案例的目錄下創(chuàng)建一個pytest.ini的配置文件

內(nèi)容如下
# 創(chuàng)建pytest.ini文件 # [pytest] # addopts=-s #這個先這樣寫,這個主要是執(zhí)行參數(shù) # testpaths = testcase # 只執(zhí)行這個目錄下的文件 # # python_files = test_*.py #執(zhí)行的文件的名字 # python_classes = Test_* #執(zhí)行類的名字 # python_functions = test_* # 執(zhí)行函數(shù)的名字
配置文件截圖

通過上面的步驟,我們就可以改變pytest的運(yùn)行規(guī)則
四、介紹pytest的斷言
pytest的斷言是用python的斷言,他不像unittest框架,他自己實(shí)現(xiàn)了斷言
# -*- coding:utf-8 -*-
# pytest是使用python自帶的斷言
import pytest
def func(x):
return x + 1
def test_a():
print("____test_a____")
assert func(2) == 5
def test_b():
print("____test_b____")
assert not func(2) == 3
def test_c():
print("____test_b____")
assert func(2) in ["a","b","c"]
def test_d():
print("____test_b____")
assert func(2) not in ["a","b","c"]
if __name__ == '__main__':
pytest.main(["-s","pytest5.py"])
五、介紹pytest的標(biāo)記(mark)
1、可以實(shí)現(xiàn)給函數(shù)打標(biāo)記,實(shí)現(xiàn)哪些標(biāo)記執(zhí)行,哪些標(biāo)記不執(zhí)行
一個函數(shù)可以打多個標(biāo)記,一個標(biāo)記同時可以給多個函數(shù)打標(biāo)記。只需要讓這個標(biāo)記的裝飾器函數(shù)裝飾我們的測試類或者測試函數(shù)
class Test_mark():
@pytest.mark.test01
def test_a(self):
print("mark test a")
@pytest.mark.test02
def test_b(self):
print("mark test b")
if __name__ == '__main__':
pytest.main(['-s',"pytest6.py"])
還有其它的執(zhí)行方式
# pytest -m test01 # pytest -n "test01 or test02" # pytest -m "not test01"
2、標(biāo)記可以實(shí)現(xiàn)不跳過某個、某些案例的作用
# -*- coding:utf-8 -*-
import pytest
# skip跳過執(zhí)行某個案例
@pytest.mark.skip(reson="只是這個函數(shù)用例不執(zhí)行")
def test_a():
print("testa")
def test_b():
print("testb")
@pytest.mark.skip(reson="整個類下的案例都不會執(zhí)行")
class Test_skip():
def test_a(self):
print("testa")
def test_b(self):
print("testb")
# 可以根據(jù)條件判斷,為真,則不執(zhí)行
@pytest.mark.skipif(1 > 2,reson="整個類下的案例滿足條件都不會執(zhí)行")
class Test_skipif():
def test_a(self):
print("testa")
def test_b(self):
print("testb")
六、介紹pytest的數(shù)據(jù)參數(shù)化
1、傳入單個參數(shù)
# pytest的數(shù)據(jù)參數(shù)化
# 1、傳入單個參數(shù)
#
# pytest.mark.parametrize(argnames,argvalues)
# argnames 參數(shù)的名稱
#
# argvalues 參數(shù)對應(yīng)的值,類型必須是可迭代的類型,一般使用list
@pytest.mark.skip(reson="只是這個函數(shù)用例不執(zhí)行")
def test_a():
print("testa")
@pytest.mark.parametrize("name",["cui1","cui2","cui3","cui4"])
def test_b(name):
print("testb----->{name}".format(name = name))
if __name__ == '__main__':
pytest.main(["-s", "pytest8.py"])
實(shí)現(xiàn)的效果name作為參數(shù)的名稱,這個案例會執(zhí)行4次,參數(shù)分別是name=“cui1”\name="cui2"\....

2、傳入多個參數(shù)
import pytest
# pytest的數(shù)據(jù)參數(shù)化
# 1、傳入多個參數(shù)
#
# pytest.mark.parametrize((argnames1,argnames2),[(argvalues1,argvalues1),(argvalues1,argvalues1)],(argvalues1,argvalues1)]])
@pytest.mark.skip(reson="只是這個函數(shù)用例不執(zhí)行")
def test_a():
print("testa")
@pytest.mark.parametrize(("name","age"),[("cui1",12),("cui2",13),("cui3",14)])
def test_b(name,age):
print("testb----->{name}----->{age}".format(name = name,age = age))
if __name__ == '__main__':
pytest.main(["-s", "pytest9.py"])
實(shí)現(xiàn)的效果如下

七、介紹pyest的常用第三方插件
1、美化pytest的輸出報(bào)告插件
# pip install pytest-html # 用來美化輸出報(bào)告的插件 # 只需要在配置文件中加這個配置即可 # # addopts=-s --html=report.html
效果


2、失敗案例重試插件,下面的示例實(shí)現(xiàn)的就是失敗重啟3,失敗后間隔2s在進(jìn)行重試
# pip install pytest-rerunfailures # 失敗重試的第三方插件 # 只需要在配置文件中加這個配置即 # --reruns 3 --reruns-delay 2
至此,pytest的框架基本使用已經(jīng)講解清楚,小伙伴們還有不清楚的嗎?歡迎大家來溝通?。?!
到此這篇關(guān)于python單元測試框架pytest的使用示例的文章就介紹到這了,更多相關(guān)python單元測試框架pytest內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python基于win32ui模塊創(chuàng)建彈出式菜單示例
這篇文章主要介紹了Python基于win32ui模塊創(chuàng)建彈出式菜單,結(jié)合實(shí)例形式分析了Python使用win32ui模塊創(chuàng)建彈出式菜單的具體步驟與相關(guān)操作技巧,并附帶說明了win32ui模塊的安裝命令,需要的朋友可以參考下2018-05-05
Python實(shí)現(xiàn)按學(xué)生年齡排序的實(shí)際問題詳解
這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)按學(xué)生年齡排序?qū)嶋H問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
Matplotlib實(shí)戰(zhàn)之折線圖繪制詳解
折線圖是一種用于可視化數(shù)據(jù)變化趨勢的圖表,它可以用于表示任何數(shù)值隨著時間或類別的變化,本文主要介紹了如何利用Matplotlib實(shí)現(xiàn)折線圖的繪制,感興趣的可以了解下2023-08-08
python在linux環(huán)境下安裝skimage的示例代碼
這篇文章主要介紹了python在linux環(huán)境下安裝skimage,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
python兒童學(xué)游戲編程知識點(diǎn)總結(jié)
在本文里小編給大家整理了關(guān)于python兒童學(xué)游戲編程知識點(diǎn)以及內(nèi)容總結(jié),需要的朋友們參考學(xué)習(xí)下。2019-06-06
Python實(shí)現(xiàn)解析ini配置文件的示例詳解
在開發(fā)過程中,配置文件是少不了的,而且配置文件是有專門的格式的,比如:ini,?yaml,?toml?等等。而對于?Python?而言,也都有相應(yīng)的庫來解析相應(yīng)格式的文件,下面我們來看看?ini?文件要如何解析2022-09-09

