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 特点 入门简单,文档丰富 支持单元测试,功能测试 支持参数化,重复执行,部分执行,测试跳过 兼容其他测试 ...
随机推荐
- 9个常用ES6特性归纳(一般用这些就够了)
ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在 2015 年 6 月正式发布了.它的目标,是使得 JavaScript 语言可以用来编写复杂的大型应 ...
- python处理转载博客html
前景 在转载别人博客的时候通常我们会通过复制html然后放到编辑器里面, 但是通常html里有很多杂七杂八的东西, 比如script, svg这些标签导致排版出现问题 例如由lu标签引起的 由svg标 ...
- unity3d学习笔记(一) 第一人称视角实现和倒计时实现
unity3d学习笔记(一) 第一人称视角实现和倒计时实现 1. 第一人称视角 (1)让mainCamera和player(视角对象)同步在一起 因为我们的player是生成的,所以不能把mainCa ...
- C#图解教程(第四版)—01—类型,存储,变量
3.1 如何广泛的描述C#程序 可以说C程序是一组函数和数据类型,C++程序是一组函数和类,然而C#程序是一组类型声明 3.2 类型 可以把类型想象成一个用来创建数据结构的模板,模板本身并不是数据结构 ...
- myBatis源码解析-反射篇(4)
前沿 前文分析了mybatis的日志包,缓存包,数据源包.源码实在有点难顶,在分析反射包时,花费了较多时间.废话不多说,开始源码之路. 反射包feflection在mybatis路径如下: 源码解析 ...
- c# Attribute会不会影响性能
Attribute很方便,标记一个类,设置这个类的额外信息,而不用另外设计存储这个信息. 那么频繁大量使用Attribute会不会影响类的性能. 为此,简单测试. 代码: 略............. ...
- 【算法•日更•第十二期】信息奥赛一本通1585:【例 1】Amount of Degrees题解
废话不多说,直接上题: 1585: [例 1]Amount of Degrees 时间限制: 1000 ms 内存限制: 524288 KB提交数: 130 通过数: 68 [ ...
- CAP 3.1 版本发布通告
前言 今天,我们很高兴宣布 CAP 发布 3.1 版本正式版.同时我们也很高兴的告诉你 CAP 在 GitHub 已经突破了 4000 Star. CAP 3000 Star 还是去年8月份的时候,最 ...
- Azure Command Line (一)入门
一,引言 今天我们讲解一个新的 Azure 的知识,叫 “Azure Command Line”,简称 Azure CLI,具体概念是什么,我这里也不多说了,总结下来,Azure CLI 其实就是 用 ...
- 每天都在用String,你真的了解吗?
1.String概述 java.lang.String 类代表字符串.Java程序中所有的字符串文字(例如"abc")都可以被看作是实现此类的实例 String 中包括用于检查各个 ...