随笔 - 23  文章 - 2 评论 - 0 阅读 - 4799
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

fixture的优势

fixture是在测试函数运行前后,由pytest执行的外壳函数。fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集,配置测试前系统的初始状态,为批量测试提供数据源,等等。命名方式灵活,不局限于 setup 和teardown 这几个命名

fixture调用方式

1、使用fixture名字作为参数

复制代码
import pytest
@pytest.fixture()
def login():
    print("这是一个登录方法")

def test_1(login):
    print("test01需要登录")

if __name__ == '__main__':
    pytest.main()

============================= test session starts =============================
collecting ... collected 1 item

fixture.py::test_1

============================== 1 passed in 0.09s ==============================
这是一个登录方法
PASSED [100%]test01需要登录

复制代码

2、使用@pytest.mark.usefixtures('fixture') 装饰器

复制代码
import pytest

@pytest.fixture()
def login():
    print("这是一个登录方法")
@pytest.mark.usefixtures(
'login') def test_1(): print("test01需要登录") def test_2(): print("test02需要登录")============================= test session starts ============================= collecting ... collected 3 items fixture.py::test_3 fixture.py::test_1 fixture.py::test_2 ============================== 3 passed in 0.09s ============================== PASSED [ 33%]test03不需要登录 这是一个登录方法 PASSED [ 66%]test01需要登录 PASSED [100%]test02需要登录
复制代码

usefixtures与传fixture区别

如果fixture有返回值,那么usefixture就无法获取到返回值,这个是装饰器usefixture与用例直接传fixture参数的区别。

当fixture需要用到return出来的参数时,只能讲参数名称直接当参数传入,不需要用到return出来的参数时,两种方式都可以

3、单个fixture传递测试数据

复制代码
import pytest
@pytest.fixture()
def fixturefun():
    return (1,2,3,4)

def test_1(fixturefun):
    assert fixturefun[0] == 1

============================= test session starts =============================
collecting ... collected 1 item

fixture.py::test_1

============================== 1 passed in 0.09s ==============================
PASSED [100%]

复制代码

4、多个fixture传递测试数据

复制代码
import pytest
@pytest.fixture()
def fixturefun1():
    return (1,2,3,4)

@pytest.fixture()
def fixturefun2():
    return (1,2,3,4)

def test_1(fixturefun1,fixturefun2):
    assert fixturefun1[0] == fixturefun2[0]

============================= test session starts =============================
collecting ... collected 1 item

fixture.py::test_1

============================== 1 passed in 0.09s ==============================
PASSED [100%]
Process finished with exit code 0

复制代码

 5、Fixture的相互调用

复制代码
 
import pytest
@pytest.fixture()
def fixturefun():
print("第一层fixture")
a = "zhangsan"
return a

@pytest.fixture()
def login(fixturefun):
print("第二层fixture")

class Test_Class1():
def test_two(self,login):
print("直接使用第二层fixture,返回值为{}".format(login))

def test_three(self,fixturefun):
print("直接使用第一层fixture,返回值为{}".format(fixturefun))

if __name__ == '__main__':
pytest.main()

============================= test session starts =============================
collecting ... collected 2 items

test1.py::Test_Class1::test_two
test1.py::Test_Class1::test_three

============================== 2 passed in 0.09s ==============================
第一层fixture      #fixture互相调用时会先找到所有fixture再一层一层往上找
第二层fixture
PASSED [ 50%]直接使用第二层fixture,返回值为None  #当有多层调用,直接被调用的fixture,不会将上一层的返回值自动回传
第一层fixture
PASSED [100%]直接使用第一层fixture,返回值为zhangsan

复制代码

注:

  • 即使fixture之间支持相互调用,但普通函数直接使用fixture是不支持的,一定是在测试函数内调用才会逐级调用生效
  • 有多层fixture调用时,最先执行的是最后一层fixture,而不是先执行传入测试函数的fixture
  • 上层fixture的值不会自动return,这里就类似函数相互调用一样的逻辑
posted on   luoqingqing6  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示