pytest二次开发:生成用例参数

@pytest.fixture是一个装饰器,用于声明一个fixture。Fixture是pytest中的一个核心概念,它提供了一种将测试前的准备代码(如设置测试环境、准备测试数据等)和测试后的清理代码(如恢复测试环境、删除临时文件等)与测试用例分离的方法。通过这种方式,测试用例可以更加专注于测试逻辑本身,而不是测试环境的准备和清理。

pytest使用fixture,实现接受一个函数作为输入,函数内部可以包含条件表达式、循环等复杂逻辑,并返回一个参数列表

python 复制代码
import pytest

# 直接传入数组
@pytest.fixture(params=[1,2,3])
def param_complex(request):
    print('--------fixture\t')
    print(request)
    print(request.param)
    return request.param
def test_generatePara(param_complex):
    print('---------param_complex------- test\t')
    print(param_complex)
    assert True




def generate_param(a, b):
    params = []
    ouShu = []
    jiShu = []
    for i in range(a,b):
        if i % 2 == 0:
            ouShu.append(i)
        else:
            jiShu.append(i)
    params.append(ouShu)
    params.append(jiShu)
    return params

#  01 函数执行:generate_param(2,6)在pytest解析fixture声明时执行,生成一个包含多个pytest.param对象的列表
#  02 参数化:测试用例使用param_complex_gene fixture ,pytest将pytest.param对象的列表作为params的值,并为列表中的每个pytest.param对象执行一次测试用例
#  03 fixture函数:对于列表中的每个pytest.param对象,pytest都会调用param_complex_gene fixture函数一次,并将当前的pytest.param对象作为request.param传递给这个函数。然后,param_complex_gene函数返回request.param,即当前的测试参数。
@pytest.fixture(params=generate_param(2,6))
def param_complex_gene(request):
    print('--------fixture param_complex_gene \t')
    print(request)
    print(request.param)
    return request.param

def test_generatePara1(param_complex_gene):

    print('---------param_complex_gene------- test\t')
    print(param_complex_gene)

    assert True

--------fixture

<SubRequest 'param_complex' for <Function test_generatePara1>>

1

PASSED 20%---------param_complex------- test

1

--------fixture

<SubRequest 'param_complex' for <Function test_generatePara2>>

2

PASSED 40%---------param_complex------- test

2

--------fixture

<SubRequest 'param_complex' for <Function test_generatePara3>>

3

PASSED 60%---------param_complex------- test

3

--------fixture param_complex_gene

<SubRequest 'param_complex_gene' for <Function test_generatePara1param_complex_gene0>>

2, 4

PASSED 80%---------param_complex_gene------- test

2, 4

--------fixture param_complex_gene

<SubRequest 'param_complex_gene' for <Function test_generatePara1param_complex_gene1>>

3, 5

PASSED 100%---------param_complex_gene------- test

3, 5

======= Global cleanup =======

相关推荐
2601_962284501 天前
安卓APP UI自动化测试:Python + UiAutomator2 + pytest + pytest-html
python·pytest·uiautomator2·ui自动化测试·安卓app
2601_962300811 天前
基于pytest的接口自动化测试:使用requests模块的实战项目与接口关联方法详解
接口自动化测试·测试用例·pytest·接口关联·requests模块
2601_962300811 天前
Python + Requests + Pytest + Excel + Allure:接口自动化测试项目实战
python·excel·pytest·allure·requests
11路没有终点2 天前
Pytest 安装、版本与第一个测试用例
测试用例·pytest
11路没有终点3 天前
pytest 参数化与 Mark 标记
pytest
2601_962296283 天前
五种Python自动化测试框架汇总,附学习方法
自动化测试·pytest·unittest·robotframework·python框架
11路没有终点4 天前
pytest Fixture 进阶:conftest 与依赖注入
pytest
2601_962387827 天前
web自动化测试实战教程【selenium/unittest/pytest】【共193课
selenium·pytest·devops·web自动化测试·unittest
weixin_440730507 天前
使用pytest中方法控制执行步骤(test_begin.py、test_end.py,@pytest.mark.run(order=1))
开发语言·python·pytest
2601_962297257 天前
Python、Pytest、Allure、Selenium和Jenkins实现自动化测试集成实例
python·selenium·jenkins·pytest·allure