pytest笔记——fixture作用范围

一、前言

在使用pytest测试框架的时候,会经常使用到fixture,fixture相对灵活,能更好的实现一些用例场景的前置以及后置的操作,但在使用的过程中也经常遇到各种问题,例如我明明已经声明了一个fixture,但在调用的时候却报错找不到,因此记录一下不同参数下fixture的一些作用范围。

二、fixture参数之scope

fixture(scope="function", params=None, autouse=False, ids=None, name=None):

fixture有五个参数,这里主要讲scope参数,该参数决定了fixture的作用范围

scope有四个值,function、class、module、session,如果没传默认取function

1.scope=“function”,指作用范围为函数

复制代码
import pytest

@pytest.fixture(scope="function")
def open():
    print("打开浏览器,并跳转百度")

def test_input1(open):
    print("输入关键词1")

def test_input2(open):
    print("输入关键词2")

def test_input3():
    print("输入关键词3")

if __name__ == '__main__':
    pytest.main(["-s", "test_fixture_01.py"])
复制代码

以上例子,test_input1跟test_input2调用了fixture open,test_input3没调用,所以在运行test_input1跟test_input2前会先运行open,运行test_input3的时候没有运行open。结果如下:

 

 2.scope=“class”,指作用范围为类

复制代码
import pytest

@pytest.fixture(scope="class")
def open():
    print("打开浏览器,并跳转百度")


class TestCase1():
    def test_input1(self, open):
        print("输入关键词1")

    def test_input2(self, open):
        print("输入关键词2")

class TestCase2(): def test_input3(self): print("输入关键词3") if __name__ == '__main__': pytest.main(["-s", "test_fixture_02.py"])
复制代码
测试类TestCase1,在运行前调用了open,TestCase2没调用,所以在运行类TestCase1之前会先运行fixture open,在运行
TestCase2的时候不会运行open。但有一点需要注意的是,尽管测试类TestCase1里的两个函数都调用了open,但open只会运行一次,即一个类最多只会调用1次fixture。
运行结果如下:

 

3.scope=“class”,指作用范围为整个.py文件,在当前.py脚本里面所有用例开始前只执行一次

复制代码
import pytest

@pytest.fixture(scope="module")
def open():
    print("打开浏览器,并跳转百度")


def test_input1(open):
    print("输入关键词1")

def test_input2(open):
    print("输入关键词2")

class TestCase():
    def test_input3(self, open):
        print("输入关键词3")


if __name__ == '__main__':
    pytest.main(["-s", "test_fixture_03.py"])
复制代码

这里整个.py文件里的函数、类里的函数,都调用了open,但由于scope为module,所以只会运行一次open。运行结果如下:

 

4.scope="session",指可以跨.py文件调用,即可以多个py文件共用同一个fixture,一般为session级别时,需要写在配置文件conftest.py里,现有以下文件

 

# conftest.py

import pytest

@pytest.fixture(scope="session")
def open():
    print("打开浏览器,并跳转百度")
复制代码
# test_fixture_01

import pytest


def test_input1(open):
    print("输入关键词1")


if __name__ == '__main__':
    pytest.main(["-s", "test_fixture_01.py"])
复制代码
复制代码
# test_fixture_02

import pytest


def test_input2(open):
    print("输入关键词2")


if __name__ == '__main__':
    pytest.main(["-s", "test_fixture_02.py"])
复制代码
复制代码
# test_fixture_03

import pytest


def test_input3(open):
    print("输入关键词3")


if __name__ == '__main__':
    pytest.main(["-s", "test_fixture_03.py"])
复制代码

想要同时运行test_fixture_01、test_fixture_02、test_fixture_03这三个文件,需要在控制台运行命令

pytest -s test_fixture_01.py test_fixture_02.py test_fixture_03.py,得到结果如下:

 

 

 三、总结

当scope="function"时,作用范围为函数级别

当scope="class"时,作用范围为类级别,一个类最多调用1次fixture

当scope="module"时,作用范围为一个.py文件,一个.py文件最多调用1次fixture

当scope="session"时,作用范围为多个.py文件,所有.py文件最多调用1次fixture

 

 

 

posted @   chanzjj  阅读(247)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示