校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁(yè) > 知識(shí)庫(kù) > pytest實(shí)現(xiàn)測(cè)試用例參數(shù)化

pytest實(shí)現(xiàn)測(cè)試用例參數(shù)化

熱門標(biāo)簽:房產(chǎn)電銷外呼系統(tǒng) 南京銷售外呼系統(tǒng)軟件 地圖標(biāo)注的意義點(diǎn) 蓋州市地圖標(biāo)注 地圖制圖標(biāo)注位置改變是移位嗎 地圖標(biāo)注微信發(fā)送位置不顯示 上海機(jī)器人外呼系統(tǒng)哪家好 315電話機(jī)器人廣告 浙江電銷卡外呼系統(tǒng)好用嗎

背景

本文總結(jié)pytest的測(cè)試用例參數(shù)化。

說(shuō)明

軟件測(cè)試中,輸入相應(yīng)值,檢查期望值,是常見(jiàn)測(cè)試方法。
在自動(dòng)化測(cè)試中,一個(gè)測(cè)試用例對(duì)應(yīng)一個(gè)測(cè)試點(diǎn),通常一組測(cè)試數(shù)據(jù)無(wú)法完全覆蓋測(cè)試范圍,所以,需要參數(shù)化來(lái)傳遞多組數(shù)據(jù)。

pytest的測(cè)試用例參數(shù)化使用如下裝飾器即可完成。

@pytest.mark.parametrize(argnames, argvalues)
# 參數(shù):
# argnames:以逗號(hào)分隔的字符串
# argvaluse: 參數(shù)值列表,若有多個(gè)參數(shù),一組參數(shù)以元組形式存在,包含多組參數(shù)的所有參數(shù)
# 以元組列表形式存在

示例:

參數(shù)化之一個(gè)參數(shù)。

# ./test_case/test_func.py
import pytest

@pytest.mark.parametrize("arg_1", [4399, 2012])
def test_add_by_func_aaa(arg_1):
 print(arg_1)
 
# ./run_test.py
import pytest

if __name__ == '__main__':
 pytest.main(['-v','-s'])
 
'''
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 2 items

test_case/test_func.py::test_add_by_func_aaa[4399] 4399
PASSED
test_case/test_func.py::test_add_by_func_aaa[2012] 2012
PASSED

============================== 2 passed in 0.04s ==============================
[Finished in 1.3s]
''' 

參數(shù)化之多個(gè)參數(shù)。

# ./test_case/test_func.py
import pytest  

@pytest.mark.parametrize("arg_1, arg_2", [(4399, 'AAAA'), (2012, 'BBBB')])
def test_add_by_func_aaa(arg_1,arg_2):
 print("arg_1:{}  arg_2:{}".format(arg_1, arg_2))

# ./run_test.py
import pytest

if __name__ == '__main__':
 pytest.main(['-v','-s'])
 
'''
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 2 items

test_case/test_func.py::test_add_by_func_aaa[4399-AAAA] arg_1:4399  arg_2:AAAA
PASSED
test_case/test_func.py::test_add_by_func_aaa[2012-BBBB] arg_1:2012  arg_2:BBBB
PASSED

============================== 2 passed in 0.05s ==============================
[Finished in 1.3s]
'''  

以上第2個(gè)示例,展現(xiàn)的是一個(gè)測(cè)試用例有兩個(gè)參數(shù),然后參數(shù)化了兩組數(shù)據(jù)。

但在實(shí)際測(cè)試中,有的場(chǎng)景,比如多條件查詢,比如有2個(gè)查詢條件,每個(gè)條件有3個(gè)選項(xiàng),如果要全部覆蓋,則是3*3==9種情況。這種情景,人工測(cè)試一般是不會(huì)全部覆蓋的,但在自動(dòng)化測(cè)試中,只要你想,就可以做到。如下示例:

如下格式參數(shù)化,其測(cè)試結(jié)果為所有參數(shù)選項(xiàng)數(shù)量的乘積。

# ./test_case/test_func.py
import pytest
from func import *

'''
class TestFunc:

 # 正常測(cè)試用例
 def test_add_by_class(self):
  assert add(2,3) == 5


 def test_add_by_class_11(self):
  assert add(2,3) == 5
'''  

@pytest.mark.parametrize("arg_1", [4399,  2012, 1997])
@pytest.mark.parametrize("arg_2", ['AAAA', 'BBBB', 'CCCC'])
def test_add_by_func_aaa(arg_1,arg_2):
 print("arg_1:{}  arg_2:{}".format(arg_1, arg_2))
 

# ./run_test.py
import pytest

if __name__ == '__main__':
 pytest.main(['-v','-s'])
  
 
'''
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 9 items

test_case/test_func.py::test_add_by_func_aaa[AAAA-4399] arg_1:4399  arg_2:AAAA
PASSED
test_case/test_func.py::test_add_by_func_aaa[AAAA-2012] arg_1:2012  arg_2:AAAA
PASSED
test_case/test_func.py::test_add_by_func_aaa[AAAA-1997] arg_1:1997  arg_2:AAAA
PASSED
test_case/test_func.py::test_add_by_func_aaa[BBBB-4399] arg_1:4399  arg_2:BBBB
PASSED
test_case/test_func.py::test_add_by_func_aaa[BBBB-2012] arg_1:2012  arg_2:BBBB
PASSED
test_case/test_func.py::test_add_by_func_aaa[BBBB-1997] arg_1:1997  arg_2:BBBB
PASSED
test_case/test_func.py::test_add_by_func_aaa[CCCC-4399] arg_1:4399  arg_2:CCCC
PASSED
test_case/test_func.py::test_add_by_func_aaa[CCCC-2012] arg_1:2012  arg_2:CCCC
PASSED
test_case/test_func.py::test_add_by_func_aaa[CCCC-1997] arg_1:1997  arg_2:CCCC
PASSED

============================== 9 passed in 0.06s ==============================
[Finished in 1.4s]
'''

總結(jié)

以上,就是我們測(cè)試中使用的pytest測(cè)試用例參數(shù)化。

當(dāng)然,如實(shí)際需要,你也可以把測(cè)試數(shù)據(jù)獨(dú)立到文件里。然后讀取出來(lái),傳遞給@pytest.mark.parametrize(argnames, argvalues)裝飾器

到此這篇關(guān)于pytest實(shí)現(xiàn)測(cè)試用例參數(shù)化的文章就介紹到這了,更多相關(guān)pytest 測(cè)試用例參數(shù)化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python pytest進(jìn)階之conftest.py詳解
  • Pytest框架之fixture的詳細(xì)使用教程
  • Pytest測(cè)試框架基本使用方法詳解
  • Python 測(cè)試框架unittest和pytest的優(yōu)劣
  • Pytest參數(shù)化parametrize使用代碼實(shí)例
  • python單元測(cè)試框架pytest的使用示例
  • 使用PyCharm安裝pytest及requests的問(wèn)題
  • 通過(guò)代碼實(shí)例解析Pytest運(yùn)行流程

標(biāo)簽:赤峰 克拉瑪依 日照 臨汾 貴州 陽(yáng)泉 金華 雙鴨山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《pytest實(shí)現(xiàn)測(cè)試用例參數(shù)化》,本文關(guān)鍵詞  pytest,實(shí)現(xiàn),測(cè),試用,例,參數(shù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《pytest實(shí)現(xiàn)測(cè)試用例參數(shù)化》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于pytest實(shí)現(xiàn)測(cè)試用例參數(shù)化的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 南木林县| 平顺县| 仪征市| 侯马市| 河北省| 新宁县| 东宁县| 博野县| 广南县| 巴林左旗| 内江市| 南宫市| 比如县| 涟水县| 灵山县| 图片| 和平区| 土默特右旗| 祁东县| 南漳县| 通许县| 武城县| 汤阴县| 台中市| 平凉市| 遵义县| 岚皋县| 新泰市| 望江县| 伊金霍洛旗| 迁西县| 新邵县| 桓仁| 天峨县| 右玉县| 揭东县| 如东县| 淳安县| 福鼎市| 汶上县| 安丘市|