1

我目前正在做 Zed A. Shaw 的“Learn Python the Hard Way”,我在使用 assert_raises 的练习 49 上遇到了很多麻烦。这是我在测试文件中使用的代码:

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.")
4

1 回答 1

4

您需要提供异常的实际类作为第一个参数,而不是包含名称的字符串,例如

assert_raises(ZeroDivisionError, operator.div,  1, 0)

文档实际上在标准模块unittest中,nose 将名称改编为 pep8。

于 2013-05-28T21:01:48.523 回答