Pytest学习(三) - setup和teardown的使用
一、前言
从文章标题可以看出,就是初始化和释放的操作,根据我的java习惯来学习pytest,个人感觉没差太多,理解上也不是很难。
哦,对了,差点跑题了,这个框架是基于Python语言的,在学习的时候难免总会用java的类比思想来学习,下面言归正传哈。
我们还从 unittest与pytest来对比学习吧
二、unittest用法
unittest有两个前置方法,两个后置方法,分别是:
- setup()
- setupClass()
- teardown()
- teardownClass()
个人始终觉得unittest和Junit像极了。
三、pytest用法
当然,Pytest也提供了类似setup、teardown的方法,分别是:
- 模块级别:setup_module、teardown_module
- 函数级别:setup_function、teardown_function,不在类中的方法
- 类级别:setup_class、teardown_class
- 方法级别:setup_method、teardown_method
- 方法细化级别:setup、teardown
我总感觉学习pytest像是在学习testng一样,难道是我的错觉吗,啊啊啊啊,不能吧。
四、unittest示例
unittest的setupClass和teardownClass,需要配合@classmethod装饰器一起使用,也就是我们java说的注解呀,这块是翻译给java学Python的同学的,可忽略哈。
示例代码如下:
# -*- coding: utf-8 -*-
# @Time : 2020/10/21 20:09
# @Author : longrong.lang
# @FileName: test_setup_teardown_unittest.py
# @Software: PyCharm
# @Cnblogs :https://www.cnblogs.com/longronglang
'''
unittest代码示例
'''
import unittest
class TestUnitTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("所有用例执行前执行")
def setUp(self):
print("每个用例开始前执行")
def tearDown(self):
print("每个用例结束后执行")
@classmethod
def tearDownClass(cls):
print("所有用例执行后执行")
def testA(self):
'''用例A'''
print("用例A执行了")
self.assertEquals(1, 1)
def testB(self):
'''用例B'''
print("用例B执行了")
self.assertTrue(True)
if __name__ == "__main__":
unittest.main()
执行结果
可以看出执行顺序为:
setUpClass
setUp
testA
tearDown
setUp
testB
tearDown
tearDownClass
用例之间按用例名称ASCII码的顺序加载,数字与字母顺序为0~9,A~Z,a~z, 所以testA会在testB之前运行。
五、pytest示例
函数级的setup_function、teardown_function只对函数用例生效,而且不在类中使用
依旧还是把类和函数都有的情况放在一起,示例代码如下:
# -*- coding: utf-8 -*-
# @Time : 2020/10/21 20:27
# @Author : longrong.lang
# @FileName: test_setup_teardown_pytest.py
# @Software: PyCharm
# @Cnblogs :https://www.cnblogs.com/longronglang
'''
pyetest示例
'''
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_1():
print("正在执行用例1")
x = "this"
assert 'h' in x
def test_2():
print("正在执行用例2")
assert 1 == 1
class TestClass(object):
def setup_class(self):
print("setup_class(self):每个类之前执行一次,只执行一次")
def teardown_class(self):
print("teardown_class(self):每个类之后执行一次,只执行一次")
def test_A(self):
print("正在执行用例A")
x = "this"
assert 'h' in x
def test_B(self):
print("正在执行B")
assert 1 == 1
if __name__ == "__main__":
pytest.main(["-q", "test_setup_teardown_pytest.py"])
执行结果
可以看出来,互不影响,执行顺序为:
setup_module()
setup_function()
test_1
teardown_function()
setup_function()
test_2
teardown_function()
setup_class(self)
test_A
test_B
teardown_class(self)
teardown_module
main方法中的-q,为pytest打印测试用例的执行结果级别。
如不清楚,请移步到《Pytest学习(一)- 入门及基础》。
Pytest学习(三) - setup和teardown的使用的更多相关文章
- pytest 2.测试用例setup和teardown
之前我写的unittest的setup和teardown,还有setupClass和teardownClass(需要配合@classmethod装饰器一起使用),接下来就介绍pytest的类似于这类的 ...
- python:pytest中的setup和teardown
原文:https://www.cnblogs.com/peiminer/p/9376352.html 之前我写的unittest的setup和teardown,还有setupClass和teardow ...
- Pytest学习笔记2-setup和teardown
前言 我们在做自动化的时候,常常有这样的需求: 执行每一条用例时,都重新启动一次浏览器 每一条用例执行结束时,都清除测试数据 在unittest中,我们可以使用 setUp() 和 tearDown( ...
- pytest测试框架 -- setup和teardown等
一.用例运行级别 1.函数级别(setup.teardown 或 setup_function.teardown_function): 仅对处于同作用域的测试函数有效(该函数定义不在类中,则对非类中测 ...
- pytest二:setup和teardown
用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(setup_function/teardown_function)只对函数用例生效(不在 ...
- pytest用例setup和teardown
函数式以下两种: setup_function/teardown_function 每个用例开始和结束调用一次 setup_module/teardown_module setup_modu ...
- pytest的setup和teardown
学过unittest的setup和teardown,前置和后置执行功能.pytest也有此功能并且功能更强大,今天就来学习一下吧. 用例运行级别: 模块级(setup_module/teardown_ ...
- 【pytest】(十二)参数化测试用例中的setup和teardown要怎么写?
还是一篇关于pytest的fixture在实际使用场景的分享. fixture我用来最多的就是写setup跟teardown了,那么现在有一个用例是测试一个列表接口,参数化了不同的状态值传参,来进行测 ...
- pytest自动化2:测试用例setup和teardown
前言: pytest支持函数和类两种用例方式,针对每种情况都有不同的代码 pytest用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(s ...
随机推荐
- python变量及简单数据类型
python 目录 python 1.变量 1.变量的定义 2.变量的命名 3. 关键字 4.变量的命名规则 5.变量的类型 5.不同类型变量之间的计算 6.变量的输入 7.变量的格式化输出 8.格式 ...
- .Net EF 学习之model first
新建一个控制台项目,然后点击添加新建项,选择ADO.Net 实体数据模型 选择空模型 右击设计器,新增,实体 右击新增,标量属性, 右侧可以设置最大长度和一些属性信息: 建好对象后右击根据模型生成数据 ...
- Django ContentType(ORM操作)
ContentType-设计课程表 # 数据: """ 免费课:Python入门 学位课:Python全栈 价格策略: Linux入门 7 0 Python入门 7 0 ...
- 我搭建了一套企业级私有Git服务,抗住了每天上万次攻击!
写在前面 事情是这样的,今年疫情期间,我在某云购买了一套服务器,做什么呢?不是用来部署项目,也不是用来搭建网站,而是用来做代码备份和管理.没错,都是我个人的代码,也许你会说,你个人能有多少代码啊?确实 ...
- eclipse validating 卡着一直不动
处理方式: 1.对项目的.project文件去掉下面两个配置 org.eclipse.wst.jsdt.core.javascriptValidator 和 org.eclipse.wst.jsdt. ...
- py004.python的逻辑运算,随机数及判断语句if,elif,else
判断语句又称 "分支语句" if判断语句的格式: if 条件1: 条件1满足时,执行的代码 -- # 前面有缩进4个空格 elif 条件2: 条件2满足时,执行的代码 -- # 前 ...
- 模型集成model ensemble
A prediction model that is composed of a set of models is called a model ensemble. Baggging 和Boostin ...
- Pipelines
https://blog.csdn.net/buracag_mc/article/details/100155599 ML Pipelines提供了一组基于DataFrame构建的统一的高级API,可 ...
- C 面向对象编程 --- 一模块的串口协议解析
// 任务目的// 解析串口收到的54个字节.这54个字节包含了8个车道的5大信息以及校验信息.// 实现了查询每条车道包含了哪些信息. #include <stdio.h>#includ ...
- 为cmd中的命令添加别名,以解决java:错误: 编码 GBK 的不可映射字符 (0xAF)
使用sublineText3编写了java代码,通过cmd javac编译 提示 错误:编码GBK的不可映射字符 解决方法 使用javac -encoding UTF-8 Person.java 结果 ...