pytest(二十二)--fixture的作用范围(scope)

本文详细介绍了pytest中fixture的scope参数,包括function、class、module和session四个级别。function级别在每个测试用例前运行,class级别在每个类开始时运行,module级别在每个.py文件开始时运行,而session级别则跨.py文件调用,适用于全局共享资源。此外,还讨论了conftest.py文件在pytest中的角色和作用层级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fixture作用范围

fixture里面有个scope参数可以控制fixture的作用范围:session > module > class > function

fixture(scope="function", params=None, autouse=False, ids=None, name=None):
    """使用装饰器标记fixture的功能
     可以使用此装饰器(带或不带参数)来定义fixture功能。 fixture功能的名称可以在以后使用
     引用它会在运行测试之前调用它:test模块或类可以使用pytest.mark.usefixtures(fixturename标记。
     测试功能可以直接使用fixture名称作为输入参数,在这种情况下,夹具实例从fixture返回功能将被注入。
 
    :arg scope: scope 有四个级别参数 "function" (默认), "class", "module" or "session".
 
    :arg params: 一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它
 
    :arg autouse:  如果为True,则为所有测试激活fixture func 可以看到它。 如果为False(默认值)则显式需要参考来激活fixture
 
    :arg ids: 每个字符串id的列表,每个字符串对应于params 这样他们就是测试ID的一部分。 如果没有提供ID它们将从params自动生成
 
    :arg name:   fixture的名称。 这默认为装饰函数的名称。 如果fixture在定义它的同一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽; 解决这个问题的一种方法是将装饰函数命名
                       “fixture_ <fixturename>”然后使用”@ pytest.fixture(name ='<fixturename>')“”。
  • function 每一个函数或方法都会调用
  • class 每一个类调用一次,一个类可以有多个方法
  • module,每一个.py文件调用一次,该文件内又有多个function和class
  • session 是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module

scope="function"

@pytest.fixture()如果不写参数,默认就是scope=“function”,它的作用范围是每个测试用例之前运行一次,销毁代码在测试用例运行之后运行

#test_fix1.py
import pytest
@pytest.fixture()
def user():
    a="admin"
    print("获取用户名")
    return a
 
@pytest.fixture(scope="function")
def pwd():
    print("获取用户密码")
    p="888888"
    return p
def test_t1(user,pwd):
    print("用户名:{},密码:{}".format(user,pwd))
    assert pwd!=None
def test_t2(pwd):
    print("用户密码:%s"%pwd)
if __name__=="__main__":
    pytest.main(["test_fix1.py"]) 

运行结果:

img

用例放到类里面也一样

#test_fix1.py
import pytest
@pytest.fixture()
def user():
    a="admin"
    print("获取用户名")
    return a
 
@pytest.fixture(scope="function")
def pwd():
    print("获取用户密码")
    p="888888"
    return p
class Test_T:
    def test_t1(self,user,pwd):
        print("用户名:{},密码:{}".format(user,pwd))
        assert pwd!=None
    def test_t2(self,pwd):
        print("用户密码:%s"%pwd)
        assert pwd=="888888"
if __name__=="__main__":
    pytest.main(["test_fix1.py"]) 

运行结果:

img

scope="class"

fixture为class级别的时候,如果一个class里面有多个用例,都调用了此fixture,那么此fixture只在该class里所有用例开始前执行一次。

#test_fix1.py
import pytest
@pytest.fixture()
def user():
    a="admin"
    print("获取用户名")
    return a
 
@pytest.fixture(scope="class")
def pwd():
    print("class获取用户密码")
    p="888888"
    return p
def test_f1(user,pwd):
    print("test_f1,{},{}\n".format(user,pwd))
 
class Test_T:
    def test_t1(self,user,pwd):
        print("test_t1,用户名:{},密码:{}".format(user,pwd))
        assert pwd!=None
    def test_t2(self,pwd):
        print("test_t2,用户密码:%s"%pwd)
        assert pwd=="888888"
if __name__=="__main__":
    pytest.main(["test_fix1.py"])

运行结果:

发现scope=“class”的,在类外面也可以调用

img

scope="module"

fixture为module级别时,在当前.py脚本里所有用例在开启前只执行一次

#test_fix1.py
import pytest
@pytest.fixture(scope="module")
def one():
    a="admin"
    print("获取用户名")
    return a
 
def test_f1(one):
    print("test_f1,{}\n".format(one))
 
class Test_T:
    def test_t1(self,one):
        print("test_t1,名:{}".format(one))
        assert one!=None
    def test_t2(self,one):
        print("test_t2,名:%s"%one)
        assert one
if __name__=="__main__":
    pytest.main(["test_fix1.py"]) 

运行结果:

img

scope="session"

fixture为session级别时可以跨.py模块调用的,也就是当我们有多个.py文件的用例时候,如果多个用例只需调用一次fixture,那就可以设置scope=“session”,并且写到conftest.py文件里。

conftest.py文件名称是固定的,pytest会自动识别该文件。放到工程的根目录下,就可以全局调用了,如果放到某个package包下,那就只在该package内有效。

#conftest.py
import pytest
 
@pytest.fixture(scope="session")
def the():
    print("\n 获取名字,session级别的scope")
    a="huazai"
    return a
#test_f1.py
import pytest
def test_1(the):
     print("\n test_1,名字:{}".format(the))
class Test_C:
    def test_2(self,the):
        print("test_2,名字:%s"%the)
if __name__=="__main__":
    pytest.main("test_f1.py")
#test_f2.py
import pytest
def test_t1(the):
     print("\n test_t1,名字:{}".format(the))
class Test_C:
    def test_t2(self,the):
        print("test_t2,名字:%s"%the)
if __name__=="__main__":
    pytest.main(["test_f2.py"]) 

如果想同时运行test_f1.py, test_f2.py,在cmd执行

pytest -s test_f1.py test_f2.py

运行结果:

img

pytest(二十三)--conftest.py作用范围

前言

一个测试工程下是可以有多个conftest.py的文件,一般在工程根目录放一个conftest.py起到全局作用。

在不同的测试子目录也可以放conftest.py,作用范围只在该层级及以下目录生效。

conftest层级关系

在web_item_py项目工程下建两个子项目(包)QQ、UC,并且每个目录下都放一个conftest.py和__init__.py(python的每个package必须要有__init__.py)

img

案例分析

web_item_py工程下conftest.py文件代码如下:

#web_item_py/conftest.py
# coding:utf-8
import pytest
@pytest.fixture(scope="session")
def begin():
    print("\n打开首页")

QQ目录下conftest.py文件代码如下:

#web_item_py/qq/conftest.py
# coding:utf-8
import pytest
 
@pytest.fixture(scope="session")
def open_baidu():
    print("打开百度页面_session")  

QQ目录下test_1_qq.py代码如下;

#web_item_py/qq/test_1_qq.py
# coding:utf-8
import pytest
def test_q1(begin,open_baidu):
    print("测试用例test_q1")
    assert 1
def test_q2(begin,open_baidu):
    print("测试用例test_q2")
    assert 1
if __name__=="__main__":
    pytest.main(["-s","test_1_QQ.py"]) 

运行QQ目录下test_1_qq.py,结果可以看出,begin和open_baidu是session级别的,只运行一次。

img

UC目录下conftest.py和test_1_UC.py代码如下

#web_item_py/UC/conftest.py
# coding:utf-8
import pytest
@pytest.fixture(scope="function")
def go():
    print("打开搜狗页面")
#web_item_py/UC/test_1_UC.py
# coding:utf-8
import pytest
def test_u1(begin,go):
    print("测试用例u1")
    assert 1
def test_u2(begin,go):
    print("测试用例u2")
    assert 2
def test_u3(begin,open_baidu):
    print("测试用例u3")
    assert 2
if __name__=="__main__":
    pytest.main(["-s","test_1_UC.py"]) 

运行结果:begin起到全局作用,UC目录下的go是function级别,每个用例调用一次。

test_u3(begin,open_baidu)用例不能夸模块调用QQ模块下的open_baidu,所以test_u3用例会运行失败。

img

同时指向web_item_py工程下的所有用例

img

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值