刚才查了,它应该是nose测试框架中的一个测试用例的书写办法。如果没有文档,就看它的源代码。我刚刚下载了nose。
在1.0版本里找到这样一句话。
def raises(*exceptions):
"""Test must raise one of expected exceptions to pass.
.....
If you want to test many assertions about exceptions in a single test,
you may want to use `assert_raises` instead.
"""
从含义上看,应该是确保一定要发生异常。而且要用在大量使用assert语言产生异常的条件下。
下面一段更说明问题。
def assert_raises(exception, callable, *args, **kw):还比如这样子。
import math
import py.test
py.test.raises(OverflowError, math.log, 0)
py.test.raises(ValueError, math.sqrt, -1)
py.test.raises(ZeroDivisionError, "1 / 0")
import nose.tools
nose.tools.assert_raises(OverflowError, math.log, 0)
nose.tools.assert_raises(ValueError, math.sqrt, -1)
我刚学python没多久,还是不太懂哎,
nose.tools.assert_raises(OverflowError, math.log, 0)
nose.tools.assert_raises(ValueError, math.sqrt, -1)
这个是什么意思?能举例说明一下么?谢谢!
nose.tools.assert_raises(OverflowError, math.log, 0) 这一句话就是说,函数math.log(0)确保它一定会出现OverflowError这个异常
nose.tools.assert_raises(ValueError, math.sqrt, -1)
这句话是说测试用例要确保math.sqrt(-1)弹出异常ValueError,否则就是测试不通过。
额,这个的意思是不是说 assert_raises(A,B,C) ,A是我定义的错误,B用来在参数C的条件下引发这个错误,C可以是很多参数,是这个意思吗?
追答对。是这个意思。不过,这个方法,仅仅适用于异常测试。通常不用这样。直接用assert就可以了。
追问好的,真是万分感谢!