pytest(1) pytest.mark.parametrize裝飾器可以實現用例參數化


裝飾器@pytest.mark.parametrize()可以使用單個變量接收數據,也可以使用多個變量接收,測試用例函數需要與其保持一致

例子:

import pytest
data1 = [[1,2,3],[4,5,9]]
def add(a,b):
    return a+b

@pytest.mark.parametrize("a,b,expect",data1)
def test_add1(a,b,expect):
    print("測試函數1測試數據為 {}-{}".format(a,b))
    assert add(a,b) == expect

@pytest.mark.parametrize("value",data1)
def test_add2(value):
    print("測試函數2測試數據為{}".format(value))
    assert add(value[0],value[1]) == value[2]

if __name__ == "__main__":
    pytest.main(["-sv", "test_2.py"])

 

測試數據組合

import pytest

data_1 = [1, 2]
data_2 = [3, 4]

@pytest.mark.parametrize('a', data_1)
@pytest.mark.parametrize('b', data_2)
def test_parametrize_1(a, b):
    print('\n測試數據為\n{},{}'.format(a, b))

if __name__ == '__main__':
    pytest.main(["-s", "test_3.py"])
 
        

標記用例

參數化裝飾器可以標記用例失敗(xfail)或者跳過(skip或skipif)

import pytest

data_1 = [[1,2,3],
          pytest.param(3,4,8,marks=pytest.mark.skip)]

def add(a,b):
    return a+b

@pytest.mark.parametrize("a,b,expect",data_1)
def test_pa(a,b,expect):
    print('測試數據為{},{}'.format(a,b))
    assert add(a,b)== expect

if __name__ == '__main__':
    pytest.main(['-s','test_4.py'])

標記為失敗(xfail)

import pytest

data_1 = [[1,2,3],
          pytest.param(3,4,8,marks=pytest.mark.xfail)]

def add(a,b):
    return a+b

@pytest.mark.parametrize("a,b,expect",data_1)
def test_pa(a,b,expect):
    print('測試數據為{},{}'.format(a,b))
    assert add(a,b)== expect

if __name__ == '__main__':
    pytest.main(['-s','test_4.py'])

 

增加可讀行:使用參數ids

import pytest
data_1 = [
    (1, 2, 3),
    (4, 5, 9)
]
ids = ["a:{} + b:{} = expect:{}".format(a, b, expect) for a, b, expect in data_1]
def add(a, b):
    return a + b
@pytest.mark.parametrize('a, b, expect', data_1, ids=ids)
class TestParametrize(object):
    def test_parametrize_1(self, a, b, expect):
        print('\n測試函數1測試數據為\n{}-{}'.format(a, b))
        assert add(a, b) == expect
    def test_parametrize_2(self, a, b, expect):
        print('\n測試函數2數據為\n{}-{}'.format(a, b))
        assert add(a, b) == expect
if __name__ == '__main__':
    pytest.main(['-v','test_4.py'])  # -v : 更加詳細的輸出測試結果

說明:我運行了但是並沒有打印出更詳細的結果

自定義id做標識

import pytest

data_1 = [
    pytest.param(1, 2, 3, id="(a+b):pass"),  # id的值可以自定義, 只要方便理解每個用例是干什么的即可
    pytest.param(4, 5, 10, id="(a+b):fail")
]

def add(a, b):
    return a + b

class TestParametrize(object):
    @pytest.mark.parametrize('a, b, expect', data_1)
    def test_parametrize_1(self, a, b, expect):
        assert add(a, b) == expect

if __name__ == '__main__':
    pytest.main(['-v','test_4.py'])

總結

Pytest中實現數據驅動就是如此了

掌握

1.裝飾器與測試用例使用單個變量接收多組數據與多個變量接收多個數據的訪問方法

2.不同測試數據形式(列表嵌套元組,列表,字典等)時,如何傳遞數據及訪問數據

3.裝飾器裝飾測試類和測試函數的不同之處:裝飾測試類時,類內所有的方法必須接送測試數據,否則會報錯,裝飾測試函數時比較靈活,如果函數不使用數據就可以不裝飾

4.為了輸出結果的可讀性,可以選擇使用ids參數,與測試數據中定義id參數值來標識測試用例

注意

1. 裝飾器的第一個參數是一個列表形式的字符串參數"a, b, c" 不能寫成"a", "b", "c"

2. ids是個字符串列表,它的長度需要與測試數據列表的長度一致

 

 

 

 

 

pytest參數化:pytest.mark.parametrize

https://www.cnblogs.com/peiminer/p/9507111.html

https://www.cnblogs.com/linuxchao/archive/2019/07/25/linuxchao-pytest-parametrize.html

 

unittest參數化:paramunittest

https://www.cnblogs.com/yoyoketang/p/8856362.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM