Pytest09-Fixture

Pytest09-Fixture

高清B站视频链接

1 Fixture快速入门

官方原话:
https://docs.pytest.org/en/latest/explanation/fixtures.html#about-fixtures
pytest fixtures被设计为明确的、模块化的以及可扩展的
什么是fixtures
在测试中,fixture为测试提供了已定义的,可靠的和一致的上下文。这可能包括环境(例如,通过已知的参数配置数据库)或内容(例如,数据集)。
Fixtures定义了构成测试准备阶段的步骤和数据。在pytest中,它们是为这些目的而定义的函数。它们也可以用来定义测试执行阶段;这是一个设计复杂用例的强大技术。
测试功能通过参数访问由fixtures设置的服务,状态或其他操作环境。对于测试功能使用的每个fixture,通常在测试功能的定义中都有一个参数(在 fixture后命名)。
**我们可以通过用修饰器@pytest.fixture来告诉pytest某个特定函数是一个fixture **。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 Fixture快速入门
"""

import pytest

@pytest.fixture()
def first_fix():
    # 省略代码
    print("fix执行")
    return ['a']

def test01(first_fix):
    print("用例1执行")
    first_fix.append("b")
    print(first_fix)

def test02(first_fix):
    print("用例2执行")

def test03():
    print("用例3执行")

if __name__ == '__main__':
    pytest.main(['-vs'])

2 autouse

fixture有个参数autouse,默认是false不开启的,可以设置为True,开启自动使用fixutre的功能

@pytest.fixture(autouse=True)
def first_fix():
    # 省略代码
    print("fix执行")
    return ['a']

def test01(first_fix):
    print("用例1执行")
    first_fix.append("b")
    print(first_fix)

def test02():
    print("用例2执行")

def test03():
    print("用例3执行")

if __name__ == '__main__':
    pytest.main(['-vs','test_02_auto_scope.py'])

3 autouse+scope

fixture里还有个参数,scope,
定义被标记方法的作用域session>module>class>function
1.“function” (default):作用于每个测试方法,每个test都运行一次
2.“class”:作用于整个类,每个class的所有test只运行一次 一个类中可以有多个方法
3.“module”:作用于整个模块,每个module的所有test只运行一次;
每一个.py文件调用一次,该文件内又有多个function和class
4."session:作用于整个session(慎用),每个session只运行一次;
是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module

import pytest

@pytest.fixture(scope='session', autouse=True)
def test0():
    print('\n开始执行session')

@pytest.fixture(scope='module', autouse=True)
def test1():
    print('\n开始执行module')


@pytest.fixture(scope='class', autouse=True)
def test2():
    print('\n开始执行class')


@pytest.fixture(scope='function', autouse=True)
def test3():
    print('\n开始执行function')


def test_a():
    print('---用例a执行---')


def test_d():
    print('---用例d执行---')


class Test_Case:
    def test_b(self):
        print('---用例b执行---')

    def test_c(self):
        print('---用例c执行---')


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

4 其他能力

fixture调用fixture

import pytest
# 环境准备
@pytest.fixture
def first_entry():
    return "a"

# 环境准备
@pytest.fixture
def order(first_entry):
    return [first_entry]


def test_string(order):
    # 用例执行
    order.append("b")

    # 断言
    assert order == ["a", "b"]

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

使用多个fixture

import pytest


@pytest.fixture()
def test1():
    a = 'zz'
    print('\n传出a')
    return a


@pytest.fixture()
def test2():
    b = '123456'
    print('传出b')
    return b


def test3(test1, test2):
    u = test1
    p = test2
    assert u == 'zz'
    assert p == '123456'
    print('传入多个fixture参数正确')


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

使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

import pytest

# fixture标记的函数可以用装饰器来指定使用
@pytest.fixture()
def fix1():
    print('\n引入fix1,开始执行function')

@pytest.fixture()
def fix2():
    print('\n引入fix2,开始执行function')

@pytest.mark.usefixtures('fix1')
def test_1():
    print('---用例1执行---')

#1.需要前面标记了fxi1、fix2函数,这里才可以使用
#2.前面标记了fix2函数,如果不引用的话,执行后不会执行fix2函数
@pytest.mark.usefixtures('fix1')
class Test_Case:
    def test_2(self):
        print('---用例2执行---')

    def test_3(self):
        print('---用例3执行---')

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

叠加usefixtures

import pytest


@pytest.fixture()
def fix1():
    print('\n开始执行fix1')


@pytest.fixture()
def fix2():
    print('\n开始执行fix2')


@pytest.mark.usefixtures('fix1')
@pytest.mark.usefixtures('fix2')
def test_1():
    print('---用例1执行---')


@pytest.mark.usefixtures('fix2')
@pytest.mark.usefixtures('fix1')
class Test_Case:
    def test_2(self):
        print('---用例2执行---')

    def test_3(self):
        print('---用例3执行---')


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

fixture 之 params 使用示例

import pytest


def read_list():
    return ['1', '2', '3']


@pytest.fixture(params=read_list())
def get_param(request):
    #request是pytest的内置 fixture ,主要用于传递参数
    return request.param


def test_01(get_param):
    print('测试用例:' + get_param)
    print(get_param)


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

测试技术交流请联系我
在这里插入图片描述
备注CSDN扶摇

【学习软件测试/Python自动化测试技术/领取Python自动化测试学习路线图/简历优化】
视频链接:
课程服务介绍

自动化全栈学习路线图讲解

软件测试面试合集

Python编程刷题合集

Pytest入门到实战

Python接口自动化合集

PythonWeb自动化合集

性能测试合集

Jmeter接口自动化测试实战全集

2023GPT探索发现合集

2024Pytest合集

加微信(备注CSDN扶摇)即可免费领取下面的自动化测试资料和一份软件测试面试宝典
在这里插入图片描述
在这里插入图片描述

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试老宅男扶摇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值