pytest测试框架 -- setup和teardown等
一、用例运行级别
1、函数级别(setup、teardown 或 setup_function、teardown_function):
仅对处于同作用域的测试函数有效(该函数定义不在类中,则对非类中测试函数有效;若该函数定义在类中,则对类中测试函数有效)
def setup_function(): # setup()也一样
print("setup_function") def teardown_function(): # teardown()也一样
print("teardown_function") def test_01():
print("---用例a执行---") class TestCase():
def test_02(self):
print("---用例b执行---") def test_03(self):
print("---用例c执行---") def test_04():
print("---用例d执行---") 输出结果:
test_fixture2.py setup_function
---用例a执行---
.teardown_function
---用例b执行---
.---用例c执行---
.setup_function
---用例d执行---
.teardown_functio
2、方法级别(setup_method、teardown_method):
该函数只能定义在类中,且对类中的测试函数均有效
def test_01():
print("---用例a执行---") class TestCase():
def setup_method(self):
print("setup_method") def teardown_method(self):
print("teardown_method") def test_02(self):
print("---用例b执行---") def test_03(self):
print("---用例c执行---") 输出结果:
test_fixture2.py ---用例a执行---
.setup_method
---用例b执行---
.teardown_method
setup_method
---用例c执行---
.teardown_method
3、类级别(setup_class、teardown_class):
该函数只能定义在类中,且分别在类开始和结束前各执行一次
def test_01():
print("---用例a执行---") class TestCase():
def setup_class(self):
print("setup_class") def teardown_class(self):
print("teardown_class") def test_02(self):
print("---用例b执行---") def test_03(self):
print("---用例c执行---") 输出结果:
test_fixture2.py ---用例a执行---
.setup_class
---用例b执行---
.---用例c执行---
.teardown_class
4、模块级别(setup_module、teardown_module):
该函数只可定义在非类中,且分别在所有测试函数开始和结束前各执行一次
def setup_module():
print("setup_module") def teardown_module():
print("teardown_module") def test_01():
print("---用例a执行---") class TestCase(): def test_02(self):
print("---用例b执行---") def test_03(self):
print("---用例c执行---") def test_04():
print("---用例d执行---") 输出结果:
setup_module
---用例a执行---
.---用例b执行---
.---用例c执行---
.---用例d执行---
.teardown_modul
5、不同级别函数混合:
运行的优先级:module > function > class > method > setup、teardown
def setup_module():
print("setup_module") def teardown_module():
print("teardown_module") def setup_function():
print("setup_function") def teardown_function():
print("teardown_function") def test_01():
print("---用例a执行---") class TestCase(): def setup(self):
print("setup") def teardown(self):
print("teardown") def setup_method(self):
print("setup_method") def teardown_method(self):
print("teardown_method") def setup_class(self):
print("setup_class") def teardown_class(self):
print("teardown_class") def test_02(self):
print("---用例b执行---") def test_03(self):
print("---用例c执行---") 输出结果:
test_fixture2.py setup_module
setup_function
---用例a执行---
.teardown_function
setup_class
setup_method
setup
---用例b执行---
.teardown
teardown_method
setup_method
setup
---用例c执行---
.teardown
teardown_method
teardown_class
teardown_module
参考:https://www.cnblogs.com/yoyoketang/p/9374957.html
pytest测试框架 -- setup和teardown等的更多相关文章
- Pytest测试框架(二):pytest 的setup/teardown方法
PyTest支持xUnit style 结构, setup() 和 teardown() 方法用于初始化和清理测试环境,可以保证测试用例的独立性.pytest的setup/teardown方法包括:模 ...
- Pytest测试框架(三):pytest fixture 用法
xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...
- 『德不孤』Pytest框架 — 1、Pytest测试框架介绍
目录 1.什么是单元测试框架 2.单元测试框架主要做什么 3.单元测试框架和自动化测试框架有什么关系 4.Pytest测试框架说明 5.Pytest框架和Unittest框架区别 (1)Unittes ...
- pytest测试框架 -- 简介
一.pytest测试框架简介: (1)pytest是python的第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效. (2)pytest框架可以兼容unittest用 ...
- Pytest测试框架(一):pytest安装及用例执行
PyTest是基于Python的开源测试框架,语法简单易用,有大量的插件,功能非常多.自动检测测试用例,支持参数化,跳过特定用例,失败重试等功能. 安装 pip install -U pytest ...
- Pytest测试框架(五):pytest + allure生成测试报告
Allure 是一款轻量级.支持多语言的开源自动化测试报告生成框架,由Java语言开发,可以集成到 Jenkins. pytest 测试框架支持Allure 报告生成. pytest也可以生成juni ...
- python pytest测试框架介绍三
之前介绍了pytest以xUnit形式来写用例,下面来介绍pytest特有的方式来写用例 1.pytest fixture实例1 代码如下 from __future__ import print_f ...
- python pytest测试框架介绍二
在介绍一中简单介绍了pytest的安装和简单使用,接下来我们就要实际了解pytest了 一.pytest的用例发现规则 pytest可以在不同的函数.包中发现用例,发现的规则如下 文件名以test_开 ...
- Pytest 测试框架
一 . Pytest 简介 Pytest是python的一种单元测试框架. 1. pytest 特点 入门简单,文档丰富 支持单元测试,功能测试 支持参数化,重复执行,部分执行,测试跳过 兼容其他测试 ...
随机推荐
- wordpress-技术博客主题推荐
推荐主题 1.WordStar 这个主题是干净的,以博客为中心,设计清晰,简单,直接的排版,可在各种各样的屏幕尺寸可读,适合多种语言. 效果图 还是非常简洁, 基本和CSDN差不多了 除了没有广告以外 ...
- github渗透测试工具库[转载]
前言 今天看到一个博客里有这个置顶的工具清单,但是发现这些都是很早以前就有文章发出来的,我爬下来后一直放在txt里吃土.这里一起放出来. 漏洞练习平台 WebGoat漏洞练习平台:https://gi ...
- 为创建Golang GUI程序选择合适的库
我认为在Go语言中创建GUI只有两种相对较好的方式,一是Qt,二则是Electron. 如何选择? 这要看你的需求.如果你会HTML+CSS+JavaScript,只想使用Go开发对性能没有多高的程序 ...
- c++知识点 2006-10-14 12:59
这是自己开发一个简易的监控系统时的所用到的知识点. 第一个问题 在网络传输数据时可以传int,float,doule,char,等包括结构体类型但是除类类型外. 在调试中要常用merry寄存器. 应用 ...
- SweetAlert 弹框之后点击OK执行方法
swal( '审核通过!', '', 'success' ).then(function () { Return(); })
- 本blog的地图
欢迎 CTRL+F收索 / CTRL+D 持续更新 C++: C++快速排序 C++归并排序 高精度 CSS: CSS实现ps基础操作 PYTHON: python爬虫教程,一篇就够了 其他推荐 ...
- Robot Framework(2)——简单运行案例
1.打开RIDE 之前介绍的3种方式都可以 2.创建工程和测试套件 1>点击File-New Project ①Name:工程命名 ②Parent Directory:上级目录,工程会创建在这个 ...
- 在服务器上使用python-gym出现的显示问题
参考链接: http://www.luyixian.cn/news_show_392045.aspx https://www.cnblogs.com/cenariusxz/p/12666938.htm ...
- Spring编程式注解使用不当导致其他事务无法正常提交
1.事故背景 原本在使用的是注解式事务,后面因为需要在事务中增加异步推送机制,所以需要将推送机制放到事务之外,修改后发现系统经常出现事务长时间无法提交导致回滚. 2.排查流程 (1)一开始重启应用是能 ...
- Maven通解
参考博文:通俗理解maven 该篇文章篇幅很长,大概的思路如下 maven的介绍,初步认识,获取jar包的三个关键属性 --> 介绍仓库(获取的jar包从何而来)-->用命令行管理ma ...