首页 > 其他分享 >pytest中的setup和teardown函数

pytest中的setup和teardown函数

时间:2022-10-22 11:00:16浏览次数:83  
标签:... teardown setup print pytest test def

1、setup和teardown函数的作用

setup函数:主要用来做初始化作用,比如数据库连接、前置参数的赋值等

teardown函数:测试后的清除作用,比如参数的还原或销毁,关闭数据库连接等

2、分类

2.1、不定义在测试类中:

模块级:指的是一个py文件

​​​setup_module()/teardown_module()​​:开始于模块始末,全局的

def setup_module():
print('setup_module...')


def test_01():
print('test_01...')


def test_02():
print('test_02...')


def teardown_module():
print('teardown_module...')

pytest中的setup和teardown函数_pytest

函数级:类外定义的方法叫函数

​​​​​setup_function()/teardown_function()​​​​:只对函数用例生效(不在类中)。

def setup_function():
print('setup_function...')


def test_01():
print('test_01...')


def test_02():
print('test_02...')


class Test(object):
def test_03(self):
print('test_03...')


def teardown_function():
print('teardown_function...')

pytest中的setup和teardown函数_pytest_02

2.2、定义在测试类中

类级:指的是一个class类

​​​setup_class()/teardown_class()​​:只在类中前后运行一次(在类中)。

def test_01():
print('test_01...')


def test_02():
print('test_02...')


class Test(object):
@staticmethod
def setup_class():
print('setup_class...')

def test_03(self):
print('test_03...')

def test_04(self):
print('test_04...')

@staticmethod
def teardown_class():
print('teardown_class...')

pytest中的setup和teardown函数_pytest_03

方法级:类中定义的方法叫方法

​​​setup_method()/teardown_method()​​:开始于方法始末(在类中)。

def test_01():
print('test_01...')


def test_02():
print('test_02...')


class Test(object):
@staticmethod
def setup_method():
print('setup_method...')

def test_03(self):
print('test_03...')

def test_04(self):
print('test_04...')

@staticmethod
def teardown_method():
print('teardown_method...')

pytest中的setup和teardown函数_pytest_04

2.3、既可以写在类中,也可以写在类外

自由的:​​setup()/teardown()​​:

2.3.1、写在类外,与模块级别类似

def setup():
print('setup...')


def test_01():
print('test_01...')


def test_02():
print('test_02...')


class Test(object):

def test_03(self):
print('test_03...')

def test_04(self):
print('test_04...')


def teardown():
print('teardown...')

pytest中的setup和teardown函数_pytest_05

2.3.2、写在类中:与方法级别类似

def test_01():
print('test_01...')


def test_02():
print('test_02...')


class Test(object):

@staticmethod
def setup():
print('setup...')

def test_03(self):
print('test_03...')

def test_04(self):
print('test_04...')

@staticmethod
def teardown():
print('teardown...')

pytest中的setup和teardown函数_pytest_06


标签:...,teardown,setup,print,pytest,test,def
From: https://blog.51cto.com/u_15694134/5785725

相关文章

  • pytest的用例定义规范
    pytest编写测试用例的话,默认的用例规范是:首先,模块名需要以test_开头;有一次直接用testdemo定义用例模块名,pytest运行时没有发现,说明需要时test_开头1、用例文件:test_开头......
  • pytest中文文档
     在网上找到的感觉还不错的pytest的中文文档,这里收藏一下:   翻译的中文文档:完整的Pytest文档 中文文档链接地址:​​https://www.osgeo.cn/pytest/contents.html#full-......
  • Python全功能测试框架pytest
    目录PyTest一、快速开始1、介绍2、安装3、第一个测试4、断言引发异常5、分组测试6、添加临时目录二、调用测试1、布局规则1.1规则1.2测试布局1.2.1测试代码分......
  • pytest
    关于pytestpytest单元测试框架:插件丰富简单易上手可以搭配其他的第三方工具生成漂亮的测试报告参数化https://www.cnblogs.com/Neeo/articles/11832655......
  • 数据驱动【pytest结合Yaml】
    1.pytest结合yamlyaml是一个可读性高,用来表达数据序列化的格式。pyyaml模块在python中用于处理yaml格式数据,主要使用yaml.safe.dump()和yaml.safe.load函数将python值和ya......
  • Pytest进阶使用
    fixture特点:命令灵活:对于setup,teardown可以省略数据共享:在conftest.py配置里写方法可以实现数据共享,不需要import导入,可以跨文件共享scope的层次及神奇的yield组......
  • pytest fixtures[控制用例的执行顺序2]
    pytest可以使用@pytest.fixture装饰器来装饰一个方法,被装饰的方法名可以作为一个参数传入到测试方法中。可以使用这种方法来完成测试之前的初始化,也可以返回数据给测试函数......
  • Pytest插件pytest-order指定用例顺序
    Pytest插件pytest-order指定用例顺序安装 pipinstallpytest-order注意不是pytest-ordering说起来这里有个故事 关于pytest-ordering和pytest-orderhttps......
  • 自动化测试 - mac安装jenkins并安装allure插件,构建执行pytest自动化用例并产出allure
    背景:jenkins+pytest+allure,产出自动化测试结果步骤如下:一、mac安装jenkins1.方法一:(不推荐,跳过)安装tomcat下载jenkins并放到tomcat的www目录2.方法二:brew安装......
  • 【pytest官方文档】解读- 插件开发之hooks 函数(钩子)
    上一节讲到如何安装和使用第三方插件,用法很简单。接下来解读下如何自己开发pytest插件。但是,由于一个插件包含一个或多个钩子函数开发而来,所以在具体开发插件之前还需要先......