Chinaunix首页 | 论坛 | 博客
  • 博客访问: 703367
  • 博文数量: 102
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 1748
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-23 15:42
个人简介

寻找严肃、沉默和专注的力量。

文章分类

全部博文(102)

文章存档

2015年(26)

2014年(8)

2013年(68)

分类: Python/Ruby

2013-06-06 17:05:24

    Nose是最流行的针对Python的测试库之一(可以从 免费下载)。
一、待测试的模块为:temperature.py

  1. def to_celsius(t):
  2.       return (t-32.0)*5.0/9.0
  3. def above_freezing(t):
  4.      return t>0


二、新建一个名为test_temperature.py的文件(必须以test_开头),当运行Nose时,它会自动寻找其名称以"test_"开头的文件。
     跟测试模块的名称一样,测试函数的名称也必须以test_开头。

三、测试模块test_temperature.py的框架为:

  1. import nose
  2.      import temperature


  3.     def test_to_celsius():
  4.           '''Test function for to_celsius'''
  5.           pass
  6.     def test_above_freezing():
  7.          '''Test function for above_freezing.'''
  8.          pass
  9.    if __name__ == '__main__':
  10.         nose.runmodule()


   四、测试temperature模块的to_celsius()函数

  1. import nose
  2. from temperature import to_celsius
  3. def test_freezing():
  4.     assert to_celsius(32) ==0
  5. def test_boiling():
  6.     assert to_celsius(212) ==100
  7. def test_roundoff():
  8.     assert to_celsius(100) == 38
  9. if __name__ == '__main__':
  10.     nose.runmodule()


五、测试temperature模块的above_freezing()函数:
  
  1. import nose
  2. frome temperature import above_freezing
  3. def test_above_freezing():
  4.     assert above_freezing(89.4),'A temperature above freezing.'
  5.     assert not above_freezing(-42),'A temperature below freezing.'
  6.     assert not above_freezing(0),'A temperature at freezing.'
  7. if __name__ == '__main__':
  8.    nose.runmodule()


阅读(9977) | 评论(0) | 转发(1) |
0

上一篇:C++类大小的一些说明

下一篇:perl读写Excel

给主人留下些什么吧!~~