nose简单试水

1:下载

Link:https://pypi.python.org/packages/source/n/nose/nose-1.3.6.tar.gz#md5=0ca546d81ca8309080fc80cb389e7a16

2:安装

python setup.py install

如果是windows环境,nosetests应该是安装到了python的Script目录下,因此假如后面想直接调用必须得先将它添加到系统环境变量PATH当中;linux下我通过Cygwin实验了一把,默认是安装到了目录/usr/bin/下,应该是可以直接找到

LiHui@GodLike ~
$ which nosetests
/usr/bin/nosetests

3:运行

$ cat lihui.py
#!/usr/bin/env python

def test_foo1():
    a = 0
    assert a > 0

这个python脚本里面只有一个函数,而且没有调用,因此假如直接执行这个python脚本不会输出任何信息;但是函数里面assert的内容显然为假

运行nosetests进行测试

$ nosetests lihui.py
F
======================================================================
FAIL: lihui.test_foo1
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/nose-1.3.6-py2.7.egg/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/LiHui/lihui.py", line 5, in test_foo1
    assert a > 0
AssertionError

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

这里就将函数里assert里的错误输出来了;但是有一点要注意的,函数名必须以test,Test开头

$ cat lihui.py
#!/usr/bin/env python

def test_foo1():
    a = 0
    assert a > 0

def test_foo2():
    b = 1
    c = 2
    assert b == c

def foo3():
    d = 3
    assert d < 0

这里能输出错误信息的就会缺少foo3

FF
======================================================================
FAIL: lihui.test_foo1
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/nose-1.3.6-py2.7.egg/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/LiHui/lihui.py", line 5, in test_foo1
    assert a > 0
AssertionError

======================================================================
FAIL: lihui.test_foo2
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/nose-1.3.6-py2.7.egg/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/LiHui/lihui.py", line 10, in test_foo2
    assert b == c
AssertionError

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=2)

发表评论