测试框架pytest教程(7)实现 xunit 风格的setup

pytest支持setup和teardown,对于使用unittest和nose框架的用户来说对这些很熟悉,但是在pytest可以使用功能更强大的fixture来实现固定装置。

模块级别

如果单个模块中有多个测试函数和测试类,您可以选择实现以下固定方法,这些方法通常会为所有函数调用一次:

python 复制代码
def setup_module(module):
    """setup any state specific to the execution of the given module."""


def teardown_module(module):
    """teardown any state that was previously setup with a setup_module
    method.
    """
复制代码

类级别

在调用类的所有测试方法之前和之后,在类级别调用以下方法

python 复制代码
@classmethod
def setup_class(cls):
    """setup any state specific to the execution of the given class (which
    usually contains tests).
    """


@classmethod
def teardown_class(cls):
    """teardown any state that was previously setup with a call to
    setup_class.
    """

方法级别

在每个方法调用周围都会调用以下方法

python 复制代码
def setup_method(self, method):
    """setup any state tied to the execution of the given method in a
    class.  setup_method is invoked for every test method of a class.
    """


def teardown_method(self, method):
    """teardown any state that was previously setup with a setup_method
    call.
    """

直接在模块中定义

python 复制代码
def setup_function(function):
    """setup any state tied to the execution of the given function.
    Invoked for every test function in the module.
    """


def teardown_function(function):
    """teardown any state that was previously setup with a setup_function
    call.
    """
相关推荐
给你一页白纸4 小时前
Pytest 测试用例自动生成:接口自动化进阶实践
python·pytest·接口自动化
工会主席-阿冰7 小时前
使用pytest-selenium插件,ui自动化示例
selenium·pytest
工会主席-阿冰8 小时前
pytest,ui自动化示例
pytest·ui自动化
我一定会有钱2 天前
pytest基础
python·测试工具·测试用例·pytest
西游音月2 天前
(6)pytest+Selenium自动化测试-测试用例编写
selenium·测试用例·pytest
兴趣使然黄小黄4 天前
【Pytest】Pytest框架快速入门
python·pytest
m0_632482505 天前
Jenkins + Pytest +allure接口自动化测试配置与操作
jenkins·集成测试·pytest·jenkins配置
李星星BruceL5 天前
Pytest第三章(参考指南1)
python·自动化·pytest
哎呀呦呵5 天前
pytest基本使用
python·pytest
Kristen_YXQDN5 天前
PyCharm 中 pytest 运行 python 测试文件报错:D:\Python_file\.venv\Scripts\python.exe: No module named pytest
运维·开发语言·python·pycharm·pytest