欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2021年04月16日 09:42:20   作者:阿刁阿  
這篇文章主要介紹了pytest實(shí)現(xiàn)測試用例參數(shù)化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

背景

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

說明

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

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

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

示例:

參數(shù)化之一個參數(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ù)化之多個參數(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個示例,展現(xiàn)的是一個測試用例有兩個參數(shù),然后參數(shù)化了兩組數(shù)據(jù)。

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

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

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

'''
class TestFunc:

 # 正常測試用例
 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é)

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

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

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

相關(guān)文章

  • Pandas中Series的創(chuàng)建及數(shù)據(jù)類型轉(zhuǎn)換

    Pandas中Series的創(chuàng)建及數(shù)據(jù)類型轉(zhuǎn)換

    這篇文章主要介紹了Pandas中Series的創(chuàng)建及數(shù)據(jù)類型轉(zhuǎn)換,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • Python多繼承以及MRO順序的使用

    Python多繼承以及MRO順序的使用

    這篇文章主要介紹了Python多繼承以及MRO順序的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Python中如何導(dǎo)入類示例詳解

    Python中如何導(dǎo)入類示例詳解

    這篇文章主要給大家介紹了關(guān)于Python中如何導(dǎo)入類的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Python SMTP配置參數(shù)并發(fā)送郵件

    Python SMTP配置參數(shù)并發(fā)送郵件

    這篇文章主要介紹了Python SMTP配置參數(shù)并發(fā)送郵件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • python實(shí)現(xiàn)邏輯回歸的方法示例

    python實(shí)現(xiàn)邏輯回歸的方法示例

    這篇文章主要介紹了python實(shí)現(xiàn)邏輯回歸的方法示例,這是機(jī)器學(xué)習(xí)課程的一個實(shí)驗(yàn),整理出來共享給大家,需要的朋友可以參考學(xué)習(xí),下來要一起看看吧。
    2017-05-05
  • Python 實(shí)現(xiàn)數(shù)據(jù)庫(SQL)更新腳本的生成方法

    Python 實(shí)現(xiàn)數(shù)據(jù)庫(SQL)更新腳本的生成方法

    當(dāng)我們需要準(zhǔn)備更新腳本的使用,不小心會忘記改動了哪里,所以小編試著用Python來實(shí)現(xiàn)自動的生成更新腳本,具體操作方法,大家參考下本文吧
    2017-07-07
  • pythotn條件分支與循環(huán)詳解(3)

    pythotn條件分支與循環(huán)詳解(3)

    這篇文章主要介紹了Python條件分支和循環(huán)用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python邏輯運(yùn)算操作符,條件分支語句,循環(huán)語句等功能與基本用法,需要的朋友可以參考下
    2021-08-08
  • python基礎(chǔ)詳解之if循環(huán)語句

    python基礎(chǔ)詳解之if循環(huán)語句

    這篇文章主要介紹了python基礎(chǔ)詳解之if循環(huán)語句,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有很好的幫助需要的朋友可以參考下
    2021-04-04
  • Python 字符串、列表、元組的截取與切片操作示例

    Python 字符串、列表、元組的截取與切片操作示例

    這篇文章主要介紹了Python 字符串、列表、元組的截取與切片操作,結(jié)合實(shí)例形式分析了Python針對字符串、列表、元組的截取與切片相關(guān)操作技巧,需要的朋友可以參考下
    2019-09-09
  • python解析文件示例

    python解析文件示例

    這篇文章主要介紹了python解析文本文件示例,大家參考使用吧
    2014-01-01

最新評論