超强干货,Pytest自动化测试框架fixture固件使用,0-1精通实战


前言

如果有以下场景:用例 1 需要先登录,用例 2 不需要登录,用例 3 需要先登录。很显然无法用 setup 和 teardown 来实现了

fixture 可以让我们自定义测试用例的前置条件

fixture 的优势

命名方式灵活,不局限于 setup 和teardown 这几个命名;
conftest.py 配置里可以实现数据共享,不需要 import 就能自动找到fixture;
scope=“module” 可以实现多个.py 跨文件共享前置;
scope=“session” 以实现多个.py 跨文件使用一个 session 来完成多个用例;

fixture 参数列表

@pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None)
def test():
    print("fixture初始化的参数列表")

参数列表
scope:可以理解成 fixture 的作用域,默认:function,还有 class、module、package、session 四个【常用】

autouse:默认:False,需要用例手动调用该 fixture;如果是 True,所有作用域内的测试用例都会自动调用该 fixture

name:默认:装饰器的名称,同一模块的 fixture 相互调用建议写个不同的name

注意:
session 的作用域:是整个测试会话,即开始执行 Pytest 到结束测试

测试用例如何调用 fixture

三种方式:
将 fixture 名称作为测试用例函数的输入参数;
测试用例加上装饰器:@pytest.mark.usefixtures(fixture_name);
fixture 设置 autouse=True;

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest

# 调用方式一
@pytest.fixture
def login():
    print("输入账号,密码先登录")


def test_s1(login):
    print("用例 1:登录之后其它动作 111")


def test_s2():  # 不传 login
    print("用例 2:不需要登录,操作 222")


# 调用方式二
@pytest.fixture
def login2():
    print("please输入账号,密码先登录")


@pytest.mark.usefixtures("login2", "login")
def test_s11():
    print("用例 11:登录之后其它动作 111")


# 调用方式三
@pytest.fixture(autouse=True)
def login3():
    print("====auto===")


# 不是test开头,加了装饰器也不会执行fixture
@pytest.mark.usefixtures("login2")
def loginss():
    print(123)

注意:
在类声明上面加@pytest.mark.usefixtures(),代表这个类里面所有测试用例都会调用该 fixture;

可以叠加多个@pytest.mark.usefixtures(),先执行的放底层,后执行的放上层;

可以传多个 fixture 参数,先执行的放前面,后执行的放后面;

如果 fixture 有返回值,用@pytest.mark.usefixtures()是无法获取到返回值的,必须用传参的方式(方式一);

fixture 的实例化顺序

较高 scope 范围的 fixture(session)在较低 scope 范围的 fixture( function 、 class )之前实例化【session > package > module > class > function】

具有相同作用域的 fixture 遵循测试函数中声明的顺序,并遵循 fixture 之间的依赖关系【在 fixture_A 里面依赖的 fixture_B 优先实例化,然后到 fixture_A 实例化】

自动使用(autouse=True)的 fixture 将在显式使用(传参或装饰器)的 fixture 之前实例化

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest

order = []

@pytest.fixture(scope="session")
def s1():
    order.append("s1")


@pytest.fixture(scope="module")
def m1():
    order.append("m1")


@pytest.fixture
def f1(f3, a1):
    # 先实例化f3, 再实例化a1, 最后实例化f1
    order.append("f1")
    assert f3 == 123


@pytest.fixture
def f3():
    order.append("f3")
    a = 123
    yield a


@pytest.fixture
def a1():
    order.append("a1")


@pytest.fixture
def f2():
    order.append("f2")


def test_order(f1, m1, f2, s1):
    # m1、s1在f1后,但因为scope范围大,所以会优先实例化
    assert order == ["s1", "m1", "f3", "a1", "f1", "f2"]

关于 fixture 的注意点

添加了 @pytest.fixture ,如果 fixture 还想依赖其他 fixture,需要用函数传参的方式,不能用@pytest.mark.usefixtures()的方式,否则会不生效

@pytest.fixture(scope="session")
def open():
    print("===打开浏览器===")

@pytest.fixture
# @pytest.mark.usefixtures("open") 不可取!!!不生效!!!
def login(open):
    # 方法级别前置操作setup
    print(f"输入账号,密码先登录{open}")

前面其实都是 setup 的操作,下面就来讲解 teardown 实现

fixture 之 yield 实现 teardown

用 fixture 实现 teardown 并不是一个独立的函数,而是用 yield 关键字来开启 teardown 操作

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest


@pytest.fixture(scope="session")
def open():
    # 会话前置操作setup
    print("===打开浏览器===")
    test = "测试变量是否返回"
    yield test
    # 会话后置操作teardown
    print("==关闭浏览器==")


@pytest.fixture
def login(open):
    # 方法级别前置操作setup
    print(f"输入账号,密码先登录{open}")
    name = "==我是账号=="
    pwd = "==我是密码=="
    age = "==我是年龄=="
    # 返回变量
    yield name, pwd, age
    # 方法级别后置操作teardown
    print("登录成功")


def test_s1(login):
    print("==用例1==")
    # 返回的是一个元组
    print(login)
    # 分别赋值给不同变量
    name, pwd, age = login
    print(name, pwd, age)
    assert "账号" in name
    assert "密码" in pwd
    assert "年龄" in age


def test_s2(login):
    print("==用例2==")
    print(login)

yield 注意事项

如果 yield 前面的代码,即 setup 部分已经抛出异常了,则不会执行 yield 后面的 teardown 内容

如果测试用例抛出异常,yield 后面的 teardown 内容还是会正常执行

yield + with 的结合

# 官方例子
@pytest.fixture(scope="module")
def smtp_connection():
    with smtplib.SMTP("smtp.gmail.com", 587, timeout=5) as smtp_connection:
        yield smtp_connection  # provide the fixture value

该 smtp_connection 连接将测试完成执行后已经关闭,因为 smtp_connection 对象自动关闭时, with 语句结束

addfinalizer 终结函数

@pytest.fixture(scope="module")
def test_addfinalizer(request):
    # 前置操作setup
    print("==再次打开浏览器==")
    test = "test_addfinalizer"

    def fin():
        # 后置操作teardown
        print("==再次关闭浏览器==")

    request.addfinalizer(fin)
    # 返回前置操作的变量
    return test


def test_anthor(test_addfinalizer):
    print("==最新用例==", test_addfinalizer)

注意:
如果 request.addfinalizer() 前面的代码,即 setup 部分已经抛出异常了,则不会执行 request.addfinalizer() 的 teardown 内容(和 yield 相似,应该是最近新版本改成一致了)

可以声明多个终结函数并调用

下面是我整理的2023年最全的软件测试工程师学习知识架构体系图

一、Python编程入门到精通

请添加图片描述

二、接口自动化项目实战

请添加图片描述

三、Web自动化项目实战在这里插入代码片

请添加图片描述

四、App自动化项目实战

请添加图片描述

五、一线大厂简历

请添加图片描述

六、测试开发DevOps体系

请添加图片描述

七、常用自动化测试工具

请添加图片描述

八、JMeter性能测试

请添加图片描述

九、总结(尾部小惊喜)

只有拼尽全力,才能迎来未来的辉煌;只有永不放弃,才能创造人生的奇迹;只有勇往直前,才能成就自己的梦想;让努力成为你每一天的座右铭,坚定迈向成功的道路!

岁月无常,时光荏苒,唯有奋斗不懈,方能拥抱未来的辉煌。在困境中坚持,永不放弃,才能让意志之火燃烧出更加耀眼的光芒,迎接生命的绚丽风景。相信自己的力量,勇往直前,书写属于自己的壮丽篇章!

只有在坚持不懈的奋斗中,才能超越自我、创造辉煌;世界会向你敞开大门,只要你持之以恒、勇往直前。相信自己,追逐梦想,成为最好的自己!

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值