Pytest----如何使用parametrize参数化

简介: Pytest----如何使用parametrize参数化

一、对测试函数使用parametrize参数化

如下所示,即传入两个参数,参数化使用元祖的列表

test_demo.py代码如下:

import pytest


@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

执行结果如下:

$ pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 3 items                                                                                                                                                       

test_demo.py ..F

=============================================================================== FAILURES ===============================================================================
__________________________________________________________________________ test_eval[6*9-42] ___________________________________________________________________________

test_input = '6*9', expected = 42

    @pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
    def test_eval(test_input, expected):
>       assert eval(test_input) == expected
E       AssertionError: assert 54 == 42
E        +  where 54 = eval('6*9')

test_demo.py:6: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_demo.py::test_eval[6*9-42] - AssertionError: assert 54 == 42
===================================================================== 1 failed, 2 passed in 0.11s ======================================================================

二、对测试类使用parametrize参数化

对测试类使用参数化,则测试类中的每个测试函数都会使用测试类上的参数列表

test_demo.py代码如下:

import pytest


@pytest.mark.parametrize("n,expected", [(1, 2), (3, 4)])
class TestClass:
    def test_simple_case(self, n, expected):
        assert n + 1 == expected

    def test_weird_simple_case(self, n, expected):
        assert (n * 1) + 1 == expected

执行结果如下:

$ pytest -v
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- D:\Python39\python.exe
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('D:\\src\\blog\\tests\\.hypothesis\\examples')
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 4 items                                                                                                                                                       

test_demo.py::TestClass::test_simple_case[1-2] PASSED                                                                                                             [ 25%]
test_demo.py::TestClass::test_simple_case[3-4] PASSED                                                                                                             [ 50%]
test_demo.py::TestClass::test_weird_simple_case[1-2] PASSED                                                                                                       [ 75%]
test_demo.py::TestClass::test_weird_simple_case[3-4] PASSED                                                                                                       [100%]

========================================================================== 4 passed in 0.19s ===========================================================================

三、通过定义pytestmark对模块内所有测试函数参数化化

通过在模块内定义pytestmark变量,从而可以对模块内的所有测试函数以及测试类中的测试方法进行参数化处理

test_demo.py代码如下:

import pytest

pytestmark = pytest.mark.parametrize("n,expected", [(1, 2), (3, 4)])


def test_01(n,expected):
    assert n + 1 == expected

class TestClass:
    def test_simple_case(self, n, expected):
        assert n + 1 == expected

    def test_weird_simple_case(self, n, expected):
        assert (n * 1) + 1 == expected

执行结果如下:

$ pytest -v
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- D:\Python39\python.exe
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('D:\\src\\blog\\tests\\.hypothesis\\examples')
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 6 items                                                                                                                                                       

test_demo.py::test_01[1-2] PASSED                                                                                                                                 [ 16%]
test_demo.py::test_01[3-4] PASSED                                                                                                                                 [ 33%]
test_demo.py::TestClass::test_simple_case[1-2] PASSED                                                                                                             [ 50%]
test_demo.py::TestClass::test_simple_case[3-4] PASSED                                                                                                             [ 66%]
test_demo.py::TestClass::test_weird_simple_case[1-2] PASSED                                                                                                       [ 83%]
test_demo.py::TestClass::test_weird_simple_case[3-4] PASSED                                                                                                       [100%]

========================================================================== 6 passed in 0.14s ===========================================================================

四、使用parametrize参数的数据中可以使用skip或者xfail函数标记

在使用参数化的过程中,同样可以对具体数据进行xfail或者skip标记

test_demo.py代码如下:

import pytest


@pytest.mark.parametrize(
    "test_input,expected",
    [("3+5", 8), ("2+4", 6), pytest.param("6*9", 42, marks=pytest.mark.xfail)],
)
def test_eval(test_input, expected):
    assert eval(test_input) == expected

执行结果如下:

$ pytest -v
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- D:\Python39\python.exe
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('D:\\src\\blog\\tests\\.hypothesis\\examples')
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 3 items                                                                                                                                                       

test_demo.py::test_eval[3+5-8] PASSED                                                                                                                             [ 33%]
test_demo.py::test_eval[2+4-6] PASSED                                                                                                                             [ 66%]
test_demo.py::test_eval[6*9-42] XFAIL                                                                                                                             [100%]

===================================================================== 2 passed, 1 xfailed in 0.19s =====================================================================

五、使用parametrize参数化对测试数据全排列组合测试

有时需要对所有数据进行排列组合测试,比如x,y,两个测试数据,期望x和y充分的组合测试,如下

test_demo.py代码如下:

import pytest


@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
    assert x<y

执行结果如下:

$ pytest -v
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- D:\Python39\python.exe
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('D:\\src\\blog\\tests\\.hypothesis\\examples')
rootdir: D:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 4 items                                                                                                                                                       

test_demo.py::test_foo[2-0] PASSED                                                                                                                                [ 25%]
test_demo.py::test_foo[2-1] PASSED                                                                                                                                [ 50%]
test_demo.py::test_foo[3-0] PASSED                                                                                                                                [ 75%]
test_demo.py::test_foo[3-1] PASSED                                                                                                                                [100%]

========================================================================== 4 passed in 0.13s ===========================================================================
目录
相关文章
|
测试技术 数据处理
Pytest参数化详解-上
Pytest参数化详解-上
61 0
|
测试技术 Python
pytest--运行指定的测试和参数化
pytest--运行指定的测试和参数化
|
测试技术 数据处理
Pytest参数化详解-下
Pytest参数化详解-下
98 0
|
测试技术
09-pytest-parametrize参数化
09-pytest-parametrize参数化
|
测试技术
pytest学习和使用13-Pytest的fixture如何使用request传入参数?
pytest学习和使用13-Pytest的fixture如何使用request传入参数?
101 0
【pytest】(十二)参数化测试用例中的setup和teardown要怎么写?
【pytest】(十二)参数化测试用例中的setup和teardown要怎么写?
【pytest】(十二)参数化测试用例中的setup和teardown要怎么写?
|
SQL JSON 数据格式
【pytest】(十一)fixture参数化-巧用params和ids的真接口自动化实战
【pytest】(十一)fixture参数化-巧用params和ids的真接口自动化实战
|
测试技术 数据处理 数据库
【pytest】(十)fixture参数化-巧用params和ids优雅的创建测试数据
【pytest】(十)fixture参数化-巧用params和ids优雅的创建测试数据
|
机器学习/深度学习 测试技术 数据安全/隐私保护
Pytest系列(9) - 参数化@pytest.mark.parametrize
Pytest系列(9) - 参数化@pytest.mark.parametrize
297 0
Pytest系列(9) - 参数化@pytest.mark.parametrize