Pytest学习笔记2-setup和teardown
前言
我们在做自动化的时候,常常有这样的需求:
执行每一条用例时,都重新启动一次浏览器
每一条用例执行结束时,都清除测试数据
在unittest中,我们可以使用 setUp() 和 tearDown() 两个方法来实现以上需求,其中 setUp() 方法用于初始化测试固件;而 tearDown() 方法用于销毁测试固件。程序会在运行每个测试用例(以 test_ 开头的方法)之前自动执行 setUp() 方法来初始化测试固件,井在每个测试用例(以 test_ 开头的方法)运行完成之后自动执行 tearDown() 方法来销毁测试固件。
那么如何实现只启动一次浏览器,所有测试用例执行结束后再清除数据这样的需求呢?
- unittest提供了setUpClass()和tearDownClass()两个方法,配合@classmethod装饰器使用即可
作为比unittest更强大的框架,pytest自然也有类似的方法
pytest的setup/teardown方法包括:
- 模块级别(setup_module/teardown_module)
- 函数级别(setup_function/teardown_function)
- 类级别(setup_class/ teardown_class)
- 方法级别(setup_method/teardown_methond或者setup/teardown)
模块级别
模块中的第一个用例开始前执行一次setup_module方法,模块中的最后一个测试用例结束后执行一次teardown_module方法
import pytest
def setup_module():
print("执行setup_module")
def teardown_module():
print("执行teardown_module")
class TestDemo(object):
def test_case1(self):
print("执行测试用例1")
assert 1 + 1 == 2
def test_case2(self):
print("执行测试用例2")
assert 1 + 3 == 4
def test_case3(self):
print("执行测试用例3")
assert 1 + 5 == 6
运行结果如下:
函数级别
在每个测试函数前运行一次setup_function方法,在每个测试函数结束后运行一次teardown_function方法,只对函数用例生效,不在类中。
import pytest
def setup_function():
print("执行setup_function")
def teardown_function():
print("执行teardown_function")
def test_case1():
print("执行测试用例1")
assert 1 + 1 == 2
def test_case2():
print("执行测试用例2")
assert 1 + 3 == 4
def test_case3():
print("执行测试用例3")
assert 1 + 5 == 6
运行结果如下:
类级别
setup_class/teardown_class 对类有效,位于类中,在执行测试类之前和之后各调用一次
import pytest
class TestDemo(object):
def setup_class(self):
print("执行setup_class")
def teardown_class(self):
print("执行teardown_class")
def test_case1(self):
print("执行测试用例1")
assert 1 + 1 == 2
def test_case2(self):
print("执行测试用例2")
assert 1 + 3 == 4
def test_case3(self):
print("执行测试用例3")
assert 1 + 5 == 6
运行结果如下:
方法级别
setup_method/teardown_method和setup/teardown,在测试类中每个测试方法前后调用一次。这两个方法效果是一样的
import pytest
class TestDemo(object):
def setup_method(self):
print("执行setup_method")
def teardown_method(self):
print("执行teardown_method")
def test_case1(self):
print("执行测试用例1")
assert 1 + 1 == 2
def test_case2(self):
print("执行测试用例2")
assert 1 + 3 == 4
def test_case3(self):
print("执行测试用3")
assert 1 + 5 == 6
运行结果如下:
四种级别混合使用
如果把这四种级别的方法混合使用,运行顺序如何呢?
import pytest
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_a():
print("执行测试函数a")
def test_b():
print("执行测试函数b")
class TestDemo(object):
def setup_class(self):
print("测试类开始时,执行setup_class")
def teardown_class(self):
print("测试类结束时,执行teardown_class")
def setup_method(self):
print("类中的方法开始时,执行setup_method")
def teardown_method(self):
print("类中的方法结束时,执行teardown_method")
def test_case1(self):
print("执行测试用例1")
assert 1 + 1 == 2
def test_case2(self):
print("执行测试用例2")
assert 1 + 3 == 4
def test_case3(self):
print("执行测试用例3")
assert 1 + 5 == 6
运行结果如下:
总结
- 模块级(setup_module/teardown_module)开始于模块始末,全局的
- 函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
- 类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
- 方法级(setup_method/teardown_method或setup/teardown)开始于方法始末(在类中)
Pytest学习笔记2-setup和teardown的更多相关文章
- [转载]pytest学习笔记
pytest学习笔记(三) 接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, ...
- pytest学习笔记(pytest框架结构)
一.pytest框架中使用setup.teardown.更灵活按照用例级别可以分为以下几类: 1.模块级:(setup_module.teardown_module)在模块始末调用 2.函数级:(se ...
- pytest学习笔记
From: https://blog.csdn.net/gaowg11/article/details/54910974 由于对测试框架了解比较少,所以最近看了下pytest测试框架,对学习心得做个记 ...
- Pytest学习笔记3-fixture
前言 个人认为,fixture是pytest最精髓的地方,也是学习pytest必会的知识点. fixture用途 用于执行测试前后的初始化操作,比如打开浏览器.准备测试数据.清除之前的测试数据等等 用 ...
- pytest 学习笔记一:参数化与组织分层
组织分层: 1.普通方式,和unittest分层类似: setup_module() # 通常放在类外 setup_class(cls) setup(self) teardown(self) tea ...
- pytest学习笔记二 fixtrue
前言 官方文档关于fixture功能的解释如下: The purpose of test fixtures is to provide a fixed baseline upon which test ...
- 『德不孤』Pytest框架 — 10、setUp()和tearDown()函数
目录 1.setUp()和tearDown()函数介绍 2.setUp()和tearDown()函数作用 3.setUp()和tearDown()函数说明 4.示例 (1)方法级 (2)类级 (3)函 ...
- pytest学习笔记(二)
继续文档的第二章 (一)pytest中可以在命令行中静态/动态添加option,这里没什么好讲的,略过... 这里面主要讲下如何试用skip/xfail,还有incremental(包含一些列的测试步 ...
- pytest 学习笔记一 入门篇
前言 之前做自动化测试的时候,用的测试框架为Python自带的unittest框架,随着工作的深入,发现了另外一个框架就是pytest (官方地址文档http://www.pytest.org/en/ ...
随机推荐
- controller通过map返回减少dto类的创建
更多精彩关注公众号 不要把实体类对象直接返给前端 ,首先想到的是创建DTO,但是这样就造成大量的DTO,显得很臃肿,为了减少dto的数量,像一些比较少的参数避免创建不必要的DTO,通过本次优化达到业务 ...
- git的一些常用命令总结
1.拉取代码Git clone "链接名称" 2.新建分支 git checkout -b "分支名称" 3.提交代码步骤 (1)Git status查看项目 ...
- 通过Dapr实现一个简单的基于.net的微服务电商系统(十)——一步一步教你如何撸Dapr之绑定
如果说Actor是dapr有状态服务的内部体现的话,那绑定应该是dapr对serverless这部分的体现了.我们可以通过绑定极大的扩展应用的能力,甚至未来会成为serverless的基础.最开始接触 ...
- 消息队列RabbitMQ(三):消息确认机制
引言 RabbitMQ的模型是生产者发送信息到 Broker (代理),消费者从 Broker 中取出信息.但是生产者怎么知道消息是否真的发送到 Broker 中了呢?Broker 又怎么知道消息到底 ...
- 80行代码教你写一个Webpack插件并发布到npm
1. 前言 最近在学习 Webpack 相关的原理,以前只知道 Webpack 的配置方法,但并不知道其内部流程,经过一轮的学习,感觉获益良多,为了巩固学习的内容,我决定尝试自己动手写一个插件. 这个 ...
- Java集合详解(三):LinkedList原理解析
概述 本文是基于jdk8_271源码进行分析的. LinkedList底层是基于链表实现.链表没有长度限制,内存地址不需要固定长度,也不需要是连续的地址来进行存储,只需要通过引用来关联前后元素即可完成 ...
- 前端Node的实用方法
Node 一.什么是Node Node是以基于Chrome V8引擎的JavaScript运行环境,使用了一个事件驱动.非阻塞式I/O模型(I/O是 input/output的缩写,即输入输出端口,在 ...
- Linux 系统运行着许多子系统和应用程序。您可以使用系统日志记录从启动时就收集有关运行中系统的数据。有时
概述 在本教程中,您将学习以下内容: 配置 syslog 守护程序 了解标准设施.优先级和操作 配置日志轮换 了解 rsyslog 和 syslog-ng 系统内部发生了什么 Linux 系统运行着许 ...
- centos国内镜像下载
国内镜像下载 http://mirrors.aliyun.com/centos/6/isos/x86_64/ 如果需要下载centos 7 版本进入对应7的/isos/x86_64/ 选择minima ...
- Linux_配置匿名访问FTP服务
[RHEL8]-FTPserver:[Centos7]-FTPclient !!!测试环境我们首关闭防火墙和selinux(FTPserver和FTPclient都需要) [root@localhos ...