Pytest-fixture夹具-上篇
创始人
2024-05-06 05:19:05
0

官方文档说:
测试也不必局限于单个装置。它们可以依赖于您想要的任意多个装置,装置也可以使用其他装置。这才是pytest的夹具系统真正闪耀的地方。
如果能让事情变得更干净,不要害怕拆散。

入门操作

import pytest@pytest.fixture()
def func():print("我是前置 func")yield 12print("我是后置 func")def test_data(func):assert func == 12print("我是 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 itemstest_b.py::test_data 我是前置 func
我是 test_data
PASSED我是后置 functest_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 12print("我是后置 func")def test_data(func):assert func == 12print("我是 test_data")

自动适配-所有用例

import pytest@pytest.fixture(autouse=True)
def func():print("我是前置 func")yield 12print("我是后置 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 12print("我是后置 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 nameprint("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 12print("我是后置 func_a")@pytest.fixture()
def func_b():print("我是前置 func_b")yield 12print("我是后置 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_atest_b.py::Test_A::test_a 我是前置 func_a
我是前置 func_b
我手动使用了--func_b--夹具
PASSED我是后置 func_b
我是后置 func_atest_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 12def test_first_func():print(first_func)assert first_func == 12def 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 == 12print("我是func函数")
test_b.py::test_first_func 
FAILED
test_b.py::test_second_func 12
PASSED
test_b.py::test_func 12
我是func函数
PASSED

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

相关内容

热门资讯

安卓系统自带的网页,功能与特色... 你有没有发现,每次打开安卓手机,那熟悉的系统界面里总有一个默默无闻的小家伙——安卓系统自带的网页浏览...
美咖云系统安卓版,开启智能生活... 你有没有发现,最近手机上多了一个叫“美咖云系统安卓版”的小家伙?它就像一个魔法师,轻轻一点,就能让你...
安卓系统推荐最好的手机,盘点性... 你有没有想过,拥有一部性能卓越的手机,就像是拥有了移动的宝藏库?在这个信息爆炸的时代,一部好手机不仅...
安卓11系统能精简吗,释放潜能 你有没有发现,随着手机越来越智能,系统也越来越庞大?安卓11系统,这个最新的操作系统,是不是也让你觉...
安卓自动重启系统软件,揭秘原因... 手机突然自动重启,是不是感觉整个人都不好了?别急,今天就来和你聊聊这个让人头疼的安卓自动重启系统软件...
苹果手机x刷安卓系统,探索安卓... 你有没有想过,你的苹果手机X竟然也能刷上安卓系统?是的,你没听错,就是那个一直以来都和我们苹果手机X...
安卓系统智商低吗,智商低下的真... 你有没有想过,为什么安卓系统的智商总被调侃得好像有点低呢?是不是觉得它总是慢吞吞的,有时候还犯点小错...
安卓系统手机联系人,揭秘你的社... 你有没有发现,手机里的联系人列表就像是一个小小的社交圈呢?里面藏着我们的亲朋好友、工作伙伴,甚至还有...
安卓系统免费铃声下载,打造个性... 手机里那首老掉牙的铃声是不是让你觉得有点out了呢?别急,今天就来给你支个招,让你轻松给安卓手机换上...
安卓系统用哪个桌面好,打造个性... 你有没有发现,手机桌面可是我们每天都要面对的“脸面”呢?换一个好看的桌面,心情都能跟着好起来。那么,...
虚拟大师是安卓10系统,功能与... 你知道吗?最近在手机圈里,有个新玩意儿引起了不小的轰动,那就是虚拟大师!而且,更让人惊喜的是,这个虚...
安卓系统与苹果优缺点,系统优缺... 说到手机操作系统,安卓和苹果绝对是两大巨头,它们各有各的特色,就像两道不同的美味佳肴,让人难以抉择。...
安卓win双系统主板,融合与创... 你有没有想过,一台电脑如果既能流畅运行安卓系统,又能轻松驾驭Windows系统,那该有多爽啊?没错,...
安卓系统可精简软件,轻松提升手... 你有没有发现,手机里的安卓系统越来越庞大,软件也越装越多,有时候感觉手机就像个“大肚子”,不仅运行速...
安卓系统基于linux的代码,... 你有没有想过,那个陪伴你每天刷抖音、玩游戏、办公的安卓系统,其实背后有着一套复杂的基于Linux的代...
苹果和安卓的拍照系统,谁更胜一... 你有没有发现,现在手机拍照已经成为我们生活中不可或缺的一部分呢?无论是记录生活的点滴,还是捕捉美丽的...
苹果和安卓系统不同吗,系统差异... 你有没有想过,为什么你的手机里装的是苹果的iOS系统,而朋友的手机却是安卓系统呢?这两种系统,看似都...
安卓系统有多少级,揭秘其多级架... 你有没有想过,那个陪伴我们日常生活的安卓系统,它其实有着丰富的层级结构呢?没错,就是那个让我们的手机...
华为鸿蒙系统与安卓的,技术融合... 你知道吗?最近科技圈可是炸开了锅,华为鸿蒙系统与安卓的较量成为了大家热议的话题。这不,今天我就来给你...
什么安卓手机是苹果系统,搭载苹... 你有没有想过,为什么有些人宁愿花大价钱买苹果手机,而有些人却对安卓手机情有独钟呢?其实,这个问题背后...