Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4450586
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: Python/Ruby

2015-08-27 21:27:14

    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()


阅读(873) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~