pytest篇7-pytest.fixture()中scope参数解析

01

引言

  1. 上一篇我们学习了confest.py文件的简单使用,也有所了解。可以适用于一些前置操作的场景下使用。目录层级下也可以有多个contest.py。
  2. 实际项目中,conftest文件实际应用中需要结合fixture来使用,今天我们一起学习一下pytest.fixture装饰器的运用。

02

源码/参数含义

大家可以看一下@pytest.fixture()的源码。可以有道翻译一波。

def fixture(
    fixture_function: Optional[_FixtureFunction] = None,
    *,
    scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function",
    '''
    其余源码已省略,可以去官网查看
    '''
  ***scope 默认为function,还有class、module、session****

scope参数

  • scope = ‘function’ -> 所有文件的测试用例执行前都会执行一次
  • scope = ‘class’ -> 测试文件中测试类执行前都会执行一次
  • scope = ‘module’ -> 每一个模块*.py文件执行前都会执行一次
  • scope = ‘session’ -> 所有测试文件执行前执行一次

03

scope = ‘function’

#!/usr/bin/env python
-*- coding:utf-8 -*-
# @Time : 2021/4/7 7:50 下午
@Author : Maynard
@File : test_07ConftestFixture.py
@Software: PyCharm

import pytest
@pytest.fixture(scope='function')
def test_function():
    print('函数开始之前运行')

def test_case_01(test_function):
    print('------测试用例1运行中')

def test_case_02(test_function):
    print('------测试用例2运行中')

if __name__ == '__main__':
    pytest.main(['-s','test_07ConftestFixture.py'])

运行结果:测试用例运行前都会执行一次

pytest篇7-pytest.fixture()中scope参数解析

04

scope = ‘class’

#!/usr/bin/env python
-*- coding:utf-8 -*-
# @Time : 2021/4/7 7:50 下午
@Author : Maynard
@File : test_07ConftestFixture.py
@Software: PyCharm
import pytest
@pytest.fixture(scope='class')
def test_class():
    print('类开始前运行')

class TestDemoClass():
    def test_case_03(self,test_class):
        print('-----测试用例3运行中')

    def test_casse_04(self,test_class):
        print('-----测试用例4运行中')

if __name__ == '__main__':
    pytest.main(['-s','test_07ConftestFixture.py'])

运行结果:类运行前运行一次

pytest篇7-pytest.fixture()中scope参数解析

05

scope = ‘module’

#!/usr/bin/env python
-*- coding:utf-8 -*-
# @Time : 2021/4/7 7:50 下午
@Author : Maynard
@File : test_07ConftestFixture.py
# @Software: PyCharm
import pytest
@pytest.fixture(scope='module')
def test_module():
    print('模块开始前运行')

class TestDemoClass_1():
    def test_case_05(self,test_module):
        print('-----测试用例5运行中')

    def test_casse_06(self,test_module):
        print('-----测试用例6运行中')

class TestDemoClass_2():
    def test_case_07(self,test_module):
        print('-----测试用例7运行中')

    def test_casse_08(self,test_module):
        print('-----测试用例8运行中')
if __name__ == '__main__':
    pytest.main(['-s','test_07ConftestFixture.py'])

运行结果:每个模块执行之前执行一次

pytest篇7-pytest.fixture()中scope参数解析

06

scope = ‘session’

目录层级

conftest (一个package包)
   -- conftest.py
   -- test_47_1.py
   -- test_47_2.py

confest.py

#!/usr/bin/env python
-*- coding:utf-8 -*-
# @Time : 2021/4/6 8:09 下午
@Author : Maynard
@File : conftest.py
@Software: PyCharm

import pytest
@pytest.fixture(scope='session')
def test_session():
    print('-------多个模块的用例,执行前执行一次')

test_47_1.py

#!/usr/bin/env python
-*- coding:utf-8 -*-
# @Time : 2021/4/7 7:56 下午
@Author : Maynard
@File : test_47_1.py
@Software: PyCharm
import pytest
def test_case_10(test_session):
    print('测试用例10')

test_47_2.py

#!/usr/bin/env python
-*- coding:utf-8 -*-
@Time : 2021/4/7 7:56 下午
@Author : Maynard
@File : test_47_2.py
@Software: PyCharm
import pytest
def test_case_11(test_session):
    print('测试用例11')

命令行运行

 pytest -s conftest

运行结果:所有模块(*.py)运行前执行一次

pytest篇7-pytest.fixture()中scope参数解析

07

总结

1、区分不同scope参数,不同的使用场景;

2、pytest.fixture()一般会结合conftest.py文件来使用;

02

PS

1、后续会继续总结pytest框架,个人觉得最强py单元测试框架;

2、大家有问题,可以通过公众号首页添加作者微信、多交流、多沟通;

3、欢迎转载文章,一起进步、一起学习;

Original: https://blog.csdn.net/m0_47127594/article/details/115543329
Author: 拉菲学测试
Title: pytest篇7-pytest.fixture()中scope参数解析

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/773412/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球