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

目录
相关文章
|
9月前
|
SQL 安全 测试技术
Pytest夹具装置(fixture)-下篇
Pytest夹具装置(fixture)-下篇
83 0
|
9月前
|
测试技术 Python
Pytest前后置以及fixture实战部分
Pytest前后置以及fixture实战部分
36 0
|
10月前
|
测试技术 C++ Python
【pytest】pytest的几种运行方式,尤其最后一种调试很方便
【pytest】pytest的几种运行方式,尤其最后一种调试很方便
|
负载均衡 监控 测试技术
pytest学习和使用20-pytest如何进行分布式测试?(pytest-xdist)
pytest学习和使用20-pytest如何进行分布式测试?(pytest-xdist)
153 0
pytest学习和使用20-pytest如何进行分布式测试?(pytest-xdist)
|
测试技术 Python
pytest学习和使用6-fixture如何使用?
pytest学习和使用6-fixture如何使用?
85 0
|
测试技术
pytest学习和使用8-fixture如何实现teardown功能?(yield的使用)
pytest学习和使用8-fixture如何实现teardown功能?(yield的使用)
74 0
|
测试技术
pytest学习和使用14-Pytest用例执行结果有哪几种状态?
pytest学习和使用14-Pytest用例执行结果有哪几种状态?
77 0
|
测试技术 Python
pytest学习和使用15-Pytest用例失败如何重跑?(pytest-rerunfailures的简单使用)
pytest学习和使用15-Pytest用例失败如何重跑?(pytest-rerunfailures的简单使用)
70 0
|
测试技术
pytest学习和使用5-Pytest和Unittest中的断言如何使用?
pytest学习和使用5-Pytest和Unittest中的断言如何使用?
70 0
pytest学习和使用5-Pytest和Unittest中的断言如何使用?