自动化框架的两种断言设计(pytest 版)
自动化测试断言失败时,根据不同业务场景,可能需要立即终止或继续执行。这里以 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 版)的更多相关文章
- 从上面的集合框架图可以看到,Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射
从上面的集合框架图可以看到,Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射.Collection 接口又有 3 ...
- 关于自动化与vTable两种暴露接口的区别-1未完......
COM组件有两种暴露组件接口的方式,一种是以虚拟列表的方式暴露:一种就是自动化方式. 虚拟列表(VTable): COM组件将自己所有的方法的地址以一个虚拟表的方式存放在一起,这个虚拟表是一种结构,有 ...
- (转)TestNG框架提供两种传入参数的方法:
1.从testng.xml传入参数. 如果参数是一些简单的值,可以直接在testng.xml中定义.这也是最常用的一种. 可以在测试用例中给参数一个默认值.这样,即使在xml文件中没有这个变量,你的测 ...
- Spring集成Quartz框架的两种方式。
可参考:https://blog.csdn.net/yk614294861/article/details/84324603 1.使用Spring与Quarta配置作业得两种方式: a.方式一,Met ...
- Web框架以及两种模式MVC,MTV
一.Web框架的本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. import socket def handle_reques ...
- 基于axis2框架的两种发布webservice的方法
这次在中韩的产品定义平台的开发,有幸接触到了通过自己写webservice给其他系统调用的项目. 具体开发背景:这个平台做了几个查询接口都是,都是用servlet方式处理请求,而这边系统之间是通过we ...
- Windows下Yii2框架的两种安装方式及升级最新版
第一种:归档文件形式安装(适合于没有安装composer的机器) 进入下载页https://www.yiiframework.com/download,选择下载第一个 下载完成之后是一个压缩包,解压文 ...
- Vue框架的两种使用方式
1.单页面应用:使用Vue CLI工具生成脚手架,这是最常见的使用方式,简单用模板生成一个HelloWorld Demo,可以学习Vue的SPA项目结构 2.传统多页面应用:通过script引入Vue ...
- NLP的两种工具的java版使用:复旦FudanNLP,中科院计算所ICTCLAS2013
编程语言:java 三种工具的简要介绍: FudanNLP google project上的介绍是: FudanNLP主要是为中文自然语言处理而开发的工具包,也包含为实现这些任务的机器学习算法和数据集 ...
随机推荐
- JavaScript:进制转换。
最近做了一些进制的转换的点. 十进制转二进制: let a = 45; a.toString(2); //"101101" let b = 123; b.toString(2); ...
- java:递归算法
JAVA中的递归是只一个方法在(满足条件时(或不满足条件时[这里的判断根据业务的实际需求写]))自己调用自己的方法名,要求参数和方法名一致, 然后根据判断跳出该方法,返回相应的返回值! 实例: 我们要 ...
- 【python正则】工作中常用的python正则代码
工作中常用的一些正则代码: 01.用户名正则 import re # 4到16位(字母,数字,下划线,减号)if re.match(r'^[a-zA-Z0-9_-]{4,16}$', "ab ...
- python网络爬虫高清PDF资料分享
1.python+网络爬虫开发实战中文PDF高清版 链接:https://pan.baidu.com/s/1bks8J9kgqCArUlEoDg4acA 提取码:h9y2 复制这段内容后打开百度网盘手 ...
- position:fixed div如何居中
div{position:fixed;margin:auto;left:0; right:0; top:0; bottom:0;width:200px; height:150px;}
- Oracle学习总结(6)—— SQL注入技术
不管用什么语言编写的Web应用,它们都用一个共同点,具有交互性并且多数是数据库驱动.在网络中,数据库驱动的Web应用随处可见,由此而存在的SQL注入是影响企业运营且最具破坏性的漏洞之一. SQL注入基 ...
- System v shm的key
shmget函数用于创建或打开一个共享内存区对象,shmget成功调用会返回一个共享内存区的标识符,供其它的共享内存区操作函数使用. key:用于创建共享内存区的键值,这个在前面其他System IP ...
- bram和dram差别
选择distributed memory generator和block memorygenerator标准: Dram和bram差别: 1.bram 的输出须要时钟,dram在给出地址后既可输出数据 ...
- POJ - 3257 Cow Roller Coaster (背包)
题目大意:要用N种材料建一条长为L的路,如今给出每种材料的长度w.起始地点x.发费c和耐久度f 问:在预算为B的情况下,建好这条路的最大耐久度是多少 解题思路:背包问题 dp[i][j]表示起始地点为 ...
- HDU - 4054 Hexadecimal View (2011 Asia Dalian Regional Contest)
题意:按要求输出.第一列是表示第几行.每行仅仅能有16个字节的字母,第二列是16进制的ASCII码.第三列大写和小写转换 思路:纯模拟,注意字母的十六进制是2位 #include <iostre ...