自动化测试断言失败时,根据不同业务场景,可能需要立即终止或继续执行。这里以 Appium + pytest 为例。

一. 断言失败立即终止

用途一:用例的预期结果是其他用例的前提条件时,assert 失败需要立即终止,这是我们设计自动化测试用例时最常见的场景。

用途二:用例中的任何一个步骤执行失败时,立即终止,因为步骤都执行失败了,没有继续执行下一步的必要。方案:思路与用途一类似,这里把每个测试步骤当做都有一个预期结果(True),封装每一个具体步骤,步骤结果返回布尔值。对该结果进行 assert,False时立即终止。

 def click(self, step):
method_name = sys._getframe().f_code.co_name
try:
element = self.find_element(**step['element_loc'])
if step.has_key('times'):
for i in range(step['times']):
element.click()
else:
element.click()
return True
except:
print u'%s失败' % method_name
return False
 for step in self.case_steps:
assert self.operate(step), 'error in step: %s' % step

二. 断言失败继续执行

主要使用了两个函数 expect, assert_expectations 。

Demo: test_delayed_assert.py

 from delayed_assert import expect, assert_expectations

 def test_should_pass():
expect(1 == 1, 'one is one')
assert_expectations() def test_should_fail():
expect(1 == 2)
x = 1
y = 2
expect(x == y, 'x:%s y:%s' % (x,y))
expect(1 == 1)
assert_expectations()

Module: delayedAssert.py

 '''
Implements one form of delayed assertions. Interface is 2 functions: expect(expr, msg=None)
: Evaluate 'expr' as a boolean, and keeps track of failures assert_expectations()
: raises an assert if an expect() calls failed Usage Example: from expectations import expect, assert_expectations def test_should_pass():
expect(1 == 1, 'one is one')
assert_expectations() def test_should_fail():
expect(1 == 2, 'one is two')
expect(1 == 3, 'one is three')
assert_expectations()
''' # --------------------------------------------------- def expect(expr, msg=None):
'keeps track of failed expectations'
if not expr:
_log_failure(msg) def assert_expectations():
'raise an assert if there are any failed expectations'
if _failed_expectations:
assert False, _report_failures() # --------------------------------------------------- import inspect
import os.path _failed_expectations = [] def _log_failure(msg=None):
(filename, line, funcname, contextlist) = inspect.stack()[2][1:5]
filename = os.path.basename(filename)
context = contextlist[0]
_failed_expectations.append('file "%s", line %s, in %s()%s\n%s' %
(filename, line, funcname, (('\n%s' % msg) if msg else ''), context)) def _report_failures():
global _failed_expectations
if _failed_expectations:
(filename, line, funcname) = inspect.stack()[2][1:4]
report = [
'\n\nassert_expectations() called from',
'"%s" line %s, in %s()\n' % (os.path.basename(filename), line, funcname),
'Failed Expectations:%s\n' % len(_failed_expectations)]
for i,failure in enumerate(_failed_expectations, start=1):
report.append('%d: %s' % (i, failure))
_failed_expectations = []
return ('\n'.join(report)) # ---------------------------------------------------
# _log_failure() notes
#
# stack() returns a list of frame records
# 0 is the _log_failure() function
# 1 is the expect() function
# 2 is the function that called expect(), that's what we want
#
# a frame record is a tuple like this:
# (frame, filename, line, funcname, contextlist, index)
# we're mainly interested in the middle 4,
# ---------------------------------------------------

参考:Delayed assert / multiple failures per test

自动化框架的两种断言设计(pytest 版)的更多相关文章

  1. 从上面的集合框架图可以看到,Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射

    从上面的集合框架图可以看到,Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射.Collection 接口又有 3 ...

  2. 关于自动化与vTable两种暴露接口的区别-1未完......

    COM组件有两种暴露组件接口的方式,一种是以虚拟列表的方式暴露:一种就是自动化方式. 虚拟列表(VTable): COM组件将自己所有的方法的地址以一个虚拟表的方式存放在一起,这个虚拟表是一种结构,有 ...

  3. (转)TestNG框架提供两种传入参数的方法:

    1.从testng.xml传入参数. 如果参数是一些简单的值,可以直接在testng.xml中定义.这也是最常用的一种. 可以在测试用例中给参数一个默认值.这样,即使在xml文件中没有这个变量,你的测 ...

  4. Spring集成Quartz框架的两种方式。

    可参考:https://blog.csdn.net/yk614294861/article/details/84324603 1.使用Spring与Quarta配置作业得两种方式: a.方式一,Met ...

  5. Web框架以及两种模式MVC,MTV

    一.Web框架的本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. import socket def handle_reques ...

  6. 基于axis2框架的两种发布webservice的方法

    这次在中韩的产品定义平台的开发,有幸接触到了通过自己写webservice给其他系统调用的项目. 具体开发背景:这个平台做了几个查询接口都是,都是用servlet方式处理请求,而这边系统之间是通过we ...

  7. Windows下Yii2框架的两种安装方式及升级最新版

    第一种:归档文件形式安装(适合于没有安装composer的机器) 进入下载页https://www.yiiframework.com/download,选择下载第一个 下载完成之后是一个压缩包,解压文 ...

  8. Vue框架的两种使用方式

    1.单页面应用:使用Vue CLI工具生成脚手架,这是最常见的使用方式,简单用模板生成一个HelloWorld Demo,可以学习Vue的SPA项目结构 2.传统多页面应用:通过script引入Vue ...

  9. NLP的两种工具的java版使用:复旦FudanNLP,中科院计算所ICTCLAS2013

    编程语言:java 三种工具的简要介绍: FudanNLP google project上的介绍是: FudanNLP主要是为中文自然语言处理而开发的工具包,也包含为实现这些任务的机器学习算法和数据集 ...

随机推荐

  1. jquery插件生成简单二维码

    除了利用第三方网站生成二维码外,这是一个比较简单的办法吧. <script src="/Scripts/jquery.qrcode.min.js" type="te ...

  2. nutz 自定义sql的使用

    虽然提供了Cnd,但是用起来是觉得有点不方便,然后就直接编写Sql语句.nutz提供了一些方法. Nutz.Dao 的自定义 SQL 部分的解决方案是: // 不推荐使用 用户可以硬编码 SQL 语句 ...

  3. js代码 注释 test

    <script type="text/javascript"> var obj = { tyep: 'GET', url: '/Backstage/Home/Menu' ...

  4. <转>Socket编程——基础介绍

    最近系统的看了下unix网络编程的一些内容,对socket的理解有了进一步的加深,在看APUE的时候,那会儿看socket上面介绍的比较少,只是模糊的懂了如何去写一个简单的TCP服务端和客户端,对其中 ...

  5. 【转】【Oracle 集群】Linux下Oracle RAC集群搭建之Oracle DataBase安装(八)

    原文地址:http://www.cnblogs.com/baiboy/p/orc8.html   阅读目录 目录 数据库安装 参考文献 相关文章 Oracle 11G RAC数据库安装(八) 概述:写 ...

  6. 关闭浏览器 清除session

    捕获关闭浏览器的事件 关于关闭IE清空session的总结 Session过期会清楚session 还可以手动清除session实现关闭浏览器时清除session的方法

  7. Vue学习之路第十一篇:为页面元素设置class类样式

    1.class为页面元素的一个属性,通过前面第五篇的内容可知,操作属性需要使用到v-bind指定(也可简写为 :). 2.先来看一个简单的页面样式内容: <!DOCTYPE html> & ...

  8. Linux设备驱动--块设备(一)之概念和框架(转)

    基本概念   块设备(blockdevice) --- 是一种具有一定结构的随机存取设备,对这种设备的读写是按块进行的,他使用缓冲区来存放暂时的数据,待条件成熟后,从缓存一次性写入设备或者从设备一次性 ...

  9. selenium+java处理鼠标悬停

    1.元素比较明确(可视) 2.元素隐藏,需要鼠标移动到一定地方才显现(下图为某论坛列表,需要将鼠标移动到列表才显示操作按钮)

  10. C#中的string驻留池

    刚开始学习C#的时候,就听说CLR对于String类有一种特别的内存管理机制:有时候,明明声明了两个String类的对象,但是他们偏偏却指向同一个实例.如下: String s1 = "He ...