目录

如何实现xunit样式setup\teardown

模块级setup/teardown

类级别setup/teardown

方法和功能级别setup/teardown


如何实现xunit样式setup\teardown

本节描述了如何在每个模块/类/函数的基础上实现fixture(设置和拆卸测试状态)的经典和流行的方法。

注意:

虽然这些setup/teardown方法对于来自unittest或nose背景的用户来说既简单又熟悉,但您也可以考虑使用pytest更强大的fixture机制,该机制利用了依赖注入的概念,从而为管理测试状态提供了更模块化、更可扩展的方法,尤其是对于大型项目和功能测试。您可以在同一文件中混合使用两种fixture机制,但unittest.TestCase子类的测试方法无法接收fixture参数。

模块级setup/teardown

如果在一个模块中有多个测试函数和测试类,则可以选择实现以下fixture方法,这些方法通常会对所有函数调用一次:

def setup_module(module):"""setup any state specific to the execution of the given module."""def teardown_module(module):"""teardown any state that was previously setup with a setup_modulemethod."""

从pytest-3.0开始,模块参数是可选的。

类级别setup/teardown

类似地,在调用类的所有测试方法之前和之后,在类级别调用以下方法:

@classmethod
def setup_class(cls):"""setup any state specific to the execution of the given class (whichusually contains tests)."""@classmethod
def teardown_class(cls):"""teardown any state that was previously setup with a call tosetup_class."""

方法和功能级别setup/teardown

类似地,在每次方法调用时都会调用以下方法:

def setup_method(self, method):"""setup any state tied to the execution of the given method in aclass.  setup_method is invoked for every test method of a class."""def teardown_method(self, method):"""teardown any state that was previously setup with a setup_methodcall."""

从pytest-3.0开始,方法参数是可选的。

如果您更愿意直接在模块级别定义测试函数,您也可以使用以下函数来实现fixture:

def setup_function(function):"""setup any state tied to the execution of the given function.Invoked for every test function in the module."""def teardown_function(function):"""teardown any state that was previously setup with a setup_functioncall."""

从pytest-3.0开始,函数参数是可选的。

评论:

  • 在每个测试过程中,可以多次调用setup/teardown。
  • 如果相应的设置函数存在并且失败/被跳过,则不调用teardown函数。
  • 在pytest-4.2之前,xunit风格的函数不遵守fixture的作用域规则,因此,例如,可以在会话作用域的autouse fixture之前调用setup_method。
  • 现在,xunit风格的函数与fixture机制集成在一起,并遵守调用中涉及的fixture的适当范围规则。

Pytest setup teardown相关推荐

  1. 第六:Pytest中的setup/teardown

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

  2. Pytest测试框架(二):pytest 的setup/teardown方法

    系列文章目录 Pytest测试框架(一):pytest安装及用例执行 Pytest测试框架(二):pytest 的setup/teardown方法 Pytest测试框架(三):pytest fixtu ...

  3. 三、pytest接口自动化之pytest中setup/teardown,setup_class/teardown_class讲解

    pytest框架实现的前后置的处理(固件,夹具),很多种方式,常见的三种. 一.setup/teardown,setup_class/teardown_class 为什么需要这些功能? Class T ...

  4. setup/teardown 的用法

    # 方法级别函数 setup_method/teardown_method和setup/teardown对类有效,也位于类中,这两个效果一样,在测试类中每个测试方法前后调用一次.# 每条用例都要执行一 ...

  5. gtest 中Setup TearDown SetUpTestCase和TearDownTestCase 的区别

    前言 在单元测试中,我们经常需要在某个测试套件.测试用例或者整个测试运行之前进行前置条件设置及检查,或者运行之后对运行结果进行校验等操作.在gtest中,称之为事件机制.gtest将事件按照作用的范围 ...

  6. pytest—setup和teardown简单用法

    前言 小伙伴们好呀,我又来了我们今天聊聊关于pytest前后置应用,使用过unittest的小伙伴们都知道,setup和teardown是用来处理用例的开始前工作和结束后的工作,其中还有setupcl ...

  7. 【pytest】概述pytest——setup、teardown方法和conftest中的fixture用法,来执行测试用例的前置/后置条件语句操作

    大家好,我是好学的小师弟.今天和大家分享下-在pytest中的前置/后置操作. 所谓的前置/后置操作,就是在测试用例执行前,你预先要执行的一些步骤:在测试用例执行完毕后,你要执行的一些数据清理/备份工 ...

  8. setup/teardown用法汇总

    pytest中的前置 pytest比较强大,提供了不仅仅一种方法的前置和后置: setup.teardown 先介绍第一个大家都比较熟悉的与unittest中的书写一直,这个可以在类中使用,也可以在类 ...

  9. katalon等待时间、断言、setup teardown

    设置等待时间 硬等待: 不管结果真么样等待时间一过立马执行以后代码 // 线程休眠5秒 Thread.sleep(5000) 每条语句等待: 每一个WebUI.方法都等待一段时间,这就要手动设置一下了 ...

最新文章

  1. centos下修改mysql默认端口_CentOS下修改Apache默认端口80
  2. Spring JdbcTemplate快速入门
  3. intellij关联本地的maven的repository
  4. C# 枚举(Enum)
  5. Oracle入门(一)之入门级知识详解
  6. 数据冒险控制冒险_劳动生产率和其他冒险
  7. Seq(HDU-6672)
  8. redis入门——集群篇
  9. 对象行为型 - 模版方法模式
  10. OpenCV-绘制同心圆
  11. ROS激光雷达小车自主导航学习(一)(持续更新中)
  12. STM32_4键位触摸按键实现单击、双击、三击、长按1s、长按2s以上每隔200ms、左右滑动1-3个键位等多功能程序
  13. 【问题解决】error: pathspec ‘XXX‘ did not match any file(s) known to git
  14. 如何把word转成pdf格式以及如何从指定页开始设置页码技巧
  15. 如何在模拟器里面脱360的壳
  16. Python学习心得——列表和元组的操作方法
  17. 介绍四种绘制词云图的方法
  18. hdu 5055(坑)
  19. VMware虚拟机的使用
  20. [家里蹲大学数学杂志]第266期中南大学2013年高等代数考研试题参考解答

热门文章

  1. 【Axure】实例:微信登录
  2. 小米的隔空充电技术或许是概念产品,需要跨越的技术难点太多
  3. poj 2683 Ohgas' Fortune 利率计算
  4. Linux学习笔记——文件IO
  5. 格密码LLL算法:如何解决最短向量SVP问题(2)
  6. pip 不使用本地缓存安装指定库
  7. 互联网理财系统开发-满足投资人随投随取、多种产品、多种选择、风险更小、收益更多
  8. 《HelloGitHub》第 48 期
  9. 装修公司选择Tips
  10. Java导出Word(导出图片,类,List)