这几天都有遇到nosetest,一是hardway learn python中,还有一个是django中,因此想趁此了解下有什么用,看了半天也不理解具体的用处,还是老规矩把尝试的一些内容记下来。之所以叫感知,可以发现真的是没有理解,关键是中间的执行过程和执行后的作用。

官方文档:http://nose.readthedocs.org/en/latest/usage.html

网上找了一个比较好点的例子,比较好理解点,不像IBM网站上的类的例子,那个看起来太绕了;

关于摄氏与华氏的转换函数:

temperature.py

def to_celsius(t):
    return (t - 32.0) * 5.0 / 9.0

def above_freezing(t):
    return t > 0
接下来是测试脚本1,主要作用:导入了nose和temperature模块,写了两个测试函数(感觉没有动作),最后是运行模块。

test_temperature.py

import nose
import temperature

def test_to_celsius():
    '''Test function for to_celsius'''
    pass

def test_above_freezing():
    '''Test function for above_freezing.'''
    pass

if __name__ == '__main__':
    nose.runmodule()
接下来是测试脚本2,导入了nose和celsius函数,然后定义了3个不同的值进行执行判断。
test_celsius.py

import nose
from temperature import to_celsius

def test_freezing():
    assert to_celsius(32) == 0

def test_boiling():
    assert to_celsius(212) == 100

def test_roundoff():
    assert to_celsius(100) == 38

if __name__ == '__main__':
    nose.runmodule()
接下来是测试脚本3,导入了nose和freezing函数,然后定义了3个不同的值进行执行判断。
test_freezing.py

import nose
from temperature import above_freezing

def 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.'

if __name__ == '__main__':
    nose.runmodule()

其中运行结果如下:

先运行一遍temperature.py,然后运行test.temperature.py,如下:

root@LOonux:/home/loongson/python/nose# ls
nose_test.py    test_1.py        test_freezing.py
temperature.py  test_celsius.py  test_temperature.py
root@LOonux:/home/loongson/python/nose# python temperature.py 
root@LOonux:/home/loongson/python/nose# nosetests -v test_temperature.py 
Test function for to_celsius ... ok
Test function for above_freezing. ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.006s

OK
root@LOonux:/home/loongson/python/nose# 
这两个ok只能说明都执行了吧?不知还能说明什么。接下来测试celsius

root@LOonux:/home/loongson/python/nose# nosetests -v test_celsius.py 
test_celsius.test_freezing ... ok
test_celsius.test_boiling ... ok
test_celsius.test_roundoff ... FAIL

======================================================================
FAIL: test_celsius.test_roundoff
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/nose-1.3.0-py2.6.egg/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/loongson/python/nose/test_celsius.py", line 13, in test_roundoff
    assert to_celsius(100) == 38
AssertionError

----------------------------------------------------------------------
Ran 3 tests in 0.010s

FAILED (failures=1)
root@LOonux:/home/loongson/python/nose# 
这里有一个报错,主要是小数点的问题,

>>> t =100
>>> (t - 32.0) * 5.0 / 9.0
37.77777777777778
不相等,当然报错了。

接下来测试freezing,

root@LOonux:/home/loongson/python/nose# nosetests -v test_freezing.py 
test_freezing.test_above_freezing ... ok

----------------------------------------------------------------------
Ran 1 test in 0.006s

OK
root@LOonux:/home/loongson/python/nose# 

这个就一个ok,后两个因为都小于等于0,所以感觉在temperature.py中没有返回值,到这里结果就不执行了一样。

就是这个样子,真心没搞懂有什么意义,主要其它自动化测试框架也没有接触过。感觉写这个的时间比我自己测试的代价大多了,而且根本就没有测试全面。

如果你知道,请指导下!


暂时到此,以后理解了来补充。

11-18

常用命令行参数:

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

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



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



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


posted on 2022-07-05 18:12  我在全球村  阅读(55)  评论(0编辑  收藏  举报