我目前正在做Zed A. Shaw的“学习Python困难之路”,而且我在使用assert_raises时遇到了很多麻烦。这是我在测试文件中使用的代码:
def test_parseVerb():
assert_raises("ParserError",parser.parse_verb,[('stop', 'the'),
('noun', 'bear')])
这是PowerShell给我的错误:
======================================================================
ERROR: tests.parser_tests.test_parseVerb
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\MrnMicro\Python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "C:\Documents and Settings\sthma2\Documents\LPTHW\Projects\ex48\tests\parser_tests.py", line 37, in test_parseVer
b
assert_raises("ParserError",parser.parse_verb,fail_list)
File "C:\MrnMicro\Python27\lib\unittest\case.py", line 476, in assertRaises
callableObj(*args, **kwargs)
File "C:\MrnMicro\Python27\lib\unittest\case.py", line 117, in __exit__
if not issubclass(exc_type, self.expected):
TypeError: issubclass() arg 2 must be a class or tuple of classes
----------------------------------------------------------------------
Ran 10 tests in 0.016s
FAILED (errors=1)
我不知道说实话是什么,如果有人能提供帮助,我将不胜感激!
谢谢!
修改的
def parse_verb(word_list):
skip(word_list, 'stop')
if peek(word_list) == 'verb':
return match(word_list, 'verb')
else:
raise ParserError("Expected a verb next.")
答案 0 :(得分:4)
您需要提供异常的实际类作为第一个参数,而不是包含名称的字符串,例如
assert_raises(ZeroDivisionError, operator.div, 1, 0)
文档实际上在标准模块unittest中,nose将名称改为pep8。