pytest系列(二) -- fixture详解

声明:参考B站视频,自学成长记录
https://www.bilibili.com/video/BV1u5411A7Um?p=9

一、pytest默认实现前 / 后置条件

setup / teardown 和 setup_class / teardown_class
实现全部用例的前 / 后置条件

方法的前 / 后置条件

setup:每个方法(用例)前执行一次
teardown:每个方法(用例)后执行一次

示例代码

import pytest


class Test_API:

    # 每个用例前执行一次
    def setup(self):
        print('\n方法前置:打开浏览器')

    def test_01(self):
        print('test_01执行')

    def test_02(self):
        print('test_02执行')

    # 每个用例后执行一次
    def teardown(self):
        print('方法后置:关闭浏览器')


执行结果
在这里插入图片描述

类的前 / 后置条件

setup_class:类实例化前执行一次
teardown_class:类执行完毕后执行一次

示例代码

import pytest


class Test_API:
    
    # 类实例化前执行一次
    def setup_class(self):
        print("\n类前置:创建日志、数据库、接口请求对象")

    def test_01(self):
        print('test_01执行')

    def test_02(self):
        print('test_02执行')

    # 类执行完毕后执行一次
    def teardown_class(self):
        print('类后置:销毁日志、数据库、接口请求对象')

执行结果

二、pytest之fixture实现前 / 后置条件

使用@pytest.fixture()装饰器实现 部分 用例的前 / 后置条件

fixture示例

params = [1,2,3,4]

@pytest.fixture(scope="function",params=params,autouse=True,ids="",name="")
def my_fixture():
    print('部门前置')
'''
scope: 被装饰对象my_fixture的作用域 
    默认funciton、class、module、package/session        
params: 参数- 可迭代的[列表 元组 字典]
autouse: 是否全部执行, 默认False
ids: 给params参数每一个值设置变量名
name: 给被装饰的方法设置别名
'''

scope参数

scope=‘function’ 实现方法级别的前 / 后置条件

示例代码

import pytest


@pytest.fixture(scope='function')
def my_fixture():
    print('\nfunction前置')
    yield
    print('function后置')

class Test_API:

    def test_01(self, my_fixture):
        print('test_01执行')

    def test_02(self):
        print('test_02执行')

    def test_03(self):
        print('test_03执行')

执行结果
在这里插入图片描述

scope=‘class’ 实现类级别的前 / 后置条件
在需要前 / 后置条件的类中任意方法引用即可

示例代码

import pytest


@pytest.fixture(scope='class')
def my_fixture():
    print('\nclass前置')
    yield
    print('class后置')


class Test_API1():
	# 在需要前/后置的类中任意方法引用即可	
    def test_01(self, my_fixture):
        print('test_01执行')

    def test_03(self):
        print('test_03执行')


class Test_API2:

    def test_02(self):
        print('test_02执行')


执行结果
在这里插入图片描述

scope=‘module’ 实现模块级别的前 / 后置条件
模块中 任意类下的任意方法有被引用即可

示例代码

import pytest


@pytest.fixture(scope='module')
def my_fixture():
    print('\nmodule前置')
    yield
    print('module后置')


class Test_API1():
	# 模块中 任意类下的任意方法有被引用即可
    def test_01(self, my_fixture):
        print('test_01执行')

    def test_03(self):
        print('test_03执行')


class Test_API2:

    def test_02(self):
        print('test_02执行')

执行结果
在这里插入图片描述

autouse参数

当autouse参数为True时
对应scope作用域的所有方法 / 类 / 模块的前 / 后置条件都生效

示例代码

import pytest


@pytest.fixture(scope='function',autouse=True)
def my_fixture():
    print('\nfunction前置')
    yield
    print('function后置')


class Test_API1():

    def test_01(self):
        print('test_01执行')

    def test_03(self):
        print('test_03执行')


class Test_API2:

    def test_02(self):
        print('test_02执行')

执行结果
在这里插入图片描述

params参数

params 可以实现测试数据的参数化
结合yield可以实现前 / 后置条件

示例代码

import pytest

params = [11, 222, 3333]


@pytest.fixture(params=params)
def my_fixture(request):  # 固定参数名
    # return request.param	# 固定返回值
    print('\n前置')
    yield request.param
    print('后置')


class Test_API1():

    def test_01(self, my_fixture):
        print(' ---> test_01执行{}'.format(my_fixture))

    def test_03(self):
        print('test_03执行')


class Test_API2:

    def test_02(self):
        print('test_02执行')

执行结果
在这里插入图片描述

ids参数

给params参数每一个值设置变量名

示例代码

import pytest

params = [11, 222, 3333]
ids = ['case{}'.format(i) for i in range(len(params))]

@pytest.fixture(params=params, ids=ids)
def my_fixture(request):
    yield request.param


class Test_API1():

    def test_01(self, my_fixture):
        print(' ---> test_01执行{}'.format(my_fixture))

执行结果
在这里插入图片描述

name参数

给被装饰的方法设置别名

示例代码

import pytest

# name参数将被装饰的函数名my_fixture名字设置为fix
@pytest.fixture(params=[1,2,3],name='fix')
def my_fixture(request):
    print('\n前置')
    yield request.param
    print('后置')


class Test_API1():
	# 使用fix即表示使用的my_fixture功能
    def test_01(self, fix):
        print(' ---> test_01执行{}'.format(fix))

执行结果
在这里插入图片描述

三、conftest.py结合@pytest.fixture()

conftest.py 可以让不同py文件使用同一个fixture函数
conftest.py结合@pytest.fixture()实现 全局 的前 / 后置条件效果

目录结构

testcase
	nlp
		conftest.py
		test_nlp.py
	tts
		conftest.py
		test_tts.py
	__init__.py
	conftest.py

在这里插入图片描述

testcase\conftest.py

# -*- coding: utf-8 -*-
# @File : conftest.py
import pytest

@pytest.fixture()
def all_fixture():
    print('\n所有前置')
    yield
    print('所有后置')

testcase\tts\conftest.py

# -*- coding: utf-8 -*-
# @File : conftest.py 

import pytest

@pytest.fixture()
def tts_fixture():
    print('\ntts前置')
    yield
    print('tts后置')

testcase\tts\test_tts.py

# -*- coding: utf-8 -*-
# @File : test_tts.py

class Test_TTS():

    def test_tts01(self, all_fixture ,tts_fixture):
        print('test_nlp01')

testcase\nlp\conftest.py

# -*- coding: utf-8 -*-
# @File : conftest.py 

import pytest

@pytest.fixture()
def nlp_fixture():
    print('\nnlp前置')
    yield
    print('nlp后置')

testcase\nlp\tts_nlp.py

# -*- coding: utf-8 -*-
# @File : test_nlp.py

class Test_NLP():

    def test_nlp01(self, nlp_fixture):
        print('test_nlp01')

执行结果
在这里插入图片描述

总结

1、setup/teardown、setup_class/teardown_class只作用于所有类或方法 的 前 / 后置条件
2、@pytest.fixture()作用于部分全部 前 / 后置条件
3、conftest.py和@pytest.fixture结合使用 作用域 全局前 / 后置条件

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pytest.fixture装饰器是pytest测试框架的一个重要特性,用于定义测试用例的共享资源或者测试环境的初始化和清理操作。通过使用fixture装饰器,我们可以在测试用例方便地使用这些共享资源。 fixture装饰器可以应用在函数、类或者模块级别上,它的作用是将被装饰的函数或者方法转变为一个fixture对象。fixture对象可以在测试用例作为参数进行调用,pytest会自动根据参数名匹配相应的fixture对象,并将其传递给测试用例。 fixture装饰器可以接受一些参数来定制其行为,例如scope参数用于指定fixture的作用域,autouse参数用于指定是否自动使用fixture等。 下面是一些常见的fixture用法: 1. 无参数fixture: ```python import pytest @pytest.fixture def setup(): # 初始化操作 yield # 清理操作 def test_example(setup): # 使用setup fixture assert 1 + 1 == 2 ``` 2. 带参数fixture: ```python import pytest @pytest.fixture def setup(request): # 初始化操作 def teardown(): # 清理操作 request.addfinalizer(teardown) def test_example(setup): # 使用setup fixture assert 1 + 1 == 2 ``` 3. fixture作用域: ```python import pytest @pytest.fixture(scope="module") def setup_module(): # 模块级别的初始化操作 yield # 模块级别的清理操作 @pytest.fixture(scope="function") def setup_function(): # 函数级别的初始化操作 yield # 函数级别的清理操作 def test_example(setup_module, setup_function): # 使用setup_module和setup_function fixture assert 1 + 1 == 2 ``` 通过使用fixture装饰器,我们可以更加灵活地管理测试用例的共享资源和测试环境的初始化和清理操作,提高测试用例的可维护性和可重复性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值