国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

python單元測試框架pytest的使用示例

瀏覽:3日期:2022-07-09 09:17:47

首先祝大家國慶節(jié)日快樂,這個(gè)假期因?yàn)槲依掀乓甲?huì),我也跟著天天去圖書館學(xué)了幾天,學(xué)習(xí)的感覺還是非常不錯(cuò)的,這是一篇總結(jié)。

這篇博客準(zhǔn)備講解一下pytest測試框架,這個(gè)框架是當(dāng)前最流行的python語言最流行的單測框架,不掌握可不行,首先這個(gè)框架屬于第三方模塊,需要通過pip安裝即可

pip install pytest

下面我們進(jìn)入正題

一、介紹pytest的運(yùn)行規(guī)則

1、測試文件的名稱必須要以test_*.py的格式,或者*_test.py的格式

2、測試類的名稱必須要以Test開頭,且這個(gè)類還不能有構(gòu)造方法(__init__)

3、測試函數(shù)的名稱必須要以test開頭

pytest默認(rèn)的就按照上面的三條規(guī)則來執(zhí)行案例,當(dāng)然我們可以自定義運(yùn)行規(guī)則,這個(gè)我們后面在講,這個(gè)不重要,看一個(gè)最簡單的例子

import osimport 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測試框架,對(duì)測試固件這個(gè)這個(gè)名詞就不會(huì)陌生了,如果不清楚,可以看下之前我寫的unittest測試框架的博客(https://www.jb51.net/article/197004.htm)

pytest框架的測試固件有兩種,一種函數(shù)級(jí)別的,一種是類級(jí)別,執(zhí)行的順序如下

a、執(zhí)行類的前置條件

b、執(zhí)行函數(shù)的前置條件

c、執(zhí)行函數(shù)的后置條件

d、執(zhí)行類的后置條件

使用也非常簡單,當(dāng)時(shí)函數(shù)的命名一定要和我下面的備注保持完全一致

# pytest的前置和后置條件 # 1、函數(shù)級(jí)別 setup teardown# 運(yùn)行于測試方法的開始和結(jié)束# 運(yùn)行一個(gè)測試用例,會(huì)運(yùn)行一次setup和teardown # 2、類級(jí) setup_class teardown_class# 運(yùn)行于測試類的開始和結(jié)束# 一個(gè)測試類只運(yùn)行一次setup_class teardown_class

1、函數(shù)式的案例--函數(shù)級(jí)別的前置條件&后置條件

import osimport 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ù)級(jí)別的前置') def teardown(): print('函數(shù)級(jí)別的后置')

執(zhí)行結(jié)果如下

python單元測試框架pytest的使用示例

2、類式的案例--函數(shù)級(jí)別的前置條件&后置條件

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ù)級(jí)別的前置') def teardown(self): print('函數(shù)級(jí)別的后置')if __name__ == ’__main__’: pytest.main(['-s','pytest2.py'])

執(zhí)行結(jié)果如下

python單元測試框架pytest的使用示例

3、類級(jí)別的前置條件&后臺(tái)置條件

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ù)級(jí)別的前置') def teardown(self): print('函數(shù)級(jí)別的后置') def setup_class(self): print('類級(jí)別的前置') def teardown_class(self): print('類級(jí)別的后置')if __name__ == ’__main__’: pytest.main(['-s','pytest3.py'])

結(jié)果如下

python單元測試框架pytest的使用示例

三、介紹如何修改pytest的配置文件

我們?cè)诓┛偷牡谝徊糠纸榻B了pytest框架的運(yùn)行規(guī)則,這里我們可以修改pytest的配置文件,改變框架運(yùn)行規(guī)則

首先我們要在案例的目錄下創(chuàng)建一個(gè)pytest.ini的配置文件

python單元測試框架pytest的使用示例

內(nèi)容如下

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

配置文件截圖

python單元測試框架pytest的使用示例

通過上面的步驟,我們就可以改變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í)行

一個(gè)函數(shù)可以打多個(gè)標(biāo)記,一個(gè)標(biāo)記同時(shí)可以給多個(gè)函數(shù)打標(biāo)記。只需要讓這個(gè)標(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)不跳過某個(gè)、某些案例的作用

# -*- coding:utf-8 -*- import pytest # skip跳過執(zhí)行某個(gè)案例@pytest.mark.skip(reson='只是這個(gè)函數(shù)用例不執(zhí)行')def test_a(): print('testa') def test_b(): print('testb') @pytest.mark.skip(reson='整個(gè)類下的案例都不會(huì)執(zhí)行')class Test_skip(): def test_a(self): print('testa') def test_b(self): print('testb') # 可以根據(jù)條件判斷,為真,則不執(zhí)行@pytest.mark.skipif(1 > 2,reson='整個(gè)類下的案例滿足條件都不會(huì)執(zhí)行')class Test_skipif(): def test_a(self): print('testa') def test_b(self): print('testb')

六、介紹pytest的數(shù)據(jù)參數(shù)化

1、傳入單個(gè)參數(shù)

# pytest的數(shù)據(jù)參數(shù)化 # 1、傳入單個(gè)參數(shù)## pytest.mark.parametrize(argnames,argvalues)# argnames 參數(shù)的名稱## argvalues 參數(shù)對(duì)應(yīng)的值,類型必須是可迭代的類型,一般使用list @pytest.mark.skip(reson='只是這個(gè)函數(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ù)的名稱,這個(gè)案例會(huì)執(zhí)行4次,參數(shù)分別是name=“cui1”name='cui2'....

python單元測試框架pytest的使用示例

2、傳入多個(gè)參數(shù)

import pytest # pytest的數(shù)據(jù)參數(shù)化 # 1、傳入多個(gè)參數(shù)## pytest.mark.parametrize((argnames1,argnames2),[(argvalues1,argvalues1),(argvalues1,argvalues1)],(argvalues1,argvalues1)]]) @pytest.mark.skip(reson='只是這個(gè)函數(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)的效果如下

python單元測試框架pytest的使用示例

七、介紹pyest的常用第三方插件

1、美化pytest的輸出報(bào)告插件

# pip install pytest-html # 用來美化輸出報(bào)告的插件# 只需要在配置文件中加這個(gè)配置即可## addopts=-s --html=report.html

效果

python單元測試框架pytest的使用示例

python單元測試框架pytest的使用示例

2、失敗案例重試插件,下面的示例實(shí)現(xiàn)的就是失敗重啟3,失敗后間隔2s在進(jìn)行重試

# pip install pytest-rerunfailures# 失敗重試的第三方插件# 只需要在配置文件中加這個(gè)配置即# --reruns 3 --reruns-delay 2

至此,pytest的框架基本使用已經(jīng)講解清楚,小伙伴們還有不清楚的嗎?歡迎大家來溝通!!!

到此這篇關(guān)于python單元測試框架pytest的使用示例的文章就介紹到這了,更多相關(guān)python單元測試框架pytest內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 高州市| 巴塘县| 天峨县| 合肥市| 双桥区| 霞浦县| 双峰县| 盐池县| 扎赉特旗| 宣汉县| 商洛市| 衡山县| 鄂州市| 永善县| 安阳县| 林口县| 永济市| 二手房| 甘洛县| 晋宁县| 襄城县| 民丰县| 洪泽县| 喀什市| 宁明县| 广丰县| 黄冈市| 铁岭市| 枞阳县| 哈密市| 尤溪县| 茶陵县| 武隆县| 仙游县| 犍为县| 无为县| 丹巴县| 阳泉市| 赣榆县| 酉阳| 南漳县|