二、基本使用
nosetest脚本的使用(在安装完nose之后)
nosetests [options] [(optional) test files or directories]
我们可以使用配置文件,将需要运行的参数放入,配置文件的文件名为nose.cfg中,是标准的配置文件格式,如下:
[nosetests]verbosity=3with-doctest=1
有部分参数可以不使用或禁用,你可以将配置放入配置文件中,但必须在系统中建立一个环境变量,名字为 NOSE_IGNORE_CONFIG_FILES
除了上面的运行方式,还有以下的途径使用nose
在python脚本中直接使用,如:
import nosenose.main()
如果你不想使用unittest框架测试结束时这种显示方式,可以在脚本中使用以下方式:
import noseresult = nose.run()
说了这么多,可能还是没看明白,下面来几个实例,
在nose中,测试用例可以以函数和类的形式来组织,下面分别来演示下,
1、以函数形式写的测试用例:
#coding:utf-8'''Created on 2015年6月22日@author: '''import logginglog = logging.getLogger(__name__)def test_learn_3(): print("test_lean_3") passdef test_lean_4(): print("test_learn_2") def test_lean_5(): print("test_learn_5")def setUp(): print "0002 test setUp" def tearDown(): print "0002 test teardown" test_learn_3.teardown=test_lean_5.teardown= tearDown
运行的结果如下:
E:\workspace\nosetest_lear>nosetests -v -s test_case.test_case_00020002 test setUptest_case.test_case_0002.test_learn_3 ... test_lean_30002 test teardownoktest_case.test_case_0002.test_lean_4 ... test_learn_2oktest_case.test_case_0002.test_lean_5 ... test_learn_50002 test teardownok0002 test teardown
这里加了一句
test_learn_3.teardown=test_lean_5.teardown= tearDown,说明在运行完成后就执行tearDown操作
所以这里运行顺序为:setUp-->test_learn_3-->tearDown-->test_learn_3-->test_learn_4-->tearDown-->tearDown
因次,在函数或者函数式的用例组织中,setUp和tearDown只会执行一次,分别是开始及结束,注意:必须要用@classmethod装饰器标识
2、以类的形式组织的用例:
'''@author: huzq'''class TestClass(): def setUp(self): print "MyTestClass setup" def tearDown(self): print "MyTestClass teardown" def Testfunc1(self): print "this is Testfunc1" pass def test_func1(self): print "this is test_func1" pass def Testfunc2(self): print "this is Testfunc2" pass def test_func2(self): print "this is test_func2" pass
运行结果:
E:\workspace\nosetest_lear>nosetests -v -s test_case.test_case_0003test_case.test_case_0003.TestClass.Testfunc1 ... MyTestClass setupthis is Testfunc1MyTestClass teardownoktest_case.test_case_0003.TestClass.Testfunc2 ... MyTestClass setupthis is Testfunc2MyTestClass teardownoktest_case.test_case_0003.TestClass.test_func1 ... MyTestClass setupthis is test_func1MyTestClass teardownoktest_case.test_case_0003.TestClass.test_func2 ... MyTestClass setupthis is test_func2MyTestClass teardownok----------------------------------------------------------------------Ran 4 tests in 0.026s
从运行结果中可以看出,每个用例都会分别执行setUp及tearDown,但是与unittest一样,在类中的测试用例顺序变更了,由字母排序了。
注:在以类形式运行时,还有setUpClass及tearDownClass两个函数功能,是运行测试时,只运行一次。
3、以整个包的形式运行
我们创建python package时会自动生成__init__.py文件,我们将setUp或tearDown写入时,会在每个文件执行时都执行setUp和tearDown,如下
文件内容为:
def setUp(): print "ALL starting..."
然后再次执行以类的文件,结果如下:
E:\workspace\nosetest_lear>nosetests -v -s test_case.test_case_0003ALL starting...test_case.test_case_0003.TestClass.Testfunc1 ... MyTestClass setupthis is Testfunc1MyTestClass teardownoktest_case.test_case_0003.TestClass.Testfunc2 ... MyTestClass setupthis is Testfunc2MyTestClass teardownoktest_case.test_case_0003.TestClass.test_func1 ... MyTestClass setupthis is test_func1MyTestClass teardownoktest_case.test_case_0003.TestClass.test_func2 ... MyTestClass setupthis is test_func2MyTestClass teardownok----------------------------------------------------------------------Ran 4 tests in 0.026sOK
从结果中可以看出,在执行前执行了__init__.py中的setUp函数
with_setup修饰器的使用,nose支持在每个用例前使用with_setup来自定义测试用例的setUp及tearDown功能,如下:
def setup_func(): "set up test fixtures"def teardown_func(): "tear down test fixtures"@with_setup(setup_func, teardown_func)def test(): "test ..."
也可以单独定义setup或tearDown,写法就必须如下:
test.setup = setup_functest.teardown = teardown_func
4、多个文件时nose的执行
如下图,建立了多个py文件
再次运行大的文件,查看运行结果:
E:\workspace\nosetest_lear>nosetests -v test_casetest_case.test_case_0001.test_lean_2 ... oktest_case.test_case_0002.test_learn_3 ... oktest_case.test_case_0002.test_lean_4 ... oktest_case.test_case_0002.test_lean_5 ... oktest_case.test_case_0003.TestClass.Testfunc1 ... oktest_case.test_case_0003.TestClass.Testfunc2 ... oktest_case.test_case_0003.TestClass.test_func1 ... oktest_case.test_case_0003.TestClass.test_func2 ... ok----------------------------------------------------------------------Ran 8 tests in 0.022sOK
从结果中,可以看出,文件名是按相应的字母排序,但文件里的用例根据函数式及类式不同而不一样。
5、nose运行方式
如前文中提到的一样,nose可以直接放在脚本里面运行,但不建议这样方法,更建议的是通过命令行的方式,对以后的持续集成有好处。
直接运行整个包:
E:\workspace\nosetest_lear>nosetests -v test_casetest_case.test_case_0000.test_learn_3 ... oktest_case.test_case_0001.test_lean_2 ... oktest_case.test_case_0002.test_learn_3 ... oktest_case.test_case_0002.test_lean_4 ... oktest_case.test_case_0002.test_lean_5 ... oktest_case.test_case_0003.TestClass.Testfunc1 ... oktest_case.test_case_0003.TestClass.Testfunc2 ... oktest_case.test_case_0003.TestClass.test_func1 ... oktest_case.test_case_0003.TestClass.test_func2 ... ok----------------------------------------------------------------------Ran 9 tests in 0.020sOK
运行某一个模块:
E:\workspace\nosetest_lear>nosetests -v test_case.test_case_0002test_case.test_case_0002.test_learn_3 ... oktest_case.test_case_0002.test_lean_4 ... oktest_case.test_case_0002.test_lean_5 ... ok----------------------------------------------------------------------Ran 3 tests in 0.007sOK
运行某一个用例:
E:\workspace\nosetest_lear>nosetests -v test_case.test_case_0002:test_lean_4test_case.test_case_0002.test_lean_4 ... ok----------------------------------------------------------------------Ran 1 test in 0.003sOK
运行不同模块下不同用例:
E:\workspace\nosetest_lear>nosetests -v --tests=test_case.test_case_0002:test_lean_4,test_case.test_case_0001:test_lean_2test_case.test_case_0002.test_lean_4 ... oktest_case.test_case_0001.test_lean_2 ... ok----------------------------------------------------------------------Ran 2 tests in 0.004sOK