pytest文档4-测试用例setup和teardown
前言
学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次。
当然还有更高级一点的setupClass和teardownClass,需配合@classmethod装饰器一起使用,在做selenium自动化的时候,它的效率尤为突出,可以只启动一次浏览器执行多个用例。
pytest框架也有类似于setup和teardown的语法,并且还不止这四个
用例运行级别
模块级(setup_module/teardown_module)开始于模块始末,全局的
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
方法级(setup_method/teardown_method)开始于方法始末(在类中)
类里面的(setup/teardown)运行在调用方法的前后
函数式
setup_function/teardown_function
1.pytest框架支持函数和类两种用例方式,先看函数里面的前置与后置用法:
setup_function/teardown_function 每个用例开始和结束调用一次
# test_fixt.py
# coding:utf-8
import pytest
# 函数式
** 作者:上海-悠悠 QQ交流群:588402570**
def setup_function():
print("setup_function:每个用例开始前都会执行")
def teardown_function():
print("teardown_function:每个用例结束后都会执行")
def test_one():
print("正在执行----test_one")
x = "this"
assert 'h' in x
def test_two():
print("正在执行----test_two")
x = "hello"
assert hasattr(x, 'check')
def test_three():
print("正在执行----test_three")
a = "hello"
b = "hello world"
assert a in b
if __name__ == "__main__":
pytest.main(["-s", "test_fixt.py"])
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO, inifile:
collected 3 items
test_fixt.py setup_function:每个用例开始前都会执行
正在执行----test_one
.teardown_function:每个用例结束后都会执行
setup_function:每个用例开始前都会执行
正在执行----test_two
Fteardown_function:每个用例结束后都会执行
setup_function:每个用例开始前都会执行
正在执行----test_three
.teardown_function:每个用例结束后都会执行
================================== FAILURES ===================================
__________________________________ test_two ___________________________________
def test_two():
print("正在执行----test_two")
x = "hello"
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_fixt.py:19: AssertionError
===================== 1 failed, 2 passed in 0.03 seconds ======================
2.从结果可以看出用例执行顺序:setup_function》用例1》teardown_function, setup_function》用例2》teardown_function, setup_function》用例3》teardown_function
备注:-s参数是为了显示用例的打印信息。 -q参数只显示结果,不显示过程
setup_module/teardown_module
1.setup_module是所有用例开始前只执行一次,teardown_module是所有用例结束后只执行一次
# test_fixt.py
# coding:utf-8
import pytest
# 函数式
** 作者:上海-悠悠 QQ交流群:588402570**
def setup_module():
print("setup_module:整个.py模块只执行一次")
print("比如:所有用例开始前只打开一次浏览器")
def teardown_module():
print("teardown_module:整个.py模块只执行一次")
print("比如:所有用例结束只最后关闭浏览器")
def setup_function():
print("setup_function:每个用例开始前都会执行")
def teardown_function():
print("teardown_function:每个用例结束前都会执行")
def test_one():
print("正在执行----test_one")
x = "this"
assert 'h' in x
def test_two():
print("正在执行----test_two")
x = "hello"
assert hasattr(x, 'check')
def test_three():
print("正在执行----test_three")
a = "hello"
b = "hello world"
assert a in b
if __name__ == "__main__":
pytest.main(["-s", "test_fixt.py"])
2.从运行结果可以看到setup_module和teardown_module只执行了一次
test_fixt.py setup_module:整个.py模块只执行一次
比如:所有用例开始前只打开一次浏览器
setup_function:每个用例开始前都会执行
正在执行----test_one
.teardown_function:每个用例结束前都会执行
setup_function:每个用例开始前都会执行
正在执行----test_two
Fteardown_function:每个用例结束前都会执行
setup_function:每个用例开始前都会执行
正在执行----test_three
.teardown_function:每个用例结束前都会执行
teardown_module:整个.py模块只执行一次
比如:所有用例结束只最好关闭浏览器
备注:setup_function/teardown_function和setup_module/teardown_module这四种方法是可以任意组合的,用一个和多个都可以
类和方法
1.setup/teardown和unittest里面的setup/teardown是一样的功能,setup_class和teardown_class等价于unittest里面的setupClass和teardownClass
#test_fixtclass.py
# coding:utf-8
import pytest
# 类和方法
** 作者:上海-悠悠 QQ交流群:588402570**
class TestCase():
def setup(self):
print("setup: 每个用例开始前执行")
def teardown(self):
print("teardown: 每个用例结束后执行")
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_one(self):
print("正在执行----test_one")
x = "this"
assert 'h' in x
def test_two(self):
print("正在执行----test_two")
x = "hello"
assert hasattr(x, 'check')
def test_three(self):
print("正在执行----test_three")
a = "hello"
b = "hello world"
assert a in b
if __name__ == "__main__":
pytest.main(["-s", "test_fixtclass.py"])
运行结果
test_fixtclass.py setup_class:所有用例执行之前
setup_method: 每个用例开始前执行
setup: 每个用例开始前执行
正在执行----test_one
.teardown: 每个用例结束后执行
teardown_method: 每个用例结束后执行
setup_method: 每个用例开始前执行
setup: 每个用例开始前执行
正在执行----test_two
Fteardown: 每个用例结束后执行
teardown_method: 每个用例结束后执行
setup_method: 每个用例开始前执行
setup: 每个用例开始前执行
正在执行----test_three
.teardown: 每个用例结束后执行
teardown_method: 每个用例结束后执行
teardown_class:所有用例执行之前
2.从结果看出,运行的优先级:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class
备注:这里setup_method和teardown_method的功能和setup/teardown功能是一样的,一般二者用其中一个即可
函数和类混合
1.如果一个.py的文件里面既有函数用例又有类和方法用例,运行顺序又是怎样的呢?
# coding:utf-8
import pytest
# 类和方法
** 作者:上海-悠悠 QQ交流群:588402570**
def setup_module():
print("setup_module:整个.py模块只执行一次")
print("比如:所有用例开始前只打开一次浏览器")
def teardown_module():
print("teardown_module:整个.py模块只执行一次")
print("比如:所有用例结束只最后关闭浏览器")
def setup_function():
print("setup_function:每个用例开始前都会执行")
def teardown_function():
print("teardown_function:每个用例结束前都会执行")
def test_one():
print("正在执行----test_one")
x = "this"
assert 'h' in x
def test_two():
print("正在执行----test_two")
x = "hello"
assert hasattr(x, 'check')
class TestCase():
def setup_class(self):
print("setup_class:所有用例执行之前")
def teardown_class(self):
print("teardown_class:所有用例执行之前")
def test_three(self):
print("正在执行----test_three")
x = "this"
assert 'h' in x
def test_four(self):
print("正在执行----test_four")
x = "hello"
assert hasattr(x, 'check')
if __name__ == "__main__":
pytest.main(["-s", "test_fixtclass.py"])
运行结果:
test_fixtclass.py setup_module:整个.py模块只执行一次
比如:所有用例开始前只打开一次浏览器
setup_function:每个用例开始前都会执行
正在执行----test_one
.teardown_function:每个用例结束前都会执行
setup_function:每个用例开始前都会执行
正在执行----test_two
Fteardown_function:每个用例结束前都会执行
setup_class:所有用例执行之前
正在执行----test_three
.正在执行----test_four
Fteardown_class:所有用例执行之前
teardown_module:整个.py模块只执行一次
比如:所有用例结束只最后关闭浏览器
2.从运行结果看出,setup_module/teardown_module的优先级是最大的,然后函数里面用到的setup_function/teardown_function与类里面的setup_class/teardown_class互不干涉
---------------------------------pytest结合selenium自动化完整版-------------------------
全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b
作者:上海-悠悠 QQ交流群:874033608
也可以关注下我的个人公众号:yoyoketang

pytest文档4-测试用例setup和teardown的更多相关文章
- pytest自动化2:测试用例setup和teardown
前言: pytest支持函数和类两种用例方式,针对每种情况都有不同的代码 pytest用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(s ...
- pytest文档6-fixture之yield实现teardown
前言 上一篇讲到fixture通过scope参数控制setup级别,既然有setup作为用例之前前的操作,用例执行完之后那肯定也有teardown操作. 这里用到fixture的teardown操作并 ...
- Pytest学习(三) - setup和teardown的使用
一.前言 从文章标题可以看出,就是初始化和释放的操作,根据我的java习惯来学习pytest,个人感觉没差太多,理解上也不是很难. 哦,对了,差点跑题了,这个框架是基于Python语言的,在学习的时候 ...
- pytest文档7-pytest-html生成html报告
前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...
- pytest文档3-pycharm运行pytest
前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...
- pytest_04_测试用例setup和teardown
学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次. 当然还有更高级一点的setupClass和teardownClass,需配合@ ...
- pytest 2.测试用例setup和teardown
之前我写的unittest的setup和teardown,还有setupClass和teardownClass(需要配合@classmethod装饰器一起使用),接下来就介绍pytest的类似于这类的 ...
- pytest文档51-内置fixture之cache使用
前言 pytest 运行完用例之后会生成一个 .pytest_cache 的缓存文件夹,用于记录用例的ids和上一次失败的用例. 方便我们在运行用例的时候加上--lf 和 --ff 参数,快速运行上一 ...
- pytest文档55-plugins插件开发
前言 前面一篇已经学会了使用hook函数改变pytest运行的结果,代码写在conftest.py文件,实际上就是本地的插件了. 当有一天你公司的小伙伴觉得你写的还不错,或者更多的小伙伴想要你这个功能 ...
随机推荐
- 以太坊go-ethereum客户端docker安装(二)开发(dev)环境搭建
在上一篇博客中,讲述了基于docker怎么搭建一个go-ethereum节点.作为开发人员,如果只是单纯的拥有一个Full node,还无法满足正常的开发.比如说,进行转账交易,你要考虑是否拥有一定的 ...
- hdu 5505(数论-gcd的应用)
GT and numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- Java Number类和Math类
Java Number类 一般的,当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double等. 然而,在实际开发过程中,我们经常会遇到需要使用对象,而不是内置数据类 ...
- Fedora下rstudio-server安装
安装 sudo yum install openssl sudo yum install mysql-server wget https://download2.rstudio.org/rstudio ...
- Hadoop案例(八)辅助排序和二次排序案例(GroupingComparator)
辅助排序和二次排序案例(GroupingComparator) 1.需求 有如下订单数据 订单id 商品id 成交金额 0000001 Pdt_01 222.8 0000001 Pdt_05 25.8 ...
- ubuntu sublime text 3 集成 nodejs 插件
下载nodejs插件地址:https://github.com/tanepiper/SublimeText-Nodejs 解压重命名文件夹为Nodejs打开sublime text : prefere ...
- 一个简单的ajax上传 上传进度显示
本例用了jquery.form.js请到演示页面查看 CSS Code <style> form { display: block; margin: 20px auto; backgrou ...
- Python的扩展接口[3] -> Matlab引擎 -> 使用 Python 调用 Matlab 程序
Python - Matlab 目录 Python-Matlab 引擎 Python-Matlab 数组 Python-Matlab 基本操作 Python-Matlab 调用 m 文件 Matlab ...
- 什么是DQL、DML、DDL、DCL
SQL(Structure Query Language)语言是数据库的核心语言. SQL的发展是从1974年开始的,其发展过程如下: 1974年-----由Boyce和Chamberlin提出,当时 ...
- Codeforces 408 E. Curious Array
$ >Codeforces \space 408 E. Curious Array<$ 题目大意 : 有一个长度为 \(n\) 的序列 \(a\) ,\(m\) 次操作,每一次操作给出 \ ...