pytest系列--parametrize参数化详解

1 parametrize参数化实质上是DDT,即数据驱动测试,下面首先看下不用数据驱动的方式

在test_example.py 文件中编写如下代码:

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

def test_1():
    assert add(3,5)==8

def test_2():
    assert add(2,4)==7

def test_3():
    assert add(5,7)==12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

使用pytest -s 执行结果如下:两个通过,一个失败

(PytestEnv) G:lamb_sourcepytest_example_for_full_documentation	est_demo>pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: G:lamb_sourcepytest_example_for_full_documentation	est_demo
plugins: html-2.1.1, metadata-1.10.0
collected 3 items                                                                                                                                                       

test_example.py .F.

=============================================================================== FAILURES ===============================================================================
________________________________________________________________________________ test_2 ________________________________________________________________________________

    def test_2():
>       assert add(2,4)==7
E       assert 6 == 7
E        +  where 6 = add(2, 4)

test_example.py:11: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_example.py::test_2 - assert 6 == 7
===================================================================== 1 failed, 2 passed in 0.16s ======================================================================
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

2 上述代码中,存在大量重复代码,实质每个测试用例的功能是一样的,只不过是每次的参数不一样,此时就使用数据驱动测试的方式,在pytest中即parametrize参数化

请看如下代码:

import pytest

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


@pytest.mark.parametrize("a,b,c",[(3,5,8),(2,4,7),(5,7,12)])
def test_1(a,b,c):
    assert add(a,b)==c
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

使用pytest -s执行结果如下:这里虽然只写了一个test函数,但是结果仍然显示三个用例,是因为参数化的时候填写了三个元组的数据,这就是参数化,其实叫数据驱动可能更好理解一些,这样可以节省大量的代码

(PytestEnv) G:lamb_sourcepytest_example_for_full_documentation	est_demo>pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: G:lamb_sourcepytest_example_for_full_documentation	est_demo
plugins: html-2.1.1, metadata-1.10.0
collected 3 items                                                                                                                                                       

test_example.py .F.

=============================================================================== FAILURES ===============================================================================
____________________________________________________________________________ test_1[2-4-7] _____________________________________________________________________________

a = 2, b = 4, c = 7

    @pytest.mark.parametrize("a,b,c",[(3,5,8),(2,4,7),(5,7,12)])
    def test_1(a,b,c):
>       assert add(a,b)==c
E       assert 6 == 7
E        +  where 6 = add(2, 4)

test_example.py:9: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_example.py::test_1[2-4-7] - assert 6 == 7
===================================================================== 1 failed, 2 passed in 0.16s ======================================================================
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

3 参数化功能在参数需要组合所有情况的时候,只需要将参数化叠加起来即可

请看如下代码:表示a可以取值2,4,6,而b可以取值1,3,5,而在执行测试用例的时候是将a,b全部可能的值组合起来的

import pytest

@pytest.mark.parametrize("a",[2,4,6])
@pytest.mark.parametrize("b",[1,3,5])
def test_1(a,b):
    print(a,b)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

使用pytest -s执行结果如下:

(PytestEnv) G:lamb_sourcepytest_example_for_full_documentation	est_demo>pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: G:lamb_sourcepytest_example_for_full_documentation	est_demo
plugins: html-2.1.1, metadata-1.10.0
collected 9 items                                                                                                                                                       

test_example.py 2 1
.4 1
.6 1
.2 3
.4 3
.6 3
.2 5
.4 5
.6 5
.

========================================================================== 9 passed in 0.07s ===========================================================================
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

4 当参数化应用在类上时,则此时类的所有测试方法都将使用参数化中的变量

如下代码:

import pytest

@pytest.mark.parametrize("a,b",[(1,2),(3,4),(5,6)])
class TestExample(object):
    
    def test_01(self,a,b):
        print(a,b)
        
    def test_02(self,a,b):
        print(b,a)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

使用pytest -s执行结果如下,显示运行了6个用例

(PytestEnv) G:lamb_sourcepytest_example_for_full_documentation	est_demo>pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: G:lamb_sourcepytest_example_for_full_documentation	est_demo
plugins: html-2.1.1, metadata-1.10.0
collected 6 items                                                                                                                                                       

test_example.py 1 2
.3 4
.5 6
.2 1
.4 3
.6 5
.

========================================================================== 6 passed in 0.06s ===========================================================================
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

5 在使用参数化的过程中也可以使用标记,比如标记为fail或者skip

代码如下:

import pytest

@pytest.mark.parametrize("a,b",[(1,2),(3,4),pytest.param(5,6,marks=pytest.mark.xfail),pytest.param(7,8,marks=pytest.mark.skip)])
class TestExample(object):

    def test_01(self,a,b):
        print(a,b)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

使用pytest -s执行结果如下:

(PytestEnv) G:lamb_sourcepytest_example_for_full_documentation	est_demo>pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: G:lamb_sourcepytest_example_for_full_documentation	est_demo
plugins: html-2.1.1, metadata-1.10.0
collected 4 items                                                                                                                                                       

test_example.py 1 2
.3 4
.5 6
Xs

=============================================================== 2 passed, 1 skipped, 1 xpassed in 0.05s ================================================================
原文地址:https://www.cnblogs.com/yangxiayi1987/p/13948655.html