pytest-fixtured自动化测试详解

news2024/10/6 16:24:16

fixture的作用

1.同unittest的setup和teardown,作为测试前后的初始化设置。

fixture的使用

1.作为前置条件使用

2.fixture的的作用范围

1.作为前置条件使用

@pytest.fixture()
def a():
    return 3
 
def test_b(a):
    assert a==3

2.fixture的作用范围
首先实例化更高范围的fixture.默认为scope="function",每个测试函数都会执行一次。

session : 多个.py文件执行时,只调用一次
module: 每一个.py调用一次
class : 每个类调用一次
function: 每个函数调用一次
3.fixture 作为setup/teardown执行
yield 前的在测试之前执行,yield后的在测试完后执行

@pytest.fixture
def a():
    print("setup")
    yield
    print("teardown")
 
def test_b(a):
    print("测试")

4.fixture with

# @pytest.fixture()
# def write_file():
#     f = open("myfile.txt","w")
#     f.write("hello")
#     yield
#     f.close()
@pytest.fixture()
def write_file():
    with open("myfile.txt","w") as f:
        f.write("hello1")
 
def test_write_file(write_file):
    print("ceshi")

5. fixture request可以请求测试的上下文

@pytest.fixture(params=[1,3])
def a(request):
    return request.param
 
def test_b(a):
    assert a in [1,3]

6.fixture 工厂模式

在单个测试中,如果想多次调用该fixture,就可以用工厂模式。fixture的结果返回的不是数据,而是返回生成数据的函数。

@pytest.fixture
def make_customer_record():
    def _make_customer_record(name):
        return {"name": name, "orders": []}
 
    return _make_customer_record
 
 
def test_customer_records(make_customer_record):
    customer_1 = make_customer_record("Lisa")
    customer_2 = make_customer_record("Mike")
    customer_3 = make_customer_record("Meredith")

7.带参数的fixture

params列表有几个值,测试就会执行几次。例params=[1,2],测试用例会执行2次

@pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)])
def data_set(request):
    return request.param
 
 
def test_data(data_set):
    assert data_set in [0,1]

8. 模块化:在一个fixtrue调用另一个fixture

fixture a可以调用另一个fixture c

@pytest.fixture()
def c():
    print("c")
 
@pytest.fixture()
def a(c):
    print("a")
 
def test_b(a):
    pass

9.通过fixture 对测试用例进行分组
pytest在测试运行期间最大程度地减少了fixture的数量。如果有参数化的fixtrue,那么使用它的所有测试将首先使用一个实例执行,然后在创建下一个fixture实例之前调用终结器。除其他外,这简化了对创建和使用全局状态的应用程序的测试。

@pytest.fixture(scope="module", params=[1, 2])
def input_x(request):
    print("setup")
    param = request.param
    yield param
    print("teardown")
 
def test_a(input_x):
    print("test_a")
    assert input_x in [1,2]
 
def test_b(input_x):
    print("test_b")
    assert input_x in [1,2]
 
if __name__ == '__main__':
    pytest.main(['-q','fixture_1.py'])
运行结果
fixture_1.py setup
.test_a
.test_b
teardown
setup
.test_a
.test_b
teardown

如上所示,设置scope='module',会将所有调用该fixture的用例都执行一遍,而fixture只执行了一次。

10.使用类、模块、项目中的fixture

userfixtures,可以调用在其他模块定义好的fixture.

@pytest.mark.usefixtures("x")
class TestCase:
 
    def test_1(self):
        print("test_1")
 
    def test_2(self):
        print("test_2")
 
 
if __name__ == '__main__':
    pytest.main(['-q','test_2.py'])

a.usefixture,可以添加多个fixture

@pytest.mark.usefixtures("a", "b")
def test():
    pass

b.可以使用标记机制的通用功能在测试模块级别指定fixture

pytestmark = pytest.mark.usefixtures("a")
 
def test_a():
 
def test_b():

c.可以将项目中所有测试所需的fixture放入ini文件中

#pytest.ini
 
[pytest]
usefixtures = x
 
#test_case.py
@pytest.mark.usefixtures
def test_a():
    pass

11.自动使用fixture

autouse=True

有时你可能希望自动调用fixture, 而无需显示声明函数参数或usefixtures装饰器。

import pytest
 
 
class DB:
    def __init__(self):
        self.intransaction = []
 
    def begin(self, name):
        self.intransaction.append(name)
 
    def rollback(self):
        self.intransaction.pop()
 
 
@pytest.fixture(scope="module")
def db():
    return DB()
 
 
class TestClass:
    @pytest.fixture(autouse=True)
    def transact(self, request, db):
        db.begin(request.function.__name__)
        yield
        db.rollback()
 
    def test_method1(self, db):
        assert db.intransaction == ["test_method1"]
 
    def test_method2(self, db):
        assert db.intransaction == ["test_method2"]

类级transact夹具标记有autouse = true ,这意味着该类中的所有测试方法都将使用此夹具,而无需在测试函数签名或类级usefixtures修饰器中声明它。

autouse固定装置遵循scope=关键字参数:如果有autouse固定装置,scope='session'则无论定义在何处,都只能运行一次。scope='class'表示它将每节课运行一次,依此类推。
如果在测试模块中定义了自动使用夹具,则其所有测试功能都会自动使用它。
如果在conftest.py文件中定义了自动使用的固定装置,则其目录下所有测试模块中的所有测试都将调用固定装置。
最后,请谨慎使用:如果您在插件中定义了自动使用夹具,它将在安装该插件的所有项目中的所有测试中调用它。如果固定装置仅在某些设置下(例如在ini文件中)仍然可以工作,则这很有用。这样的全局夹具应始终快速确定它是否应该做任何工作,并避免其他昂贵的导入或计算。
请注意,上面的transact灯具很可能是您希望在项目中不启用的灯具。做到这一点的规范方法是将事务处理定义放入conftest.py文件中,而无需使用autouse:

# content of conftest.py
@pytest.fixture
def transact(request, db):
    db.begin()
    yield
    db.rollback()

然后例如通过声明需要让TestClass使用它:

@pytest.mark.usefixtures("transact")
class TestClass:
    def test_method1(self):
        ...

该TestClass中的所有测试方法都将使用事务处理夹具,而模块中的其他测试类或功能将不使用它,除非它们也添加了transact引用。

fixture覆盖

1.对于测试文件夹级别,具有相同名称的fixture可以被覆盖

tests/
    __init__.py
 
    conftest.py
        # content of tests/conftest.py
        import pytest
 
        @pytest.fixture
        def username():
            return 'username'
 
    test_something.py
        # content of tests/test_something.py
        def test_username(username):
            assert username == 'username'
 
    subfolder/
        __init__.py
 
        conftest.py
            # content of tests/subfolder/conftest.py
            import pytest
 
            @pytest.fixture
            def username(username):
                return 'overridden-' + username
 
        test_something.py
            # content of tests/subfolder/test_something.py
            def test_username(username):
                assert username == 'overridden-username'

2.在测试模块中覆盖fixture

tests/
    __init__.py
 
    conftest.py
        # content of tests/conftest.py
        import pytest
 
        @pytest.fixture
        def username():
            return 'username'
 
    test_something.py
        # content of tests/test_something.py
        import pytest
 
        @pytest.fixture
        def username(username):
            return 'overridden-' + username
 
        def test_username(username):
            assert username == 'overridden-username'
 
    test_something_else.py
        # content of tests/test_something_else.py
        import pytest
 
        @pytest.fixture
        def username(username):
            return 'overridden-else-' + username
 
        def test_username(username):
            assert username == 'overridden-else-username'

3.在文件内覆盖

tests/
    __init__.py
 
    conftest.py
        # content of tests/conftest.py
        import pytest
 
        @pytest.fixture
        def username():
            return 'username'
 
        @pytest.fixture
        def other_username(username):
            return 'other-' + username
 
    test_something.py
        # content of tests/test_something.py
        import pytest
 
        @pytest.mark.parametrize('username', ['directly-overridden-username'])
        def test_username(username):
            assert username == 'directly-overridden-username'
 
        @pytest.mark.parametrize('username', ['directly-overridden-username-other'])
        def test_username_other(other_username):
            assert other_username == 'other-directly-overridden-username-other'

4.对于某些测试模块,参数化的夹具被非参数化的版本覆盖,而非参数化的夹具被参数化的版本覆盖。显然,测试文件夹级别也是如此。

tests/
    __init__.py
 
    conftest.py
        # content of tests/conftest.py
        import pytest
 
        @pytest.fixture(params=['one', 'two', 'three'])
        def parametrized_username(request):
            return request.param
 
        @pytest.fixture
        def non_parametrized_username(request):
            return 'username'
 
    test_something.py
        # content of tests/test_something.py
        import pytest
 
        @pytest.fixture
        def parametrized_username():
            return 'overridden-username'
 
        @pytest.fixture(params=['one', 'two', 'three'])
        def non_parametrized_username(request):
            return request.param
 
        def test_username(parametrized_username):
            assert parametrized_username == 'overridden-username'
 
        def test_parametrized_username(non_parametrized_username):
            assert non_parametrized_username in ['one', 'two', 'three']
 
    test_something_else.py
        # content of tests/test_something_else.py
        def test_username(parametrized_username):
            assert parametrized_username in ['one', 'two', 'three']
 
        def test_username(non_parametrized_username):
            assert non_parametrized_username == 'username'

实用技巧

1.conftest.py 共享fixture。如果定义fixture过多且需要多个地方调用,可将fixture放入conftest.py文件中,使用时不需要导入

总结:

感谢每一个认真阅读我文章的人!!!

作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1306066.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Ghidra设置主题和字体大小

文章目录 Ghidra调整主题和字体大小设置主题和全局字体大小调整反汇编页面字体大小 Ghidra调整主题和字体大小 ghidra默认字体小,默认主题白色伤眼,摸索找到了设置主题和字体的方法 设置主题和全局字体大小 启动ghidra后选择edit>theme>configure 下拉框选择主题,右上…

maui下sqlite演示增删改查

数据操作类 有分页 todoitemDatabase.cs: using SQLite; using TodoSQLite.Models;namespace TodoSQLite.Data {public class TodoItemDatabase{SQLiteAsyncConnection Database;public TodoItemDatabase(){}// 初始化数据库连接和表async Task Init(){if (Databa…

科技提升安全,基于YOLOv7【tiny/yolov7/yolov7x】开发构建商超扶梯场景下行人安全行为姿态检测识别系统

在商超等人流量较为密集的场景下经常会报道出现一些行人在扶梯上摔倒、受伤等问题,随着AI技术的快速发展与不断普及,越来越多的商超、地铁等场景开始加装专用的安全检测预警系统,核心工作原理即使AI模型与摄像头图像视频流的实时计算&#xf…

计算机视觉项目实战-驾驶员疲劳检测

😊😊😊欢迎来到本博客😊😊😊 本次博客内容将继续讲解关于OpenCV的相关知识 🎉作者简介:⭐️⭐️⭐️目前计算机研究生在读。主要研究方向是人工智能和群智能算法方向。目前熟悉深度学…

nrm 的使用 可以快速切换下载(npm)镜像,解决资源下载慢和运行失败

nrm是什么? 介绍 nrm(npm registry manager) 是 npm 的镜像源管理工具. 有时候国外资源太慢,使用 nrm 可以快速的在 npm 源之间切换 安装 npm install -g nrm 基本使用 查看可选择的源 nrm ls 切换到对应的镜像源 nrm use 对应的镜像 删除镜像源 nrm del 名字 …

翻译: LLM大语言模型图像生成原理Image generation

文本生成是许多用户正在使用的,也是所有生成式人工智能工具中影响最大的。但生成式人工智能的一部分兴奋点也在于图像生成。目前也开始出现一些可以生成文本或图像的模型,这些有时被称为多模态模型,因为它们可以在多种模式中操作,…

【Matlab】如何将二阶线性微分方程进行Laplace变换得到传递函数

二阶线性微分方程进行Laplace变换 前言正文代码实现 前言 二阶线性微分方程: 一个二阶线性微分方程通常可以写成如下形式: y ′ ′ ( t ) p ( t ) y ′ ( t ) q ( t ) y ( t ) f ( t ) y^{\prime \prime}(t)p(t) y^{\prime}(t)q(t) y(t)f(t) y′′(t)p(t)y′(t)q(t)y(t)f(…

基于Python+WaveNet+MFCC+Tensorflow智能方言分类—深度学习算法应用(含全部工程源码)(三)

目录 前言引言总体设计系统整体结构图系统流程图 运行环境模块实现1. 数据预处理2. 模型构建1)定义模型结构2)优化损失函数 3. 模型训练及保存1)模型训练2)模型保存3)映射保存 相关其它博客工程源代码下载其它资料下载…

Redis系列之简单实现watchDog自动续期机制

在分布锁的实际使用中,可能会遇到一种情况,一个业务执行时间很长,已经超过redis加锁的时间,也就是锁已经释放了,但是业务还没执行完成,这时候其它线程还是可以获取锁,那就没保证线程安全 项目环…

文件操作及函数

什么是文件? 在程序设计中,文件有两种:程序文件和数据文件。 程序文件 包括源程序文件(.c),目标文件(.obj),可执行程序(.exe)。 数据文件 文件的内容不一定是程序&…

mybatis的快速入门以及spring boot整合mybatis(二)

需要用到的SQL脚本: CREATE TABLE dept (id int unsigned PRIMARY KEY AUTO_INCREMENT COMMENT ID, 主键,name varchar(10) NOT NULL UNIQUE COMMENT 部门名称,create_time datetime DEFAULT NULL COMMENT 创建时间,update_time datetime DEFAULT NULL COMMENT 修改…

P1044 [NOIP2003 普及组] 栈——卡特兰数

传送门: P1044 [NOIP2003 普及组] 栈 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/P1044 公式一:递推式(注意开 long long ,然后 先乘完再除,防止下取整) typedef long long ll;…

超过 1450 个 pfSense 服务器因错误链而遭受 RCE 攻击

在线暴露的大约 1450 个 pfSense 实例容易受到命令注入和跨站点脚本漏洞的攻击,这些漏洞如果链接起来,可能使攻击者能够在设备上执行远程代码。 pfSense 是一款流行的开源防火墙和路由器软件,允许广泛的定制和部署灵活性。 它是一种经济高效…

SpringCloud-高级篇(七)

前面在微服务里整合了Seata,下面利用Seata去解决分布式事务的问题,回去学习Seata中的四种解决方案 :首先学习XA模式 (1)XA模式 RM在前面讲的是资源管理器,在XA标准中RM都是由数据库来实现的,数…

【二分查找】【双指针】LeetCode:2565最少得分子序列

作者推荐 【动态规划】【广度优先】LeetCode2258:逃离火灾 本文涉及的基础知识点 二分查找算法合集 有序向量的二分查找,初始化完成后,向量不会修改。 双指针: 用于计算子字符串是s的字符串的子系列。 题目 给你两个字符串 s 和 t 。 你…

19.java绘图

A.Graphics类 Graphics类是java.awt包中的一个类,它用于在图形用户界面(GUI)或其他图形应用程序中进行绘制。该类通常与Component的paint方法一起使用,以在组件上进行绘制操作。 一些Graphics类的常见用法和方法: 在组…

Java基础语法之类和对象

类的定义 类就是对一个实体的属性功能进行描述 类的定义格式 首先要用到class关键字; 其次,类名采用大驼峰形式例如Dog ClassName Student…… 最后,类中包含该类的属性,即成员变量;以及该类的功能&#xff0…

HelpLook VS Confluence:知识管理方面谁更有优势?

多年来,在线协作和文档工具市场一直被Confluence所主导。Confluence由Atlassian于2004年创立,很迅速地成为企业寻求强大而全面的协作解决方案和知识管理的热门选择。然而,随着新工具如Notion和HelpLook的出现,市场格局发生了变化&…

各地加速“双碳”落地,数字能源供应商怎么选?

作者 | 曾响铃 文 | 响铃说 随着我国力争2030年前实现“碳达峰”、2060年前实现“碳中和”的“双碳”目标提出,为各地区、各行业的低碳转型和绿色可持续发展制定“倒计时”时间表,一场围绕“数字能源”、“智慧能源”、“新能源”等关键词的创新探索进…

十六 动手学深度学习v2计算机视觉 ——样式迁移

文章目录 基于CNN的样式迁移 基于CNN的样式迁移 我们通过前向传播(实线箭头方向)计算风格迁移的损失函数,并通过反向传播(虚线箭头方向)迭代模型参数,即不断更新合成图像。 风格迁移常用的损失函数由3部分组…