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:

 #!/usr/bin/python
#coding:utf-8 import unittest
from robot.utils.asserts import assert_equal 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') if __name__ == '__main__':
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-8 import 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.py
FF.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 += ' ' + extra
raise 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).

 

robotframework API 源码阅读笔记----robot.utils.asserts的更多相关文章

  1. jdk源码阅读笔记-LinkedHashMap

    Map是Java collection framework 中重要的组成部分,特别是HashMap是在我们在日常的开发的过程中使用的最多的一个集合.但是遗憾的是,存放在HashMap中元素都是无序的, ...

  2. CI框架源码阅读笔记5 基准测试 BenchMark.php

    上一篇博客(CI框架源码阅读笔记4 引导文件CodeIgniter.php)中,我们已经看到:CI中核心流程的核心功能都是由不同的组件来完成的.这些组件类似于一个一个单独的模块,不同的模块完成不同的功 ...

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

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

  4. CI框架源码阅读笔记3 全局函数Common.php

    从本篇开始,将深入CI框架的内部,一步步去探索这个框架的实现.结构和设计. Common.php文件定义了一系列的全局函数(一般来说,全局函数具有最高的加载优先权,因此大多数的框架中BootStrap ...

  5. CI框架源码阅读笔记2 一切的入口 index.php

    上一节(CI框架源码阅读笔记1 - 环境准备.基本术语和框架流程)中,我们提到了CI框架的基本流程,这里再次贴出流程图,以备参考: 作为CI框架的入口文件,源码阅读,自然由此开始.在源码阅读的过程中, ...

  6. 源码阅读笔记 - 1 MSVC2015中的std::sort

    大约寒假开始的时候我就已经把std::sort的源码阅读完毕并理解其中的做法了,到了寒假结尾,姑且把它写出来 这是我的第一篇源码阅读笔记,以后会发更多的,包括算法和库实现,源码会按照我自己的代码风格格 ...

  7. Three.js源码阅读笔记-5

    Core::Ray 该类用来表示空间中的“射线”,主要用来进行碰撞检测. THREE.Ray = function ( origin, direction ) { this.origin = ( or ...

  8. PHP源码阅读笔记一(explode和implode函数分析)

    PHP源码阅读笔记一一.explode和implode函数array explode ( string separator, string string [, int limit] )此函数返回由字符 ...

  9. AQS源码阅读笔记(一)

    AQS源码阅读笔记 先看下这个类张非常重要的一个静态内部类Node.如下: static final class Node { //表示当前节点以共享模式等待锁 static final Node S ...

随机推荐

  1. LCT的一些坑【已经变成坑点集合了233】

    好了蠢蠢的我写了第一个LCT模板就炸掉了QAQ 开个blog记一下我能出多少锅. 1.splay写挂了hhh这个你真的是蠢 2.这个奇怪的东西 bool not_root(int x){return ...

  2. ssm框架整合抽取BaseDao接口

    import java.io.Serializable; import java.util.List; /** * DAO基础操作模板 * * @param <T> 泛型 */ publi ...

  3. Java EE的优越性主要表现在哪些方面

    J2 EE的优越性主要表现在哪些方面 J2EE基于JAVA 技术,与平台无关. J2EE拥有开放标准,许多大型公司实现了对该规范支持的应用服务器.如BEA ,IBM,ORACLE等. J2EE提供相当 ...

  4. SoupUI 结合loadrunner压力测试

    SoupUI 结合loadrunner压力测试 上一篇介绍了SoupUI接口测试,因为工作需要,需要在loadrunner进行websocket的压力测试,当然,SoupUI本身也是可以做性能测试的 ...

  5. [BZOJ2600] ricehub

    问题描述 乡间有一条笔直而长的路称为"米道".沿着这条米道上 R 块稻田,每块稻田的坐标均为一个 1 到 L 之间(含 1 和 L)的整数.这些稻田按照坐标以不减的顺序给出,即对于 ...

  6. LeetCode--058--最后一个单词(java)

    给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: &quo ...

  7. php-redis 使用命令

    PHP 使用redis 一些命令参考:https://www.jianshu.com/p/68b7114a1d70

  8. IDEA搭建spingboot项目

    1.Springboot2.x是依赖于JDK1.8及以上版本的.因此先在电脑上安装JDK1.8,由于公司的项目用的是JDK1.7,所以要在JDK1.7的基础上在安装JDK1.8,让两者同时存在于电脑上 ...

  9. 用 margin 还是用 padding ?

    边界(margin):元素周围生成额外的空白区."空白区"通常是指其他元素不能出现且父元素背景可见的区域. 补白(padding):补白位于元素框的边界与内容区之间.很自然,用于影 ...

  10. ZOJ 3329 One Person Game(概率DP,求期望)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题目大意: 有三个骰子,分别有K1,K2,K3个面,一次投掷可以得到三个 ...