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 特点 入门简单,文档丰富 支持单元测试,功能测试 支持参数化,重复执行,部分执行,测试跳过 兼容其他测试 ...
随机推荐
- JMeter软件测试工具介绍及基本安装教程
一.工具介绍 (一)简介 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 它可以用于测试 ...
- SSH 端口转发 - 你不让我看,我也能看
在之前 GRE 的文章中,我们知道隧道技术可以解决异种网络的通信问题.在今天这篇文章中,将认识隧道技术的另一应用 - SSH 端口转发. 首先我们对 SSH 并不陌生,是应该非常普遍的加密协议,用于在 ...
- python设计模式之享元模式
python设计模式之享元模式 由于对象创建的开销,面向对象的系统可能会面临性能问题.性能问题通常在资源受限的嵌入式系统中出现,比如智能手机和平板电脑.大型复杂系统中也可能会出现同样的问题,因为要在其 ...
- 微信小程序携带参数跳转页面/获取页面栈
页面跳转携带参数(以传递两个参数为例) a.wxml 页面传递 1 <navigator url="/pages/b/b?id=1&sid='289'"> &l ...
- NOIP真题索引
NOIP真题索引 NOIP2019 Day 1 格雷码 括号树 树上的数 Day 2 Emiya 家今天的饭 划分 树的重心 NOIP2018 Day 1 铺设道路 货币系统 赛道修建 Day 2 旅 ...
- 记一次mysql数据库被勒索(中)
背景在上一篇文章里面已经提过了. 现在面临的问题是nextcloud没有mysql数据库,用不起来了. 因为文件没丢,一种方法是启动新的mysql数据库,把文件重新提交一次. 为了程序员的面子,没有选 ...
- 数值分析案例:Newton插值预测2019城市(Asian)温度、Crout求解城市等温性的因素系数
数值分析案例:Newton插值预测2019城市(Asian)温度.Crout求解城市等温性的因素系数 文章目录 数值分析案例:Newton插值预测2019城市(Asian)温度.Crout求解城市等温 ...
- java class类和object类
Class类 介绍 Java的Class类是java反射机制的基础,通过Class类我们可以获得关于一个类的相关信息 Java.lang.Class是一个比较特殊的类,它用于封装被装入到JVM中的类( ...
- mac下protobuf配置记录
sudo vi /etc/profile加到文件底部 export PATH=$PATH:$GOBIN:/usr/local/go/bin export GOPATH=/Users/jinfuzhan ...
- 第二篇 Scrum冲刺博客
一.会议图片 二.项目进展 成员 完成情况 今日任务 冯荣新 搜索框,首页轮播图,分类导航 商品列表,商品详情轮播图 陈泽佳 背景展示,选择并显示图片 历史足迹,静态页面 徐伟浩 登录权限获取 商品信 ...