之前我写的unittest的setup和teardown,还有setupClass和teardownClass(需要配合@classmethod装饰器一起使用),接下来就介绍pytest的类似于这类的固件。

(1.setup_function、teardown_function 2.setup_class、teardown_class 3.setup_method、teardown_method 4.setup_module、teardown_module)

setup/teardown和unittest里面的setup/teardown是一样的功能,这里setup_method和teardown_method的功能和setup/teardown功能是一样的,优先级是先执行setup_method,在执行setup。一般二者用其中一个即可就不详细介绍了。setup_class和teardown_class等价于unittest里面的setupClass和teardownClass

一、函数级的(setup_function、teardown_function)只对函数用例生效,而且不在类中使用

#!/usr/bin/env/python
# -*-coding:utf-8-*- import pytest """
只对函数用例生效,不在类中
setup_function
teardown_function
""" def setup_function():
print "setup_function():每个方法之前执行" def teardown_function():
print "teardown_function():每个方法之后执行" def test_01():
print "正在执行test1"
x = "this"
assert 'h' in x def test_02():
print "正在执行test2"
x = "hello"
assert hasattr(x,"hello") def add(a,b):
return a+b def test_add():
print "正在执行test_add()"
assert add(3,4) == 7 if __name__=="__main__":
pytest.main(["-s","test_function.py"]) 运行结果为:(-s为了显示用例的打印信息 -q只显示结果不显示过程)
可以看出执行的结果是:
setup_function--》 test_01 --》teardown_function
setup_function--》 test_02 --》teardown_function
setup_function--》 test_add --》teardown_function

二、类级的(setup_class、teardown_class)在类中使用,类执行之前运行一次,类执行之后运行一次

#!/usr/bin/env/python
# -*-coding:utf-8-*- """
在类之前和之后执行一次
setup_class
teardown_class
""" import pytest class TestClass(object): def setup_class(self):
print "setup_class(self):每个类之前执行一次" def teardown_class(self):
print "teardown_class(self):每个类之后执行一次" def add(self,a,b):
print "这是加法运算"
return a+b def test_01(self):
print "正在执行test1"
x = "this"
assert 'h' in x def test_add(self):
print "正在执行test_add()"
assert self.add(3, 4) == 7
执行结果:
可以看出执行的顺序是 setup_class --》 test1 --》test_add()--》teardown_class

三、类中方法级的(setup_method、teardown_method)在每一个方法之前执行一次,在每一个方法之后执行一次

#!/usr/bin/env/python
# -*-coding:utf-8-*- """
开始于方法始末(在类中)
setup_method
teardown_method
"""
import pytest class TestMethod(object): def setup_class(self):
print "setup_class(self):每个类之前执行一次\n" def teardown_class(self):
print "teardown_class(self):每个类之后执行一次" def setup_method(self):
print "setup_method(self):在每个方法之前执行" def teardown_method(self):
print "teardown_method(self):在每个方法之后执行\n" def add(self,a,b):
print "这是加法运算"
return a+b def test_01(self):
print "正在执行test1"
x = "this"
assert 'h' in x def test_add(self):
print "正在执行test_add()"
assert self.add(3, 4) == 7
执行结果: setup_class --》 setup_method -->test1 -->teardown_method --》setup_method --> test_add()--》teardown_method --> teardown_class

四、模块级的(setup_module、teardown_module)全局的,在模块执行前运行一遍,在模块执行后运行一遍

#!/usr/bin/env/python
# -*-coding:utf-8-*- import pytest
"""
开始于模块始末,全局的
setup_module
teardown_module
""" def setup_module():
print "setup_module():在模块最之前执行\n" def teardown_module():
print "teardown_module:在模块之后执行" def setup_function():
print "setup_function():每个方法之前执行" def teardown_function():
print "teardown_function():每个方法之后执行\n" def test_01():
print "正在执行test1"
x = "this"
assert 'h' in x def add(a,b):
return a+b def test_add():
print "正在执行test_add()"
assert add(3,4) == 7
运行结果:setup_module --> setup_function --> test_01--> teardown_function --> setup_function --> test_add()--> teardown_function --> teardown_module

五、当类和函数都有的时候

#!/usr/bin/env/python
# -*-coding:utf-8-*- """
在类之前和之后执行一次
setup_class
teardown_class
""" import pytest def setup_module():
print "setup_module():在模块最之前执行\n" def teardown_module():
print "teardown_module:在模块之后执行" def setup_function():
print "setup_function():每个方法之前执行" def teardown_function():
print "teardown_function():每个方法之后执行\n" def test_10():
print "正在执行test1"
x = "this"
assert 'h' in x def add0(a,b):
return a+b def test_add():
print "正在执行test_add()"
assert add0(3,4) == 7 class TestClass(object): def setup_class(self):
print "setup_class(self):每个类之前执行一次" def teardown_class(self):
print "teardown_class(self):每个类之后执行一次" def add(self,a,b):
print "这是加法运算"
return a+b def test_01(self):
print "正在执行test1"
x = "this"
assert 'h' in x def test_add(self):
print "正在执行test_add()"
assert self.add(3, 4) == 7 if __name__=="__main__":
pytest.main(["-s","test_class0.py"])
运行结果:可以看出来,都互不影响,setup_module还是在最之前执行,所有之后执行。
setup_modele --> setup_function -->test1 -->teardown_function --> setuo_function -->test_add -->teardown_function -->setup_class -->teardown_class-->taerdown_module

												

pytest 2.测试用例setup和teardown的更多相关文章

  1. pytest自动化2:测试用例setup和teardown

    前言: pytest支持函数和类两种用例方式,针对每种情况都有不同的代码 pytest用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(s ...

  2. pytest_04_测试用例setup和teardown

    学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次. 当然还有更高级一点的setupClass和teardownClass,需配合@ ...

  3. python:pytest中的setup和teardown

    原文:https://www.cnblogs.com/peiminer/p/9376352.html 之前我写的unittest的setup和teardown,还有setupClass和teardow ...

  4. pytest测试框架 -- setup和teardown等

    一.用例运行级别 1.函数级别(setup.teardown 或 setup_function.teardown_function): 仅对处于同作用域的测试函数有效(该函数定义不在类中,则对非类中测 ...

  5. pytest二:setup和teardown

    用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(setup_function/teardown_function)只对函数用例生效(不在 ...

  6. pytest用例setup和teardown

    函数式以下两种: setup_function/teardown_function  每个用例开始和结束调用一次 setup_module/teardown_module     setup_modu ...

  7. 【pytest】(十二)参数化测试用例中的setup和teardown要怎么写?

    还是一篇关于pytest的fixture在实际使用场景的分享. fixture我用来最多的就是写setup跟teardown了,那么现在有一个用例是测试一个列表接口,参数化了不同的状态值传参,来进行测 ...

  8. python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)

    pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...

  9. Pytest学习(三) - setup和teardown的使用

    一.前言 从文章标题可以看出,就是初始化和释放的操作,根据我的java习惯来学习pytest,个人感觉没差太多,理解上也不是很难. 哦,对了,差点跑题了,这个框架是基于Python语言的,在学习的时候 ...

随机推荐

  1. /proc/diskstats

    读取磁盘统计信息,如下所示: linux-HpdBKE:~ # cat /proc/diskstats sda sda1 sda2 dm- dm- dm- sda为整个硬盘的统计信息,sda1为第一个 ...

  2. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  3. BugFree 安装

    BugFree基于PHP和MySQL开发,是免费且开发源代码的缺陷管理系统.服务器端在Linux和Windows平台上都可以运行:客户端无需安装任何软件,通过IE,FireFox等浏览器就可以自由使用 ...

  4. 关于事务回滚,rollback tran到底要不要写?

    关于事务回滚,有些不明白,不知道rollback tran在什么时候用. begin tran update 表1 update 表2 commit tran 这种写法,在更新表1或表2时出错,事务会 ...

  5. 小米Note3 MIUI9可以用的XPosed框架

    资源来自论坛里的:http://www.miui.com/thread-6449305-1-1.html 首先需要刷入TWRP,资源在这个帖子里:http://www.miui.com/thread- ...

  6. 英特尔DRM内核驱动程序默认启用PSR2省电功能

    导读 英特尔DRM/KMS内核驱动程序很快就会启用PSR2面板自刷新功能,以便在英特尔支持的超极本/笔记本电脑上实现更多节能. 一段时间以来,英特尔的Direct Rendering Manager驱 ...

  7. Django通用视图APIView和视图集ViewSet的介绍和使用

    原 Django通用视图APIView和视图集ViewSet的介绍和使用 2018年10月21日 14:42:14 不睡觉假扮古尔丹 阅读数:630   1.APIView DRF框架的视图的基类是 ...

  8. 微服务配合docker使用

    1.docker 安装 rabbitmq 启动脚本: docker run -d --name rabbitmq --publish : \ --publish : --publish : --pub ...

  9. Zookeeper注册中心底层实现小记

    内容摘自微信公众号,程序员小灰.推荐-ing Zookeeper的数据模型 Zookeeper的数据模型是什么样子呢?它很像数据结构当中的树,也很像文件系统的目录. 树是由节点所组成,Zookeepe ...

  10. linu系统文件授权命令