初试nose

来源:互联网 发布:淘宝3d打印价格 编辑:程序博客网 时间:2024/04/29 03:53

        阅读《Practical Programming -An Introduction to Computer Science Using Python》一书时,在第4章提到了使用Nose对Python代码进行测试,因而小试牛刀了一下。

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

安装

        正常情况下,安装步骤如下:

  • 首先,下载:setuptools-->安装   
  • 其次,打开cmd-->进入命令提示行,此时保持电脑可以上外网,在命令提示行中输入 C:\Python27\Scripts\easy_install nose 回车,此时会从网上自动下载东西,不用管它,Nose会自动安装完成。
           注:这里的C:\Python27,是指python所在的文件夹,如果不同可以更改!

        

        但是,现在我这里有两个问题:

  1. 我安装的是python3.3,但是setuptools没有支持3.3的官方版本
  2. 待我安装了python2.7和setuptools-py2.7后,按照上述方法在线安装nose时,又一直卡在那“reading xxx”,半天没反应(难道是我网速原因?)
        没办法,只有去找别的安装nose的方法,于是找到了这篇博文,介绍的安装的方法不是通过setuptools在线安装,而是如下:
  • 先下载nose包,于是我下了一个支持python3.3的nose1.2.1
  • 把下载的在python安装路径\Lib\site-packages下加压,必须把加压目录下的‘nose'文件夹放在 \Lib\site-packages路径下
  • 点击setup
        按此步骤试了,不过setup后却提示安装error了,运行如下代码测试时果然有问题:
import nosefrom temperature import to_celsiusdef test_roundoff ():'''Test that roundoff works'''assert to_celsius(100) == 38, 'Returning an unrounded result' #not 37.77...if __name__ == "__main__":nose.runmodule()
        运行后提示:
Traceback (most recent call last):  File "test_temperature.py", line 29, in <module>AttributeError: 'module' object has no attribute 'runmodule'

        网上找了半天还是没找到啥问题,也是是nose就没安装上。没办法,只好从头再来,到了晚上用上面说的正常情况下的安装步骤进行安装,这次竟然成功了~ 不过是安装python2.7版本的nose。
---------------------------------------------------------------------------------------------------------

使用

        本次使用主要是用书上的例子进行的测试。测试文件的名字以“test”开始,Nose运行时,它会自动寻找以“test”开始的文件。每个Nose测试模块都应该包含以下内容:
  • 用于引入Nose及待测模块的语句
  • 实际用于测试我们模块的函数
  • 用于触发那些测试函数的函数的调用
        测试函数的名称也必须以“test”开头。
        待测模块temperature.py代码如下:
def to_celsius (t):return round ( (t-32.0)*5.0/9.0 )def above_freezing (t):return t>0
        
        测试模块test_temperature.py代码如下:

import nosefrom temperature import to_celsiusfrom temperature import above_freezingdef test_above_freezing ():'''Test above_freezing '''assert above_freezing(89.4), 'A temperature above freezing'assert not above_freezing(-42), 'A temperature below freezing'assert not above_freezing(0), 'A temperature at freezing'def test_boiling ():'''Test boiling point'''assert to_celsius(212) == 100def test_roundoff ():'''Test that roundoff works'''assert to_celsius(100) == 38, 'Returning an unrounded result'   #not 37.77...if __name__ == "__main__":nose.runmodule()
        
        上面这段代码执行之后,每个测试都会得出以下三种结果之一:
  • 通过(pass)。实际值跟期望值相符。
  • 失败(fail)。实际值跟期望值不相符。
  • 出错(error)。测试本身出了问题;也就是说,测试代码中含有bug。在这种情况下,测试将无法告诉我们任何有关被测系统的信息。
        上面的函数test_above_freezing中有3条assert语句,但Nose还是会认为这只是一个测试,这是因为,对Nose而言,每个函数就是一个测试,一个函数想要测试多少东西那是函数自己的事情。这样有个缺点就是当其中的某个断言失败时,python就会立即停止执行它所在的函数。也就是说,如果test_above_freezing中的第一个测试就失败了,我们将无法得到其他测试的任何信息。因此通常的做法是编写许多小的测试函数,分别只测试少量的东西,而不是在其中放上一大堆各种各样的断言。
---------------------------------------------------------------------------------------------------------

参考

1. 《Practical Programming -An Introduction to Computer Science Using Python
2.《Python 单元测试模块nose windows下》
3.《Python nose test framework 介绍》
4. 《Testing with nose》


原创粉丝点击