1.支持多个测试文件执行
nosetests [options] [(optional) test filesor directories]
nosetests 文件夹名(直接查找该文件夹里面以test开头的文件运行)
2.带ID输出
nosetests test_a.py -v --with-id
带id结果输出:
#1 test_a.test ... ok
#2 test_a.test1 ... ok
#3 test_a.test4 ... ERROR
不带ID的输出:
test_a.test ... ok
test_a.test1 ... ok
test_a.test4 ... ERROR
nosetests test_a.py -v --with-id 2(指定某一个或几个ID运行:)
输出:
#2 test_a.test1 ... ok
nosetests test_a.py -v --with-id 2 3(指定某一个或几个ID运行:)
输出:
#2 test_a.test1 ... ok
#3 test_a.test4 ... ERROR
3.运行失败的(加—failed参数,运行上次失败的)
例如运行 nosetests test_a.py -v第一次运行输出:
test_a.test ... ok
test_a.test1 ... ok
test_a.test4 ... ERROR
第二次运行 nosetests test_a.py -v —-failed输出:
test_a.test4 ... ERROR
4.结果输出到XML格式
nosetests test_a.py -v --with-xunit
输出生成的XML文件路径,例如:XML: /Users/xxx/Desktop/tt/nosetests.xml
5.快速收集测试用例的名字但不执行
nosetests test_a.py -v --collect-only
输出:
test_a.test ... ok
test_a.test1 ... ok
test_a.test4 ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.001s
看结果是执行的,其实没执行,对比上边test4是ERROR的
6.nose.tools的使用
1)@timed(3)(超时的用法)
代码:
import time
from nose.tools import timed
@timed(2)
def test():
time.sleep(3)
print “test”
运行结果失败,超时
2)@eq_(a,b, msg=None)
传入两个参数,a,b,如果ab相等则执行,不等则抛出异常,并打出msg
3)@ok(expo,msg=None)
与2)相同,只是直接传入表达式True or False
4)@raises(*exception)
如果存在异常,则标记成功(不管assert是否为true),不存在则失败
例如:
@raises(Exception)
def test_1():
assert 1==2
运行成功,
@raises(Exception)
def test_1():
assert 1==1
运行失败
5)@nottest
标记不执行该测试用例
6)@istest
标记为测试用例(可用于开头不为test*的方法)