Pytest夹具装置(fixture)-上篇

简介: Pytest夹具装置(fixture)-上篇

官方文档说:

测试也不必局限于单个装置。它们可以依赖于您想要的任意多个装置,装置也可以使用其他装置。这才是pytest的夹具系统真正闪耀的地方。

如果能让事情变得更干净,不要害怕拆散。

入门操作

import pytest
@pytest.fixture()
def func():
    print("我是前置 func")
    yield 12
    print("我是后置 func")
def test_data(func):
    assert func == 12
    print("我是 test_data")
def test_func(func):
    print("我是func函数")
(venv) D:\Python_test\pythonpp\pytest_>pytest -vs test_b.py
================================================================================= test session starts =================================================================================
platform win32 -- Python 3.9.5, pytest-7.2.0, pluggy-1.0.0 -- D:\Python_test\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\Python_test\pythonpp\pytest_, configfile: pytest.ini
plugins: ordering-0.6
collected 2 items
test_b.py::test_data 我是前置 func
我是 test_data
PASSED我是后置 func
test_b.py::test_func 我是前置 func
我是func函数
PASSED我是后置 func
================================================================================== 2 passed in 0.05s =======================
  1. 与 setup、teardown 类似,提供了测试执行前和执行后的动作处理,但是相对来说又比 setup、teardown 好用
  2. fixture 通过 yield 来区分前后置,前后置可以单独存在;fixture 如果有后置,都会执行后置(除非前置报错)
  3. fixture 可用于封装数据,也可用于封逻辑动作,使用范围非常广
  4. fixture 可用于代码模块化、数据处理、流程设计等

fixture的执行方式

函数引用

import pytest
@pytest.fixture()
def func():
    print("我是前置 func")
    yield 12
    print("我是后置 func")
def test_data(func):
    assert func == 12
    print("我是 test_data")

自动适配-所有用例

import pytest
@pytest.fixture(autouse=True)
def func():
    print("我是前置 func")
    yield 12
    print("我是后置 func")
def test_func():
    print("我是func函数")
test_b.py::test_func 我是前置 func
我是func函数
PASSED我是后置 func

usefixtures-手动调用

import pytest
@pytest.fixture
def func():
    print("我是前置 func")
    yield 12
    print("我是后置 func")
@pytest.mark.usefixtures('func')
def test_func():
    print("我是func函数")
test_b.py::test_func 我是前置 func
我是func函数
PASSED我是后置 func

在类中使用及几种其他方法

"""conftest.py"""
import pytest
@pytest.fixture
def user_name():
    name = "清安"
    print("HELLO,清安")
    yield name
    print("BY !")
import pytest
@pytest.mark.usefixtures("user_name")
class Test_user:
    def test_name(self):
        print("Hello")
    def test_user(self, user_name):
        print("user_name", user_name)
test_a.py::Test_user::test_name HELLO,清安
Hello
PASSEDBY !
test_a.py::Test_user::test_user HELLO,清安
user_name  清安
PASSEDBY !

学会了吗。上述中你也可以指定多个usefixtures。

@pytest.mark.usefixtures("user_name","user_age")
class Test_user:
 ...

也可以写成这样:

import pytest
# @pytest.mark.usefixtures("user_name")
class Test_user:
    pytestmark = pytest.mark.usefixtures("user_name")
    def test_name(self):
        print("Hello")
    def test_user(self,user_name):
        print(user_name)

最后一种方式就是将usefixtures写在pytest.ini配置文件中:

[pytest]
usefixtures = user_name
import pytest
# @pytest.mark.usefixtures("user_name")
class Test_user:
    # pytestmark = pytest.mark.usefixtures("user_name")
    def test_name(self):
        print("Hello")
    def test_user(self,user_name):
        print(user_name)

综合示例--注意点

上述例子中,皆可以在测试类中作用到,且可以同时使用:

import pytest
@pytest.fixture(autouse=True)
def func_a():
    print("我是前置 func_a")
    yield 12
    print("我是后置 func_a")
@pytest.fixture()
def func_b():
    print("我是前置 func_b")
    yield 12
    print("我是后置 func_b")
@pytest.mark.usefixtures('func_b')
def test_func():
    print("我手动使用了--func_b--夹具")
class Test_A:
    @pytest.mark.usefixtures('func_b')
    def test_a(self):
        print("我手动使用了--func_b--夹具")
class Test_B:
    def test_b(self):
        print("我被--func_a--自动适配了")
test_b.py::test_func 我是前置 func_a
我是前置 func_b
我手动使用了--func_b--夹具
PASSED我是后置 func_b
我是后置 func_a
test_b.py::Test_A::test_a 我是前置 func_a
我是前置 func_b
我手动使用了--func_b--夹具
PASSED我是后置 func_b
我是后置 func_a
test_b.py::Test_B::test_b 我是前置 func_a
我被--func_a--自动适配了
PASSED我是后置 func_a

:::warning 上述示例中,体现了一个不好的一点,就是自动适配的夹具会优先使用,且手动使用的夹具还会再使用一次,应该主动避免这种事情,除非特殊要求。:::

夹具并不局限于一个

💥测试和夹具并不局限于 「请求」 一次一个固定装置。他们可以想要多少就要求多少。且可以重复使用。

import pytest
@pytest.fixture
def first_func():
    return '拾贰'
@pytest.fixture()
def second_func():
    return '清安'
@pytest.fixture()
def name_func():
    return [first_func, second_func]
def test_func(name_func):
    assert name_func == [first_func, second_func]
test_b.py::test_func PASSED

一个测试用例也可以使用多个夹具

import pytest
@pytest.fixture()
def my_name():
    yield "清安"
@pytest.fixture()
def your_name():
    yield "拾贰"
def test_our(my_name,your_name):
    print("名字有:",my_name,your_name)
"""
test_a.py::test_our 名字有: 清安 拾贰
PASSED
"""

如果不明白夹具

def first_name():
    return "清安"
def order(first_name):
    return [first_name]
def test_name(order):
    order.append("拾贰")
    assert order == ["清安", "拾贰"]
entry = first_name()
the_list = order(first_name=entry)
test_name(order=the_list)

如上代码近似于fixtrue的作用,如果还不明白,请回头学习函数。

注意点

import pytest
@pytest.fixture
def first_func():
    return 12
def test_first_func():
    print(first_func)
    assert first_func == 12
def test_second_func(first_func):
    print(first_func)
    assert first_func == 12
@pytest.mark.usefixtures('first_func')
def test_func(first_func):
    print(first_func)
    assert first_func == 12
    print("我是func函数")
test_b.py::test_first_func <function first_func at 0x0000027A41C64C10>
FAILED
test_b.py::test_second_func 12
PASSED
test_b.py::test_func 12
我是func函数
PASSED

🔴test_first_func,断言失败了,为什么,看打印信息是一个内存地址,并不是一个具体的值,所以会出现断言失败的情况。如何解决上述已经给到答案了,调用。

目录
相关文章
|
XML Java 数据库连接
【Spring Boot】使用MyBatis注解实现数据库操作
MyBatis还提供了注解的方式,相比XML的方式,注解的方式更加简单方便,无须创建XML配置文件。接下来好好研究注解的使用方式。
514 0
【Spring Boot】使用MyBatis注解实现数据库操作
|
数据采集 存储 JavaScript
(2024)豆瓣电影详情内容爬虫详解和源码
这是一个Python爬虫程序,用于抓取豆瓣电影详情页面如`https://movie.douban.com/subject/1291560/`的数据。它首先发送GET请求,使用PyQuery解析DOM,然后根据`&lt;br&gt;`标签分割HTML内容,提取电影信息如导演、演员、类型等,并将中文键转换为英文键存储在字典中。完整代码包括请求、解析、数据处理和测试部分。当运行时,会打印出电影详情,如导演、演员列表、类型、时长等。
412 1
 (2024)豆瓣电影详情内容爬虫详解和源码
|
9月前
|
机器学习/深度学习 数据可视化 算法
YOLOv11改进目录一览 | 涉及卷积层、轻量化、注意力、损失函数、Backbone、SPPF、Neck、检测头等全方位改进
YOLOv11改进目录一览 | 涉及卷积层、轻量化、注意力、损失函数、Backbone、SPPF、Neck、检测头等全方位改进
1574 6
YOLOv11改进目录一览 | 涉及卷积层、轻量化、注意力、损失函数、Backbone、SPPF、Neck、检测头等全方位改进
|
druid Java 关系型数据库
Spring Boot2 系列教程(二十五)Spring Boot 整合 Jpa 多数据源
Spring Boot2 系列教程(二十五)Spring Boot 整合 Jpa 多数据源
1364 0
|
编解码 搜索推荐 前端开发
字节跳动出大招!IconPark图标库,自定义图标,好用到停不下来!
【11月更文挑战第10天】IconPark 是字节跳动推出的一款高质量图标库,提供超过 2400 个图标,涵盖 32 种分类,支持在线编辑颜色、线条粗细等属性,提供 SVG 和 PNG 格式下载,支持 React、Vue3 等组件代码导出,开源免费商用,适用于网页、移动和桌面应用。
643 4
|
调度 Docker 容器
docker swarm创建覆盖网络
【10月更文挑战第16天】
177 5
|
机器学习/深度学习 传感器 数据采集
深度学习之时空预测
基于深度学习的时空预测是一种利用深度学习模型进行时间和空间数据的联合建模与预测的方法。时空预测模型被广泛应用于交通流量预测、气象预报、环境监测、城市计算、疫情传播等多个领域。
538 1
|
机器学习/深度学习 数据采集 算法
Python基于OpenCV和卷积神经网络CNN进行车牌号码识别项目实战
Python基于OpenCV和卷积神经网络CNN进行车牌号码识别项目实战
|
消息中间件 NoSQL 调度
Django后端架构开发:Django 与 Celery 的深度集成
Django后端架构开发:Django 与 Celery 的深度集成
756 0
|
SQL 分布式计算 Hadoop
利用Hive与Hadoop构建大数据仓库:从零到一
【4月更文挑战第7天】本文介绍了如何使用Apache Hive与Hadoop构建大数据仓库。Hadoop的HDFS和YARN提供分布式存储和资源管理,而Hive作为基于Hadoop的数据仓库系统,通过HiveQL简化大数据查询。构建过程包括设置Hadoop集群、安装配置Hive、数据导入与管理、查询分析以及ETL与调度。大数据仓库的应用场景包括海量数据存储、离线分析、数据服务化和数据湖构建,为企业决策和创新提供支持。
1765 1