自动化框架的两种断言设计(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主要是为中文自然语言处理而开发的工具包,也包含为实现这些任务的机器学习算法和数据集 ...
随机推荐
- LoadRunner时间戳函数web_save_timestamp_param
举例:1520822348346(13位,毫秒级) 做时间戳的目的是为了JS缓存和防止CSRF,在LR中可以简单的使用下面这个函数 web_save_timestamp_param 来生成时间戳 ...
- sql_1
order by SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC; SELECT Company, OrderNumber ...
- python中if语句的使用
1.对体重标准的判断 #coding:utf-8 height=170weight=65#weight=height-105if weight<height-105: print '您偏瘦!注意 ...
- 原生node写一个静态资源服务器
myanywhere 用原生node做一个简易阉割版的anywhere静态资源服务器,以提升对node与http的理解. 相关知识 es6及es7语法 http的相关网络知识 响应头 缓存相关 压缩相 ...
- MYSQL存储过程初步认知
存储过程(Stored Procedure): 一组可编程的函数,是为了完成特定功能的SQL语句集,经编译创建并保存在数据库中,用户可通过指定存储过程的名字并给定参数(需要时)来调用执行. 优点:将重 ...
- 【codeforces 799A】Carrot Cakes
[题目链接]:http://codeforces.com/contest/799/problem/A [题意] 你有一个烤炉; 每t秒能同时烤出k个蛋糕; 你可以在第一个烤炉在烤的时候;同时花费d秒建 ...
- java陷阱之spring事物未提交和回滚导致不可预知问题
案发现场 //防止全局配置了 所以这里定义sprnig 不托管事物 @Transactional(propagation = Propagation.NOT_SUPPORTED) public boo ...
- 面试宝典之基本的C#面试问答
下文是100个基本的C#面试问答清单.这些面试问题简单.直接了当,涵盖了C#最基本的概念,大部分和面向对象的概念相关.所以如果你在准备C#面试,我建议你必须掌握这100个基本的C#面试问答来复习你的C ...
- 小P寻宝记——好基友一起走
小P寻宝记--好基友一起走 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 话说.上次小P到伊利哇呀国旅行得到了一批宝藏.他是 ...
- 巧用select延时
在LINUX用户态的情况下.假设想要延时的话.用sleep是最合适的,可是,在有些情况下,须要更小单位的延时,ms us 也是要的.用循环获取到的延时是不精确的. 幸好,select函数巧用的话,是 ...