python测试框架nose研究_python测试框架nose(转)

1.安装

先用easy_install 安装 nose,easy_install是一个很好的python工具,可以方便安装很多的python程序。可以去http://pypi.python.org/pypi/setuptools了解一下easy_install。如果懒得去看的,可以直接从这里下载安装文件进行安装就可以了,注意,这个链接是windows 32位的安装包。

安装完easy_install后,在相应版本的Scripts目录下(例如C:\Python26\Scripts)会有一个 easy_install.exe程序,通过这个就可以安装了。在命令行下转到Python的Scripts目录下,执行以下的命令进行安装:

C:\Python26\Scripts\easy_install nose

上面的 C:\Python26\Scripts 需要根据您的Python的安装路径进行修改。

我的机器安装结果如下:

D:\Python26\Scripts>easy_install nose

Searching for nose

Reading http://pypi.python.org/simple/nose/

Reading http://somethingaboutorange.com/mrl/projects/nose/

Best match: nose 1.0.0

Downloading http://somethingaboutorange.com/mrl/projects/nose/nose-1.0.0.tar.gz

Processing nose-1.0.0.tar.gz

Running nose-1.0.0\setup.py -q bdist_egg --dist-dir c:\docume~1\kortide\locals~1\temp\easy_install-kev5en\nose-1.0.0\egg-dist-tmp-idohk_

no previously-included directories found matching 'doc\.build'

Adding nose 1.0.0 to easy-install.pth file

Installing nosetests-2.6-script.py script. to D:\Python26\Scripts

Installing nosetests-2.6.exe script. to D:\Python26\Scripts

Installing nosetests-2.6.exe.manifest script. to D:\Python26\Scripts

Installing nosetests-script.py script. to D:\Python26\Scripts

Installing nosetests.exe script. to D:\Python26\Scripts

Installing nosetests.exe.manifest script. to D:\Python26\Scripts

Installed d:\python26\lib\site-packages\nose-1.0.0-py2.6.egg

Processing dependencies for nose

Finished processing dependencies for nose

D:\Python26\Scripts>nosetests.exe

----------------------------------------------------------------------

Ran 0 tests in 0.000s

OK

安完毕后,在C:\Python26\Scripts下会有一个nosetests.exe文件,通过这个exe程序就可以在命令行下运行测试了。最好是把C:\Python26\Scripts加入环境变量,这样在其它目录中可以直接引用nosetests.exe。

二、运行测试

在命令行下,直接运行nosetests(注意要把nosetests.exe所在的目录加入到环境变量Path里面),它就会自动查找当前目录下包含"Test"字符串的目录和文件进行测试。例如如下的目录结构:

(图1)

如果我们在C:\PythonTestRoot下运行nosetests,那么nose会自动找到TestUnit1文件夹,然后在找到 Test2.py和TestSomething.py两个文件进行测试,我的两个文件中各有两个方法,一共有四个测试:

(图2)

这样我们可以把所有测试放在一起,然后让测试自己去运行,我们最后看结果就可以了。我们可以指定具体如何输出结果,也可以指定如何搜索文件和文件夹,还可以把测试结果输入到指定的文件,在nosetests后面加上一些命令行参数就可以,参数如何设置在最后面介绍。

三、编写测试

上面说的运行测试其实非常简单,写好测试放到一个文件夹里面,然后在这个文件夹里面执行nosetests就可以了。编写测试也非常简单:

a)简单的测试

=======================

#### file: test.py ####

=======================

1 defTestfunc():2 a=13 b=24 asserta==b

把上面的文件保存到一个目录下,然后在该目录下在命令行里执行nosetests就可以运行测试了。

b)模块的setUp和tearDown[记住:一定是模块中的setup,teardown,不是类中的]

defsetUp():print"function setup"deftearDown():print"function teardown"defTestfunc1():print"Testfunc1"assertTruedefTestfunc2():print"Testfunc2"assertTrue

nose在文件中如果找到函数setup, setup_module, setUp 或者setUpModule等,那么会在该模块的所有测试执行之前执行该函数。如果找到函数 teardown,tearDown, teardown_module或者 tearDownModule 等,那么会在该模块所有的测试执行完之后执行该函数。

对于上面的代码,nose实际的执行过程是这样的:

setUp()->Testfunc1()->Testfunc2()->tearDown()

c)测试函数的setUp和tearDown

可能会想给每个函数单独指定类似的setUp和tearDown函数,可以如下处理:

1 defsetUp():2 print"function setup"3 4 deftearDown():5 print"function teardown"6 7 deffunc1Start():8 print"func1 start"9 10 deffunc1End():11 print"func1 end"12 13 deffunc2Start():14 print"func2 start"15 16 deffunc2End():17 print"func2 end"18 19 defTestfunc1():20 print"Testfunc1"21 assertTrue22 23 defTestfunc2():24 print"Testfunc2"25 assertTrue26 27 Testfunc1.setup=func1Start28 Testfunc1.tearDown=func1End29 Testfunc2.setup=func2Start30 Testfunc2.tearDown=func2End

注意最后面的四行,分别指定了Testfunc1和Testfun2的setup和teardown函数。

nose对上面代码的具体执行顺序如下:

setUp()->func1Start()->Testfunc1()->func1End()->func2Start()->Testfunc2()->func2End()->tearDown()

上面的代码也可以换一种写法如下,注意记得 import with_setup:

1 fromnose.toolsimportwith_setup2 3 defsetUp():4 print"function setup"5 6 deftearDown():7 print"function teardown"8 9 10 deffunc1Start():11 print"func1 start"12 13 deffunc1End():14 print"func1 end"15 16 deffunc2Start():17 print"func2 start"18 19 deffunc2End():20 print"func2 end"21 22 23 @with_setup(func1Start, func1End)24 defTestfunc1():25 print"Testfunc1"26 assertTrue27 28 @with_setup(func2Start, func2End)29 defTestfunc2():30 print"Testfunc2"31 assertTrue

d)测试类的的setUp和tearDown

看如下的代码:

1 classTestClass():2 arr1=23 arr2=24 5 defsetUp(self):6 self.arr1=17 self.arr2=38 print"MyTestClass setup"9 10 deftearDown(self):11 print"MyTestClass teardown"12 13 defTestfunc1(self):14 assertself.arr1==self.arr215 16 defTestfunc2(self):17 assertself.arr1==2

这里nose会对每个类的测试方法单独创建类的实例,并且有单独的setUp和tearDown。nose对上面测试的顺序如下:

setUp()->Testfunc1()->TearDown()->setUp()->Testfunc2()->TearDown()

e)package的setUp和tearDown

package的setUp和tearDown方法需要放在__init__.py这个文件中,整个package只执行一次setUp和一次tearDown。

四、nosetest常用的命令行参数

这里只重点介绍几个常用的,其它的参数可以通过nosetests -h进行查看。

a) -w ,指定一个目录运行测试。目录可以是相对路径或绝对路径。

例如:nosetest -w c:\pythonTests\Test1,只运行目录c:\pythonTests\Test1下的测试。可以指定多个目录,例如:nosetest -w c:\pythonTests\Test1-w c:\pythonTests\Test2。

b)-s,不捕获输出,会让你的程序里面的一些命令行上的输出显示出来。例如print所输出的内容。

c)-v,查看nose的运行信息和调试信息。例如会给出当前正在运行哪个测试。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值