久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

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

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

首先祝大家國慶節(jié)日快樂,這個假期因為我老婆要考注會,我也跟著天天去圖書館學(xué)了幾天,學(xué)習(xí)的感覺還是非常不錯的,這是一篇總結(jié)。

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

pip install pytest

下面我們進入正題

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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é)果如下

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

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é)果如下

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

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

我們在博客的第一部分介紹了pytest框架的運行規(guī)則,這里我們可以修改pytest的配置文件,改變框架運行規(guī)則

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

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

內(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ù)的名字

配置文件截圖

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

通過上面的步驟,我們就可以改變pytest的運行規(guī)則

四、介紹pytest的斷言

pytest的斷言是用python的斷言,他不像unittest框架,他自己實現(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的標記(mark)

1、可以實現(xiàn)給函數(shù)打標記,實現(xiàn)哪些標記執(zhí)行,哪些標記不執(zhí)行

一個函數(shù)可以打多個標記,一個標記同時可以給多個函數(shù)打標記。只需要讓這個標記的裝飾器函數(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、標記可以實現(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'])

實現(xiàn)的效果name作為參數(shù)的名稱,這個案例會執(zhí)行4次,參數(shù)分別是name=“cui1”name='cui2'....

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

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'])

實現(xiàn)的效果如下

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

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

1、美化pytest的輸出報告插件

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

效果

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

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

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

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

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

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

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 犍为县| 来安县| 明星| 麻阳| 隆林| 镇原县| 富源县| 镇康县| 长子县| 山阳县| 工布江达县| 林州市| 定襄县| 蓬安县| 瑞金市| 布尔津县| 资阳市| 淮安市| 恩平市| 扎赉特旗| 北碚区| 娄烦县| 建水县| 怀仁县| 获嘉县| 达州市| 民乐县| 克东县| 蓬莱市| 安乡县| 乐安县| 萍乡市| 泗阳县| 逊克县| 仲巴县| 龙川县| 永和县| 天水市| 鄢陵县| 中西区| 二连浩特市|