1. fixture的调用的三种方式

  1. 将fixture名称作为测试用例函数参数
  2. 测试用例加上装饰器:@pytest.mark.usefixtures(fixture_name)
  3. fixture设置autouse=True 自动调用(在第十二节已经详解)

例子13-1:

代码:

  1. # -*- coding:utf-8 -*-
  2. # Author:tang_ren_li
  3. # 2022-2-11 22:45
  4. import pytest
  5. @pytest.fixture()
  6. def fixture_1():
  7. print("fixture_1前置")
  8. yield "fixture_1 is call "
  9. print("fixture_1后置")
  10. @pytest.fixture()
  11. def fixture_2():
  12. print("fixture_2前置")
  13. yield "fixture_2 is call "
  14. print("fixture_2后置")
  15. #测试用例函数, 通过函数名称直接使用
  16. def test_case_1(fixture_1):
  17. print("test_case_1 running !")
  18. print(fixture_1)
  19. assert 1
  20. #测试用例函数,使用@pytest.mark.usefixtures 标注需要使用的类
  21. @pytest.mark.usefixtures("fixture_1")
  22. def test_case_2():
  23. print("test_case_2 running !")
  24. print(fixture_1)#这里无法得到fixture_1的返回值
  25. assert 1
  26. @pytest.mark.usefixtures("fixture_1")
  27. class Test_demo_1():#若要再测试类里的测试用例使用fixtures 需要使用@pytest.mark.usefixtures
  28. def test_case_1_A(self,fixture_1):
  29. print("test_case_1_A running !")
  30. print(fixture_1)
  31. assert 1
  32. @pytest.mark.usefixtures("fixture_1","fixture_2")
  33. class Test_demo_2():#这里调用了2fixutre
  34. def test_case_2_A(self,fixture_1):
  35. print("test_case_2_A running !")
  36. print(fixture_1)
  37. assert 1

执行结果:

================= test session starts =============================
collecting … collected 4 items

test_fixt.py::test_case_1
fixture_1前置
PASSED [ 25%]test_case_1 running !
fixture_1 is call
fixture_1后置

test_fixt.py::test_case_2
fixture_1前置
PASSED [ 50%]test_case_2 running !

fixture_1后置

test_fixt.py::Test_demo_1::test_case_1_A
fixture_1前置
PASSED [ 75%]test_case_1_A running !
fixture_1 is call
fixture_1后置

test_fixt.py::Test_demo_2::test_case_2_A

fixture_1前置
fixture_2前置
PASSED [100%]test_case_2_A running !
fixture_1 is call
fixture_2后置
fixture_1后置

======================= 4 passed in 0.03s ===========================

说明:

  1. 如果测试用例不属于类,则使用@pytest.mark.usefixtures 调用,无法取得fixtures的返回值,只能得到函数入口地址

  2. 如果测试用例在类里,需要使用pytest.mark.usefixtures标注需要使用的fixtures,这里传入参数位字符串格式, 类里的测试用例可以单独调用一个fixtures,也可以同时调用多个fixtures

2. fixture的调用顺序

2.1 前言

  1. 12节已经介绍了, fixtures是自动调用的方式的调用顺序,其实就是类似set up tear down的调用顺序和效果。 这里主要介绍自动调用和手动调用的方式混合使用的调用顺序

2.2 调用顺序

例子13-2:


代码:

# -*- coding:utf-8 -*-
# Author:tang_ren_li
# 2022-2-12 22:45


import pytest

call_order = []

@pytest.fixture(scope="session",autouse=True)
def s1_auto():
    call_order.append("s1_auto")


@pytest.fixture(scope="module",autouse=True)
def m1_auto():
    call_order.append("m1_auto")


@pytest.fixture(scope="session")
def s1():
    call_order.append("s1")


@pytest.fixture(scope="module")
def m1():
    call_order.append("m1")


@pytest.fixture
def f1():
    call_order.append("f1")


@pytest.fixture
def f2():
    call_order.append("f2")


@pytest.fixture
def f3():
    call_order.append("f3")
    a = 123
    yield a


@pytest.fixture
def f3_f1(f1, f3):# f3_f1 是依赖于 f3 和 f1

    call_order.append("f3_f1")
    assert f3 == 123 # 可以取得


def test_order(f1, f2,f3_f1, m1, s1):
    # m1、s1在f1后,但因为scope范围大,所以会优先实例化
    print(call_order)

def test_order2(f3_f1):
    # m1、s1在f1后,但因为scope范围大,所以会优先实例化
    print(call_order)

运行结果:

test_fix_1.py::test_order1 PASSED
[ 50%][‘s1_auto’, ‘s1’, ‘m1_auto’, ‘m1’, ‘f1’, ‘f2’]

test_fix_1.py::test_order2 PASSED
[100%][‘s1_auto’, ‘s1’, ‘m1_auto’, ‘m1’, ‘f1’, ‘f2’, ‘f1’, ‘f3’, ‘f3_f1‘][

](https://blog.csdn.net/hekaiyou/article/details/79242391)

说明:

 1.   自动调用fixtures同级别优先于手动调用fixtures, 所以是's1_auto', -->'s1'  

 2.   具有相同作用域的fixture遵循测试函数中声明的顺序,并遵循fixture之间的依赖关系【在fixture_A<br />           里面依赖的fixture_B优先实例化,然后到fixture_A实例化】,  所以是**'f1'-->f3'-->f3_f1', **

 3.   基本调用顺序是范围大的优先, 传参顺序靠前的优先     <br />**          **<br />[<br />   <br />    

](https://blog.csdn.net/hekaiyou/article/details/79242391)

[

](https://blog.csdn.net/qq_30758629/article/details/95923286)
[

](https://blog.csdn.net/qq_34979346/article/details/84324748)

[

](https://blog.csdn.net/qq_30758629/article/details/95923286)

[

](https://blog.csdn.net/weixin_42618073/article/details/120455715)