pytest-fixture作用域

前言

经过之前的例子,你会发现fixture只作用在选定的测试用例,那如果只对整个测试类或者测试模块执行一次前、后置,fixture里面也提供了scope参数(默认为function)来设置作用范围

scope可选参数:function、class、module、session

那么接下来一一介绍各个参数的使用

 

fixture-scope参数

1、function:作用于函数级别,应用在单个测试函数,设定的测试函数运行前后都会执行前、后置的代码(不设置默认为function

复制代码
import pytest
@pytest.fixture(scope='function')
def add():
    print('---测试执行---')
    yield
    print('---测试结束---')

class Test_add():
    def test_01(self, add):
        print('用例一')
    def test_02(self):
        print('用例二')
    def test_03(self,add):
        print('用例三')

if __name__ == '__main__':
    pytest.main()
复制代码

 输出结果:

 

 

 可以看到不管测试类中的测试用例,还是测试类外的测试用例,只要调用了fixture,都打印出了对应的前后置输出

 

2、class:作用于类级别,应用在每个测试类,设定的测试函数所包含的测试类,测试类前后只执行一次前、后置代码,但测试类外函数仍旧照常,只有选的调用fixture的类外测试用例执行前、后置代码

复制代码
import pytest

@pytest.fixture(scope='class')
def add():
    print('---测试执行---')
    yield
    print('---测试结束---')

def test_04(add):
    print('类外用例4')
def test_05():
    print('类外用例5')
def test_06(add):
    print('类外用例6')

class Test_add():
    def test_01(self, add):
        print('用例一')
    def test_02(self):
        print('用例二')
    def test_03(self,add):
        print('用例三')

if __name__ == '__main__':
    #命令行参数--setup-show可以来查看详细的fixture信息
    pytest.main()
复制代码

 

输出结果:

 

 

 可以看到测试类中只要有一个测试函数调用了fixture,那么执行时候,这个测试类前后就会执行一次前后置的输出,如果是测试类外的测试用例调用,那么只有调用fixture的测试用例才会执行前后置输出

 

3、module:作用于模块级别,每个.py文件只调用一次,如果fixture选定的测试函数位于测试类中,类外测试用例未选定,那么作用域会被限定在测试类中。将类外测试函数也选定,那么作用域就扩大整个模块,此模块执行前后,只执行一次前后置代码

3.1、只在类中的测试用例中选定fixture:

复制代码

import pytest

@pytest.fixture(scope='module')
def add():
    print('---测试执行---')
    yield
    print('---测试结束---')

def test_04():
    print('类外用例4')
def test_05():
    print('类外用例5')
class Test_add():
    def test_01(self,add):
        print('用例一')
    def test_02(self):
        print('用例二')

class Test_add1():
    def test_01(self):
        print('---用例一')
    def test_02(self):
        print('---用例二')

if __name__ == '__main__':
    #命令行参数--setup-show可以来查看详细的fixture信息
    pytest.main()
复制代码

 

输出结果:

 

 

 

 可以看到作用域会被限定在测试类中,哪怕只有其中一个测试类中的测试用例选定了fixture,也是所有的测试类被包含在其中,类外的测试用例并没有包含进去

3.2、只在类外的测试用例中选定fixture:

复制代码
import pytest

@pytest.fixture(scope='module')
def add():
    print('---测试执行---')
    yield
    print('---测试结束---')

def test_04(add):
    print('类外用例4')

class Test_add():
    def test_01(self):
        print('用例一')
    def test_02(self):
        print('用例二')

class Test_add1():
    def test_01(self):
        print('---用例一')
    def test_02(self):
        print('---用例二')


if __name__ == '__main__':
    #命令行参数--setup-show可以来查看详细的fixture信息
    pytest.main()
复制代码

 

输出结果:

 

 

 可以看到fixture的作用域是整个py文件(module),只执行了一次前后置输出

 

4、session:作用于会话级别,整个会话只执行一次,即运行项目时,整个过程只执行一次(session:一般放在根目录下,然后作用域全局,只执行一次),一般写到conftest.py(后续文章会介绍)中全局调用

文件目录:

 

 

 conftest.py内容(设置session级别的fixture):

#conftest.py
import pytest

@pytest.fixture(scope='session')
def add():
    print('session的fixture执行')
    yield
    print('session的fixture结束')

 

接下来编写测试用例

test_demo1.py内容:

import pytest

def test_02(add):
    print('类外测试用例2')

class Test_01():
    def test_01(self):
        print('类中测试用例1')

 

test_demo2.py内容:

import pytest

def test_02():
    print('类外测试用例2')

class Test_02():
    def test_01(self):
        print('类中测试用例1')

 

在当前目录执行pytest -vs,输出结果如下:

 

 可以看到当前路径下所有测试用例都被执行了,且fixture的打印也在最前和最后被执行了一次,这就是session级别的fixture作用域

注意:如果按顺序执行的第一个测试用例模块中(例如上面的test_demo1.py文件)中有类外测试用例,但选定fixture是类中的测试用例,那么这个模块中的类外测试用例,并不会在fixture的作用域中

 

以上就是fixture的4个级别作用域,可以根据自己的需求来选取对应的作用域

posted @   测试-13  阅读(120)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示