python nose测试框架全面介绍三
三、nose的测试工具集
nose.tools模块提供了一系列的小工具,包括测试执行时间、异常输出及unittest框架中所有的assert功能。
为了使写用例更加容易,nose.tools提供了部分便利的功能函数,下面写几个常用的,如下:
nose.tools.ok_(expr, msg=None)
标准的assert,例子如下:
from nose.tools import eq_ def test_lean_2():
print "test_learn_2"
ok_(4==3,msg="Error")
运行结果如下:
E:\workspace\nosetest_lear\test_case>nosetests -v test_case_0001:test_lean_2
test_case_0001.test_lean_2 ... FAIL ======================================================================
FAIL: test_case_0001.test_lean_2
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "E:\workspace\nosetest_lear\test_case\test_case_0001.py", line 26, in tes
t_lean_2
ok_(4==3,msg="xxx")
AssertionError: xxx
-------------------- >> begin captured stdout << ---------------------
test_learn_2 --------------------- >> end captured stdout << ---------------------- ----------------------------------------------------------------------
Ran 1 test in 0.045s FAILED (failures=1) E:\workspace\nosetest_lear\test_case>
nose.tools.eq_(a, b, msg=None)
将参数a与b快速对比
from nose.tools import eq_ def test_learn_1():
eq_(5, 6, msg="Wrong")
运行结果如下:
======================================================================
FAIL: test_case_0001.test_learn_1
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "E:\workspace\nosetest_lear\test_case\test_case_0001.py", line 20, in tes
t_learn_1
eq_(5, 6, msg="Wrong")
AssertionError: Wrong
nose.tools.assert_in(member, container, msg=None)
代码如下:
from nose.tools import assert_in
def test_lean_5():
assert_in("aaa",'bbb',msg="test in failed")
运行结果如下:
======================================================================
FAIL: test_case_0002.test_lean_5
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "E:\workspace\nosetest_lear\test_case\test_case_0002.py", line 24, in tes
t_lean_5
assert_in("aaa",'bbb',msg="test in failed")
AssertionError: test in failed ----------------------------------------------------------------------
其它的assert这里就不说了,nose.tools包含很多内容,可以直接查询。
下面再介绍几个有用的nose.tools工具
nose.tools.set_trace()
-------------单步调试工具,在多个模块,大程序时这个功能好用。内部使用的是pdb.set_trace
代码如下:
from nose.tools import assert_in
from nose.tools import set_trace def test_lean_5():
set_trace()
assert_in("aaa",'bbb',msg="test in failed")
结果如下:
test_case_0002.test_lean_5 ... > e:\workspace\nosetest_lear\test_case\test_case_
0002.py(26)test_lean_5()
-> assert_in("aaa",'bbb',msg="test in failed")
(Pdb) n
AssertionError: Assertio...failed',)
> e:\workspace\nosetest_lear\test_case\test_case_0002.py(26)test_lean_5()
-> assert_in("aaa",'bbb',msg="test in failed")
(Pdb) n
--Return--
> e:\workspace\nosetest_lear\test_case\test_case_0002.py(26)test_lean_5()->None
-> assert_in("aaa",'bbb',msg="test in failed")
(Pdb) n
AssertionError: Assertio...failed',)
> c:\python27\lib\site-packages\nose\case.py(197)runTest()
-> self.test(*self.arg)
(Pdb) c
FAIL
0002 test teardown ======================================================================
FAIL: test_case_0002.test_lean_5
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "E:\workspace\nosetest_lear\test_case\test_case_0002.py", line 26, in tes
t_lean_5
assert_in("aaa",'bbb',msg="test in failed")
AssertionError: test in failed
具体的pdb调试手段可参见http://www.cnblogs.com/chencheng/archive/2013/07/07/3161778.html
nose.tools.timed(limit)
测试必须在设定的时间内(以秒为单位)完成 ,否则测试失败;代码如下:
from nose.tools import timed
import time @timed(1)
def test_lean_5():
time.sleep(2)
pass
测试结果如下:
test_case_0002.test_lean_5 ... FAIL ======================================================================
FAIL: test_case_0002.test_lean_5
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "c:\python27\lib\site-packages\nose\tools\nontrivial.py", line 100, in ne
wfunc
raise TimeExpired("Time limit (%s) exceeded" % limit)
TimeExpired: Time limit (1) exceeded ----------------------------------------------------------------------
Ran 1 test in 2.006s FAILED (failures=1)
nose.tools中还有很多assert的函数工具,不一一介绍了,列出表如下,需要的时候可以使用。
| assert_equal(first, second, msg=None) | 两个对像对比,使用"=="操作对比 |
| assert_not_equal(first, second, msg=None) | 不相等 |
| assert_true(expr, msg=None) | 判定表达式是否为真 |
| assert_false(expr, msg=None) | 判定表达式是否为假 |
| assert_is(expr1, expr2, msg=None) | expr1 is expr2 |
| assert_is_not(expr1, expr2, msg=None) | |
| assert_is_none(obj, msg=None) | 为空 |
| assert_is_not_none(obj, msg=None) | 不为空 |
| assert_in(member, container, msg=None) | merber in container判断 |
| assert_not_in(member, container, msg=None) | 不包含判断 |
| assert_is_instance(obj, cls, msg=None) | |
| assert_not_is_instance(obj, cls, msg=None) | |
|
assert_raises_regexp(expected_exception, expected_regexp, |
|
| assert_almost_equal(first, second, places=None, msg=None, delta=None) | |
| assert_greater(a, b, msg=None) | |
| assert_greater_equal(a, b, msg=None) | |
| assert_less(a, b, msg=None) | |
| assert_less_equal(a, b, msg=None) | |
| assert_regexp_matches(text, expected_regexp, msg=None) | |
| assert_not_regexp_matches(text, unexpected_regexp, msg=None) | |
| assert_items_equal(expected_seq, actual_seq, msg=None) | |
| assert_dict_contains_subset(expected, actual, msg=None) | |
| assert_multi_line_equal(first, second, msg=None) | |
| assert_sequence_equal(seq1, seq2, msg=None, seq_type=None) | |
| assert_list_equal(list1, list2, msg=None) | |
| assert_tuple_equal(tuple1, tuple2, msg=None) | |
| assert_set_equal(set1, set2, msg=None) | |
| assert_dict_equal(d1, d2, msg=None) |
下节将介绍nose的内置插件的使用
python nose测试框架全面介绍三的更多相关文章
- python nose测试框架全面介绍十---用例的跳过
又来写nose了,这次主要介绍nose中的用例跳过应用,之前也有介绍,见python nose测试框架全面介绍四,但介绍的不详细.下面详细解析下 nose自带的SkipTest 先看看nose自带的S ...
- python nose测试框架全面介绍七--日志相关
引: 之前使用nose框架时,一直使用--logging-config的log文件来生成日志,具体的log配置可见之前python nose测试框架全面介绍四. 但使用一段时间后,发出一个问题,生成的 ...
- python nose测试框架全面介绍六--框架函数别名
之前python nose测试框架全面介绍二中介绍了nose框架的基本构成,但在实际应该中我们也会到setup_function等一系列的名字,查看管网后,我们罗列下nose框架中函数的别名 1.pa ...
- python nose测试框架全面介绍五--attr介绍
之前写了一系列nose框架的,这篇介绍下attr tag 在nose框架中attr用来标识用例,使得在运行时可以通过标识来执行用例,之前在nose测试框架全面介绍四中有说明,但没有说明清楚,这里再总结 ...
- python nose测试框架全面介绍一
一.简介 nose 是python自带框架unttest的扩展,使测试更简单高效:nose是一个开源的项目,可以在官网上下载源码 1.快速安装 有以下几中安装方式: easy_install ...
- python nose测试框架全面介绍十二 ----用例执行顺序打乱
在实际执行自动化测试时,发现我们的用例在使用同一个资源的操作时,用例的执行顺序对测试结果有影响,在手工测试时是完全没法覆盖的. 但每一次都是按用例名字来执行,怎么打乱来执行的. 在网上看到一个有意思的 ...
- python nose测试框架全面介绍四
四.内部插件介绍 1.Attrib 标记,用于筛选用例 在很多时候,用例可以分不同的等级来运行,在nose中很增加了这个功能,使用attrib将用例进行划分 有两种方式: ef test_big_do ...
- python nose测试框架全面介绍二
二.基本使用 nosetest脚本的使用(在安装完nose之后) nosetests [options] [(optional) test files or directories] 我们可以使用配置 ...
- python nose测试框架全面介绍十三 ---怎么写nose插件
之前有一篇文章介绍了自己写的插件 nose进度插件,但最近有朋友问我,看着nose的官方文档写的插件没用,下面再详细介绍一下 一.准备 1.新建一个文件夹,随便文件夹的名字,假设文件夹放在f://aa ...
随机推荐
- UNIX环境编程学习笔记(9)——文件I/O之文件访问权限的屏蔽和更改
lienhua342014-09-10 1 文件访问权限 在文件访问权限和进程访问控制中,我们已经讲述过文件访问权限位,为了方便,我们重新列在下面, 表 1: 文件的 9 个访问权限位 st_mod ...
- lrzsz离线安装方法
lrzsz离线安装方法 到网上下载lrzsz安装包,这里以lrzsz-0.12.20.tar.gz为例 2 打开终端 cd 到安装包所在目录 tar zxvf lrzsz-0.12.20.tar.gz ...
- 《HTTP权威指南》学习笔记——HTTP报文
HTTP报文 HTTP:互联网的信使 HTTP报文:信使用来搬东西的包裹 1.报文流 HTTP报文:HTTP应用程序之间发送的数据块 组成:元信息开头(文本形式,描述报文的内容和含义)+可选的数据部分 ...
- redis的其他命令
1.del del key-name 用于删除已存在的键.不存在的 key 会被忽略 返回值:被删除 key 的数量 2.DUMP DUMP key-name 用于序列化给定 key ,并返回被序列化 ...
- [转]油猴Tampermonkey-让百度云下载飞起来
1. 简介 Tampermonkey,油猴脚本是一款免费的浏览器扩展程序. 我们这里用于Chrome浏览器,目的是为了让百度云里面的文件以满速下载,VIP还得出钱呢. 2. 安装 安装Lantern蓝 ...
- Unity3D的按钮添加事件有三种方式
为Unity3D的按钮添加事件有三种方式,假设我们场景中有一个Canvas对象,Canvas对象中有一个Button对象. 方式一: 创建脚本ClickObject.cs,然后将脚本添加到Canvas ...
- Java单例模式的应用
单例模式用于保证在程序的运行期间某个类有且仅有一个实例.其优势在于尽可能解决系统资源.通过修改构造方法的访问权限就可以实现单例模式. 代码如下: public class Emperor { priv ...
- Android学习——在Android中使用OpenCV的第一个程序
刚開始学习Android,因为之前比較熟悉OpenCV,于是就想先在Android上执行OpenCV试试 =============================================== ...
- hwi-web安装
hwi是hive的简单简单web端 安装hwi之前需要下载apache-hive-2.1.1-src,将hwi/web的打成hive-hwi-2.1.1.war.安装配置apache-ant-1.10 ...
- java.util.concurrent.RejectedExecutionException 线程池饱和
java.util.concurrent.RejectedExecutionException at java.util.concurrent.ThreadPoolExecutor$AbortPoli ...