测试框架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.
    """
相关推荐
莫负初9 小时前
Pytest 使用Pycharm右键直接运行测试脚本正常,控制台命令pytest运行收集不到用例无法正常测试 no tests ran in 0.01s
ide·pycharm·pytest
欲游山河十万里1 天前
pytest(三)——参数化@pytest.mark.parametrize
pytest
奶茶精Gaaa5 天前
pytest
pytest
霍格沃兹测试开发学社测试人社区7 天前
软件测试学习笔记丨Pytest 学习指南
软件测试·笔记·测试开发·学习·pytest
神即道 道法自然 如来9 天前
测试面试题:pytest断言时,数据是符点类型,如何断言?
pytest
high_tea9 天前
pytest - 多线程提速
python·pytest
傻啦嘿哟11 天前
自动化测试框架集成:将Selenium集成到pytest与unittest中
selenium·测试工具·pytest
一名在八月份找工作的测试员11 天前
自动化学习1:pytest自动化框架的基本用法:注意事项/断言assert/测试结果分析
学习·自动化·pytest
什么时候才能变强12 天前
Pytest-如何将allure报告发布至公司内网
linux·服务器·pytest