微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pytest06--测试固件

pytest测试固件

测试固件也叫测试夹具,用于指定初始化代码或清理代码/扫尾工作

fixture

fixture修饰器来标记固定的工厂函数,在其他函数,模块,类或整个工程调用它时会被激活并优先执行,通常会被用于完成预置处理和重复操作
使用方法:
# 导入pytest
import pytest
# 定义初始化函数
@pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None)
def 初始化函数():
    初始化代码
常用参数:
 scope:被标记方法的作用域
 params:(list类型)提供参数数据,供调用标记方法函数使用
 autouse:是否自动运行,认为False不运行,设置为True自动运行
# 引用初始化函数
def 测试函数(初始化函数名):

fixture优点

firture相对于setup和teardown来说应该有以下几点优势:
    命名方式灵活,不局限于setup和teardown这几个命名
    conftest.py 配置里可以实现数据共享,不需要import就能自动找到一些配置
    scope=”module” 可以实现多个.py跨文件共享前置
    scope=”session” 以实现多个.py跨文件使用一个session来完成多个用例

通过参数引用

在project_p2 中 test_a.py

import pytest
class Test_ABC:
    @pytest.fixture()
    def before(self):
        print("------->before")

    def test_a(self, before):  # ️ test_a方法传入了被fixture标识的函数,已变量的形式
        print("------->test_a")
        assert 1

if __name__ == '__main__':
    pytest.main("-s  test_abc.py")
"""
执行结果:
    test_a.py 
        ------->before # 发现before会优先于测试函数运行
        ------->test_a
"""

通过函数引用

在project_p2 中 test_b.py

import pytest
@pytest.fixture()  # fixture标记函数可以应用于测试类外部
def before():
    print("------->before")
@pytest.mark.usefixtures("before")
class Test_ABC:
    def setup(self):
        print("------->setup")

    def test_a(self):
        print("------->test_a")
        assert 1


if __name__ == '__main__':
    pytest.main("-s  test_b.py")
"""
执行结果:
test_abc.py
------->before  # 发现before会优先于测试类运行
------->setup
------->test_a
"""

认设置为运行

在project_p2 中 test_c.py

import pytest
@pytest.fixture(autouse=True) # 设置为认运行
def before():
    print("------->before")
class Test_ABC:
    def setup(self):
        print("------->setup")
    def test_a(self):
        print("------->test_a")
        assert 1
if __name__ == '__main__':
    pytest.main("-s  test_c.py")
"""
执行结果:
    test_abc.py 
    ------->before # 发现before自动优先于测试类运行
    ------->setup
    ------->test_a
"""

fixture设置作用域

测试固件可以全部放到pytest的配置文件conftest.py中,也可以将function和class范围的固件放到测试模块中,module和session放在conftest中

@pytest.fixture(scope=?)
    scope="function"
        每个测试函数方法之前都运行,认值,省略scope='function'时,括号可同时省略
    scope="class"
        每个测试函数之前运行一次,每个class之前只运行一次
    scope="module"
        每个module的所有测试方法之前只运行一次
    scope="session"
        每个session只运行一次,多个py文件调用一次,约束或控制多个py文件

fixure修饰一个或多个测试函数方法

在test_demo05.py文件

import pytest
@pytest.fixture(scope='module')
def pre_module():
    print('----------->pre_module每个测试模块前做')
@pytest.fixture(scope='class')
def pre_class():
    print('----------->pre_class每个测试类前做')
@pytest.fixture(scope='function')
def pre_function():
    print('----------->pre_function每个测试函数方法前做')
def test_3(pre_module):  # 第1个测试函数中的pre_module勿省
    print('test3')
class Test_Case:
    def test_1(self, pre_class, pre_function):  # 类中第1个测试方法pre_class勿省
        print('test1')
    def test_2(self, pre_function):  # pre_class可省
        print('test2')
if __name__=='__main__':
    pytest.main('-s test_demo05.py')

scope="session"

    要写到conftest.py文件里
    conftest.py文件名是固定的,pytest会自动识别该文件
    一个项目下可以建多个conftest.py的文件,一般在项目根目录下设置的conftest文件起到全局作用,对同目录或子目录中的*test*.py文件都起作用。在不同子目录下也可以放conftest.py的文件,作用范围只能在该层级以及子目录中起作用
    第一要执行的py文件中第一个要执行的测试函数或测试方法中应指定要使用的fixture名称,也可以所有测试函数方法都指定
    conftest.py与运行的用例要在同一个包下,并且有init.py文件

使用session范围的测试固件

创建project_p2项目并创建conftest文件以及两个测试文件

conftest.py

import pytest
@pytest.fixture(scope='session')
def pre_session():
    print('在所有测试文件前只执行一次的代码pre_session')
@pytest.fixture(scope='module')
def pre_module():
    print('pre_module')

test1.py

def test11(pre_session,pre_module):
    print('测试函数test11')
def test12():
    print('测试函数test12')

test2.py

class Test22:
    def test221(self,pre_session,pre_module):
        print('测试方法test221')
    def test222(self):
        print('测试方法test222')

执行测试

同时执行两个py文件,可以在cmd中在文件py文件所在目录下执行命令
pytest  -s  test1.py  test2.py
文件执行按照书写顺序进行

fixture自动使用

    @pytest.fixture(scope=?, autouse=True)
        当用例很多的时候,每次都传这个参数,会很麻烦
        fixture中的参数autouse,认是False
        autouse设置为True,自动调用fixture功能
        使用scope=class时,每个测试函数都会自动使用其所修饰的初始化函数

yield

yield实现用例执行完之后清除数据(或还原)操作
fixture通过scope参数控制setup级别,既然有setup作为用例之前前的操作
用例执行完之后那肯定也有teardown操作。fixture的teardown操作并不是独立的函数,用yield关键字呼唤teardown操作
yield是一个关键字,它不是单独存在的,要写在fixtrue标记的固件中

test_demo06.py

import pytest


@pytest.fixture(scope="function")
def login():
    print("登录成功")
    yield
    print("用例执行完成,收尾")


def test1(login):
    print('操作1')
    print("-----------------------------------------------")


def test2(login):
    print('操作2')
    print("-----------------------------------------------")


def test3(login):
    print('操作3')
    print("-----------------------------------------------")


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

从结果看出,虽然test1,test2,test3三个地方都调用了login函数,并且它会在每一个用例前执行一次

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐