http://robot-framework.readthedocs.io/en/latest/autodoc/robot.utils.html#robot.utils.asserts.assert_raises_with_msg

robot.utils.asserts module

Convenience functions for testing both in unit and higher levels.

对于单元测试或者更高级别封装的function非常方便的函数。

Benefits:
  • Integrates 100% with unittest (see example below)
  • 和unittest 可以百分百整合,可共用。
  • Can be easily used without unittest (using unittest.TestCase when you only need convenient asserts is not so nice)
  • 可以非常方便的使用而不必引入unittest.TestCase的测试模块,当你只需要方便的使用asserts没有其他太高要求的时候。
  • Saved typing and shorter lines because no need to have ‘self.’ before asserts. These are static functions after all so that is OK.
  • 可以省略代码,因为不需要 self. 这种东东,当你在使用asserts之前。 这些都是静态功能, 所以没问题的。
  • All ‘equals’ methods (by default) report given values even if optional message given. This behavior can be controlled with the optional values argument.
  • 所有 ‘equals’ 方法默认可以提供report 值的除非在传参里面已经指定, 这些行为是可以通过参数控制的。
Drawbacks:
  • unittest is not able to filter as much non-interesting traceback away as with its own methods because AssertionErrors occur outside.
  • 由于断言错误发生在外部,因此unittest无法像使用其自己的方法那样过滤掉多余的不感兴趣的回溯。

Most of the functions are copied more or less directly from unittest.TestCase which comes with the following license. Further information about unittest in general can be found from http://pyunit.sourceforge.net/. This module can be used freely in same terms as unittest。

Examples:

 1 #!/usr/bin/python
 2 #coding:utf-8
 3
 4 import unittest
 5 from robot.utils.asserts import assert_equal
 6
 7 class MyTests(unittest.TestCase):
 8
 9     def test_old_style(self):
10         self.assertEqual(1, 2, ‘my msg’)
11
12     def test_new_style(self):
13         assert_equal(1, 1, 'my MSG')
14
15 if __name__ == '__main__':
16     unittest.main()

======================================================================
FAIL: test_old_style (__main__.MyTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "unittest_test.py", line 10, in test_old_style
    self.assertEqual(1, 2, 'my msg')
AssertionError: my msg

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=1)

#!/usr/bin/python
#coding:utf-8import unittest
from robot.utils.asserts import *class MyTests(unittest.TestCase):def test_old_style(self):self.assertEqual(1, 2, 'my msg')def test_new_style(self):assert_equal(1, 1, 'my MSG')def test_fail(self):'''robot.utils.asserts 的fail函数报错, 参数是report的值'''fail("haha")def test_assert(self):assert_false(True, 'if true report msg')if __name__ == '__main__':unittest.main()

E:\Project-workspace\python_test\nightScript\voicecard-python\08__VOIP>python unittest_test.pyFF.F======================================================================FAIL: test_assert (__main__.MyTests)----------------------------------------------------------------------Traceback (most recent call last):  File "unittest_test.py", line 22, in test_assert    assert_false(True, 'if true report msg')  File "E:\Software\Software\Python2.7.11\lib\site-packages\robot\utils\asserts.py", line 108, in assert_false    _report_failure(msg)  File "E:\Software\Software\Python2.7.11\lib\site-packages\robot\utils\asserts.py", line 219, in _report_failure    raise AssertionError(msg)AssertionError: if true report msg

======================================================================FAIL: test_fail (__main__.MyTests)----------------------------------------------------------------------Traceback (most recent call last):  File "unittest_test.py", line 20, in test_fail    fail("haha")  File "E:\Software\Software\Python2.7.11\lib\site-packages\robot\utils\asserts.py", line 102, in fail    _report_failure(msg)  File "E:\Software\Software\Python2.7.11\lib\site-packages\robot\utils\asserts.py", line 219, in _report_failure    raise AssertionError(msg)AssertionError: haha

======================================================================FAIL: test_old_style (__main__.MyTests)----------------------------------------------------------------------Traceback (most recent call last):  File "unittest_test.py", line 10, in test_old_style    self.assertEqual(1, 2, 'my msg')AssertionError: my msg

----------------------------------------------------------------------Ran 4 tests in 0.001s

FAILED (failures=3)

从robot提供的源码可以看出assert失败的处理直接raise 了ERROR出来,会跳出执行程序
def _report_failure(msg):if msg is None:raise AssertionError()raise AssertionError(msg)def _report_inequality_failure(obj1, obj2, msg, values, delim, extra=None):if not msg:msg = _get_default_message(obj1, obj2, delim)elif values:msg = '%s: %s' % (msg, _get_default_message(obj1, obj2, delim))if values and extra:msg += ' ' + extraraise AssertionError(msg)

其他的用法大同小异,具体的可以参照官方文档, 以上只是举了几个例子。robot.utils.asserts.fail(msg=None)[source]

Fail test immediately with the given message.

robot.utils.asserts.assert_false(expr, msg=None)[source]

Fail the test if the expression is True.

robot.utils.asserts.assert_true(expr, msg=None)[source]

Fail the test unless the expression is True.

robot.utils.asserts.assert_not_none(obj, msg=None, values=True)[source]

Fail the test if given object is None.

robot.utils.asserts.assert_none(obj, msg=None, values=True)[source]

Fail the test if given object is not None.

robot.utils.asserts.assert_raises(exc_class, callable_obj, *args, **kwargs)[source]

Fail unless an exception of class exc_class is thrown by callable_obj.

callable_obj is invoked with arguments args and keyword arguments kwargs. If a different type of exception is thrown, it will not be caught, and the test case will be deemed to have suffered an error, exactly as for an unexpected exception.

If a correct exception is raised, the exception instance is returned by this method.

robot.utils.asserts.assert_raises_with_msg(exc_class, expected_msg, callable_obj, *args, **kwargs)[source]

Similar to fail_unless_raises but also checks the exception message.

robot.utils.asserts.assert_equal(first, second, msg=None, values=True)[source]

Fail if given objects are unequal as determined by the ‘==’ operator.

robot.utils.asserts.assert_not_equal(first, second, msg=None, values=True)[source]

Fail if given objects are equal as determined by the ‘==’ operator.

robot.utils.asserts.assert_almost_equal(first, second, places=7, msg=None, values=True)[source]

Fail if the two objects are unequal after rounded to given places.

inequality is determined by object’s difference rounded to the given number of decimal places (default 7) and comparing to zero. Note that decimal places (from zero) are usually not the same as significant digits (measured from the most signficant digit).

robot.utils.asserts.assert_not_almost_equal(first, second, places=7, msg=None, values=True)[source]

Fail if the two objects are unequal after rounded to given places.

Equality is determined by object’s difference rounded to to the given number of decimal places (default 7) and comparing to zero. Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).

 

转载于:https://www.cnblogs.com/brownz/p/9244147.html

robotframework API 源码阅读笔记----robot.utils.asserts相关推荐

  1. 【Flink】Flink 源码阅读笔记(20)- Flink 基于 Mailbox 的线程模型

    1.概述 转载:Flink 源码阅读笔记(20)- Flink 基于 Mailbox 的线程模型 相似文章:[Flink]Flink 基于 MailBox 实现的 StreamTask 线程模型 Fl ...

  2. 【Flink】Flink 源码阅读笔记(15)- Flink SQL 整体执行框架

    1.概述 转载:Flink 源码阅读笔记(15)- Flink SQL 整体执行框架 在数据处理领域,无论是实时数据处理还是离线数据处理,使用 SQL 简化开发将会是未来的整体发展趋势.尽管 SQL ...

  3. dgl源码阅读笔记(3)——DeepWalk

    dgl源码阅读笔记(3)--DeepWalk 图神经网络开源库dgl阅读笔记 文章目录 dgl源码阅读笔记(3)--DeepWalk 图神经网络开源库dgl阅读笔记 @[TOC](文章目录) 前言 一 ...

  4. Go-Excelize API源码阅读(十九)——SetHeaderFooter

    Go-Excelize API源码阅读(十九)--SetHeaderFooter 开源摘星计划(WeOpen Star) 是由腾源会 2022 年推出的全新项目,旨在为开源人提供成长激励,为开源项目提 ...

  5. Go-Excelize API源码阅读(三十一)——ProtectSheet(sheet string, settings *SheetProtectionOptions)

    Go-Excelize API源码阅读(三十一)-- ProtectSheet(sheet string, settings *SheetProtectionOptions) 开源摘星计划(WeOpe ...

  6. Transformers包tokenizer.encode()方法源码阅读笔记

    Transformers包tokenizer.encode()方法源码阅读笔记_天才小呵呵的博客-CSDN博客_tokenizer.encode

  7. 源码阅读笔记 BiLSTM+CRF做NER任务 流程图

    源码阅读笔记 BiLSTM+CRF做NER任务(二) 源码地址:https://github.com/ZhixiuYe/NER-pytorch 本篇正式进入源码的阅读,按照流程顺序,一一解剖. 一.流 ...

  8. 代码分析:NASM源码阅读笔记

    NASM源码阅读笔记 NASM(Netwide Assembler)的使用文档和代码间的注释相当齐全,这给阅读源码 提供了很大的方便.按作者的说法,这是一个模块化的,可重用的x86汇编器, 而且能够被 ...

  9. CI框架源码阅读笔记4 引导文件CodeIgniter.php

    到了这里,终于进入CI框架的核心了.既然是"引导"文件,那么就是对用户的请求.参数等做相应的导向,让用户请求和数据流按照正确的线路各就各位.例如,用户的请求url: http:// ...

最新文章

  1. Swift 异常处理
  2. AI Time 7 | 人机交互的终极状态——人机共生
  3. Google 的 QUIC 华丽转身成为下一代网络协议: HTTP/3.0
  4. SegmentFault D-Day 北京:大数据
  5. RPC 【Remote Procedure Call】 原理
  6. SQL 2005 数据库镜像
  7. 路由器 VS OSI七层模型
  8. 面向对象 委托
  9. 如何辨别真假柯达胶卷
  10. 微信小程序学习笔记:选项卡
  11. 进销存设计与分析_库存汇总表(13)
  12. python文件管理器_Tkinter 之文件管理器
  13. 嘉立创EDA专业版--文件名称修改、原理图尺寸修改与文本放置
  14. python入门——python基础语法
  15. Qt、C++实现五子棋人机对战与本地双人对战
  16. 真心话大冒险小程序python实现
  17. 会计专业春季高考计算机模拟,2017春季高考财会类专业知识模拟试题及答案
  18. 左程云算法 - 公开课笔记
  19. vue-baidu-map插件中bm-marker组件嵌套bm-info-window组件使用,在地图上生成多个点
  20. 小网站到亿级PV的经过

热门文章

  1. Kubernetes 1.12.0 Kube-controller-manager之replicaset-controller源码阅读分析
  2. 胶原蛋白真的有效果吗
  3. echarts 折线图 设置y轴最小刻度_Vue 项目如何使用Echarts , 手摸手带你入门
  4. 网站备案所需准备材料
  5. 大厂资历程序员求职以为很容易,没想到栽在这里…
  6. Netdata 新一代系统
  7. java开发者社区_Jeeplus开发者社区
  8. SAP MES与PP模块的集成
  9. Unity实战 RTS3D即时战略游戏开发(四)
  10. SQL报错Data truncated for column ‘password‘ at row 8