好的,问题来了:在Zed Shaw 的“Learn Python the Hard Way”的练习 49assert_raises()
中,我们需要使用. 这是我正在测试的功能,如果省略该assert_raises()
部分,它会通过测试:
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.")
这是测试功能和臭名昭著的assert_raises()
行:
def parse_verb_test():
vrb_list = [('of', 'stop'), ('from', 'stop'), ('go', 'verb'), ('north', 'direction')]
assert_equal(parse_verb(vrb_list), ('go', 'verb'))
assert_equal(vrb_list, [('north', 'direction')])
assert_raises(ParserError, parse_verb, vrb_list)
它给了我以下输出:
.........E...
======================================================================
ERROR: tests.parser_tests.parse_verb_test
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/vsevolod/Repositories/ex48/tests/parser_tests.py", line 20, in parse_verb_test
assert_raises(ParserError, parse_verb, vrb_list)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 471, in assertRaises
callableObj(*args, **kwargs)
File "/Users/vsevolod/Repositories/ex48/ex48/parser.py", line 40, in parse_verb
raise ParserError("Expected a verb next.")
TypeError: object.__new__() takes no parameters
----------------------------------------------------------------------
Ran 13 tests in 0.042s
FAILED (errors=1)
我还尝试摆脱这assert_raises()
条线并使用@raises(ParserError)
before 函数。它也不太有效,并产生了相同的输出。
像这样创建一个单独的测试有助于消除错误:
def parse_verb_fail():
fail_list = [('north', 'direction'), ('go', 'verb')]
assert_raises(ParserError, parse_verb, fail_list)
但现在测试被简单地忽略了。
我究竟做错了什么?