其他分享
首页 > 其他分享> > pytest parametrize参数化

pytest parametrize参数化

作者:互联网

目录

pytest.mark.parametrize , 参数化测试函数

# content of test_expectation.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

类参数化

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

模块参数化

需要多个参数化组合,可以堆叠parametrize装饰器

import pytest


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

标签:parametrize,mark,test,pytest,参数,expected,def
来源: https://www.cnblogs.com/py-zhq/p/16395823.html