pytest入门四:参数化

当测试数据和期望结果不一样,但操作步骤是一样的测试用例可以用上参数化。

@pytest.mark.parametrize(argnames, argvalues, indirect=False, ids=None)

参数说明:

argnames: 一个或多个参数名,用逗号分隔的字符串,如"arg1,arg2,arg3",参数名与用例入参数一致。

**argvalues:**参数值,必须是列表类型;如果有多个参数,则用元组存放值,一个元组存放一组参数值,元组放在列表中。

**indirect:**如果设置成True,则把传进来的参数当函数执行,而不是一个参数。

默认是False

False: 就是parametrize里面argnames 参数当作一个普通的变量,不会去找变量对应函数的名的方法

True: 就是parametrize里面argnames 参数当作函数执行,它会去找对应的函数,去执行

如下:

第一种,test_a就是一个单纯的变量,它接收的就是 "1", "2", "3",将这写数据传给了 test_login1用例。

第二种,test_a就不是一个变量,把它当作一个函数,将 "1", "2", "3" 数据给了test_a函数,函数的返回结果再传给了 test_login2用例。

**ids:**用例的ID,传一个字符串列表,可以标识每一个测试用例,自定义测试数据结果的显示,为了增加可读性,ids的长度需要与测试数据列表的长度一致。

复制代码
import pytest

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


@pytest.mark.parametrize("a,b,expect",[
    [1,2,3],
    [2,3,5],
    [1,5,6]
])
def test_add(a,b,expect):
    assert add(a,b) == expect

@pytest.mark.parametrize("a,b,expect",[
    (1,2,3),
    (2,3,5),
    (1,5,6)
],ids=['test1','test2','test3'])
def test_add2(a,b,expect):
    assert add(a,b) == expect


@pytest.mark.parametrize("data",[
    {'a':1,'b':2,'expect':3},
    {'a':2,'b':2,'expect':4}
])
def test_add3(data):
    assert add(data['a'],data['b']) == data['expect']


@ pytest.mark.parametrize("a,b,expect",[
    pytest.param(1,1,2,id="pass"),
    pytest.param(1.2,.2,2.4,id="fail")
])
def test_add4(a,b,expect):
    assert add(a,b) == expect


# 标记参数化
@pytest.mark.parametrize("test_input,expected", [
    ("3+5", 8),
    pytest.param("6 * 9", 42, marks=pytest.mark.xfail,id='xfial'),
    pytest.param("6*6", 42, marks=pytest.mark.skip)
])
def test_mark(test_input, expected):
    assert eval(test_input) == expected

# 笛卡尔积,组合数据
data_1 = [1, 2, 3]
data_2 = ['a', 'b']


@pytest.mark.parametrize('a', data_1)
@pytest.mark.parametrize('b', data_2)
def test_parametrize_1(a, b):
    print(f'笛卡尔积 测试数据为 : {a},{b}')

@pytest.mark.parametrize("test_a",["1","2","3"],indirect=False)
def test_login1(test_a):
    print(f"测试数据为:{test_a}")

@pytest.fixture()
def test_a(request):
    return "函数:"+request.param

@pytest.mark.parametrize("test_a",["1","2","3"],indirect=True)
def test_login2(test_a):
    print(f"测试数据为:{test_a}")
    

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

结果:

collecting ... collected 25 items

test.py::test_add[1-2-3] PASSED

test.py::test_add[2-3-5] PASSED

test.py::test_add[1-5-6] PASSED

test.py::test_add2[test1] PASSED

test.py::test_add2[test2] PASSED

test.py::test_add2[test3] PASSED

test.py::test_add3[data0] PASSED

test.py::test_add3[data1] PASSED

test.py::test_add4[pass] PASSED

test.py::test_add4[fail] FAILED

test.py::test_mark[3+5-8] PASSED

test.py::test_mark[xfial] XFAIL

test.py::test_mark[6*6-42] SKIPPED (unconditional skip)

test.py::test_parametrize_1[a-1] 笛卡尔积 测试数据为 : 1,a

PASSED

test.py::test_parametrize_1[a-2] 笛卡尔积 测试数据为 : 2,a

PASSED

test.py::test_parametrize_1[a-3] 笛卡尔积 测试数据为 : 3,a

PASSED

test.py::test_parametrize_1[b-1] 笛卡尔积 测试数据为 : 1,b

PASSED

test.py::test_parametrize_1[b-2] 笛卡尔积 测试数据为 : 2,b

PASSED

test.py::test_parametrize_1[b-3] 笛卡尔积 测试数据为 : 3,b

PASSED

test.py::test_login1[1] 测试数据为:1

PASSED

test.py::test_login1[2] 测试数据为:2

PASSED

test.py::test_login1[3] 测试数据为:3

PASSED

test.py::test_login2[1] 测试数据为:函数:1

PASSED

test.py::test_login2[2] 测试数据为:函数:2

PASSED

test.py::test_login2[3] 测试数据为:函数:3

PASSED

相关推荐
努力搬砖的咸鱼1 天前
从零开始搭建 Pytest 测试框架(Python 3.8 + PyCharm 版)
python·pycharm·pytest
FINE!(正在努力!)3 天前
PyTest框架学习
学习·pytest
程序员杰哥3 天前
接口自动化测试之pytest 运行方式及前置后置封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
测试老哥4 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
水银嘻嘻4 天前
07 APP 自动化- appium+pytest+allure框架封装
python·appium·自动化·pytest
天才测试猿4 天前
接口自动化测试之pytest接口关联框架封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
not coder6 天前
Pytest Fixture 详解
数据库·pytest
not coder6 天前
pytest 常见问题解答 (FAQ)
开发语言·python·pytest
程序员的世界你不懂6 天前
(1)pytest简介和环境准备
pytest
not coder6 天前
Pytest Fixture 是什么?
数据库·oracle·pytest