Python的学习(十八)---- 单元测试工具nose

来源:互联网 发布:朴素贝叶斯算法例子 编辑:程序博客网 时间:2024/05/23 02:05

转自http://blog.csdn.net/zbyufei/article/details/6574213

一、安装nose

  先用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的安装路径进行修改。
 
  安完毕后,在C:/Python26/Scripts下会有一个nosetests.exe文件,通过这个exe程序就可以在命令行下运行测试了。最好是把C:/Python26/Scripts加入环境变量,这样在其它目录中可以直接引用nosetests.exe。

二、运行测试
  在命令行下,直接运行nosetests(注意要把nosetests.exe所在的目录加入到环境变量Path里面),它就会自动查找当前目录下包含"Test"字符串的目录和文件进行测试
这样我们可以把所有测试case放在一起,然后让测试自己去运行,我们最后看结果就可以了。我们可以指定具体如何输出结果,也可以指定如何搜索文件和文件夹,还可以把测试结果输入到指定的文件。

三、编写测试
a)简单的测试
=======================#### file: test.py ####======================= def Testfunc():     a = 1     b = 2     assert a == b
  把上面的文件保存到一个目录下,然后在该目录下在命令行里执行nosetests就可以运行测试了。
b)模块的setUp和tearDown
def setUp():    print "function setup"def tearDown():    print "function teardown"    def Testfunc1():    print "Testfunc1"    assert Truedef Testfunc2():    print "Testfunc2"    assert True

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

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

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

c)测试函数的setUp和tearDown

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

  def setUp():      print "function setup"   def tearDown():     print "function teardown"  def func1Start():     print "func1 start"  def func1End():     print "func1 end"  def func2Start():     print "func2 start"  def func2End():     print "func2 end"     def Testfunc1():     print "Testfunc1"     assert True  def Testfunc2():     print "Testfunc2"     assert True  Testfunc1.setup = func1Start Testfunc1.tearDown = func1End Testfunc2.setup = func2Start Testfunc2.tearDown = func2End
  注意最后面的四行,分别指定了Testfunc1和Testfun2的setup和teardown函数。

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

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

  d)测试类的的setUp和tearDown

  看如下的代码:

 

 class TestClass():     arr1 = 2     arr2 = 2         def setUp(self):         self.arr1 = 1         self.arr2 = 3         print "MyTestClass setup"      def tearDown(self):         print "MyTestClass teardown"             def Testfunc1(self):         assert self.arr1 == self.arr2         def Testfunc2(self):         assert self.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的运行信息和调试信息。例如会给出当前正在运行哪个测试。



原创粉丝点击