❝官方文档说:
❞
测试也不必局限于单个装置。它们可以依赖于您想要的任意多个装置,装置也可以使用其他装置。这才是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 =======================
❝❞
与 setup、teardown 类似,提供了测试执行前和执行后的动作处理,但是相对来说又比 setup、teardown 好用
fixture 通过 yield 来区分前后置,前后置可以单独存在;fixture 如果有后置,都会执行后置(除非前置报错)
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
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,断言失败了,为什么,看打印信息是一个内存地址,并不是一个具体的值,所以会出现断言失败的情况。如何解决上述已经给到答案了,调用。
❞