Crossin的编程教室
标题: 【提问】Python执行nosetests测试时,出现AssertionError和TypeError [打印本页]
作者: Electro01 时间: 2017-8-28 23:59
标题: 【提问】Python执行nosetests测试时,出现AssertionError和TypeError
- class ParserError(Exception):
- pass
- class Sentence(object):
- def __init__(self, subject, verb, object):
- self.subject = subject [1]
- self.verb = verb[1]
- self.object = object[1]
- def peek(word_list):
- if word_list:
- word = word_list[0]
- return word[0]
- else:
- return None
- def match(word_list, expecting):
- if word_list:
- word = word_list.pop(0)
- if word[0] == expecting:
- return word
- else:
- return None
- else:
- return None
- def skip (word_list, word_type):
- while peek(word_list) == word_type:
- match(word_list, word_type)
- 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.")
- def parse_object(word_list):
- skip(word_list,'stop')
- next = peek(word_list)
- if next == 'noun':
- return match(word_list, 'noun')
- if next == 'direction':
- return match(word_list, 'direction')
- else:
- raise ParserError('Expected a noun or direction next.')
- def parse_subject(word_list, subj):
- verb = parse_verb(word_list)
- obj = parse_object(word_list)
- return Sentence(subj, verb, obj) # 执行 class Sentence
- def parse_sentence(word_list):
- skip(word_list, 'stop')
- start = peek(word_list)
- if start == 'noun':
- subj = match(word_list, 'noun')
- return parse_sentence(word_list, subj)
- elif start == 'verb':
- # assume the subject is the player then
- return parse_subject(word_list, ('noun', 'player'))
- else:
- raise ParserError("Must start with subject, object, or verb not: %s" % start)
复制代码 以上是需要测试的代码。
这是我写的测试代码,其中两部分错误较相似:- word_listA = [('verb','elsker'),('direction','ikke'),('noun','Jag')]
- def test_sentence():
- sentence = Sentence([('noun','Jag'), ('verb', 'elsker'), ('direction', 'ikke')])
- assert_equal(sentence.subject, 'Jag')
复制代码- def test_parse_sentence():
- result = parse_sentence([('verb','elsker'),('noun','Jag')])
- assert_equal(result.subject, 'player')
- assert_equal(result.verb, 'elsker')
- assert_equal(result.object, 'Jag')
- result1 = parse_sentence([('noun','Jag'), ('verb','elsker'),('direction','ikke')])
- assert_equal(result1.subject, 'Jag')
- assert_equal(result1.verb, 'elsker')
- assert_equal(result1.object, 'ikke')
- assert_raises(ParserError, parse_object,[('stop','in'),('stop','the')])
复制代码 执行nosetests后,报错:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/admin/mystuff/test/ex49_tests.py", line 6, in test_sentence
sentence = Sentence([('noun','Jag'), ('verb', 'elsker'), ('direction', 'ikke')])
TypeError: __init__() takes exactly 4 arguments (2 given)
======================================================================
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/admin/mystuff/test/ex49_tests.py", line 68, in test_parse_sentence
result1 = parse_sentence([('noun','Jag'), ('verb','elsker'),('direction','ikke')])
File "/Users/admin/mystuff/test/ex49.py", line 70, in parse_sentence
TypeError: parse_sentence() takes exactly 1 argument (2 given)
这是另一部分及其AssertionError报错:- def test_skip():
- skipA = skip([('noun','Jag'), ('verb', 'elsker')], 'verb')
- assert_equal(skipA, ('verb', 'elsker'))
- assert_equal(skipA, None)
复制代码Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/admin/mystuff/test/ex49_tests.py", line 27, in test_skip
assert_equal(skipA, ('verb', 'elsker'))
AssertionError: None != ('verb', 'elsker')
作者: crossin先生 时间: 2017-8-29 14:14
报错的内容不是都输出了吗,到标准的行那里去检查
一个是你 __init__ 参数,也就是创建对象时候给的参数不对
一个是 skipA 的值是 None
作者: Electro01 时间: 2017-8-29 18:42
crossin先生 发表于 2017-8-29 14:14
报错的内容不是都输出了吗,到标准的行那里去检查
一个是你 __init__ 参数,也就是创建对象时候给的参数不 ...
有点不明白她书中给出的expecting具体是指的什么- def match(word_list, expecting):
- # 用pop( )的方法删除这个元素,并且它还返回这个元素
- if word_list:
- word = word_list.pop(0)
- if word[0] == expecting:
- return word
- else:
- return None
- else:
- return None
复制代码 我给进去的list[('verb', 'taste'),('direction','good'),('noun','pizza')]第一个就是verb并且等于word_type :'verb'
按理说应该是匹配的,然而skipA的值是None也就是说('verb', 'taste') != expecting 咯?- Traceback (most recent call last):
- File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
- self.test(*self.arg)
- File "/Users/admin/mystuff/test/ex49_tests.py", line 60, in test_parse_subject
- result = parse_subject([('verb', 'taste'), ('direction', 'good'),('noun','pizza')])
- TypeError: parse_subject() takes exactly 2 arguments (1 given)
- ======================================================================
- ERROR: ex49_tests.test_parse_sentence
- ----------------------------------------------------------------------
- Traceback (most recent call last):
- File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
- self.test(*self.arg)
- File "/Users/admin/mystuff/test/ex49_tests.py", line 77, in test_parse_sentence
- result1 = parse_sentence([('noun','pizza'),('verb','taste'),('direction','good')])
- File "/Users/admin/mystuff/test/ex49.py", line 74, in parse_sentence
- return parse_sentence(word_list, subj)
- TypeError: parse_sentence() takes exactly 1 argument (2 given)
复制代码 还有一个没有搞明白的错误是:- Traceback (most recent call last):
- File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
- self.test(*self.arg)
- File "/Users/admin/mystuff/test/ex49_tests.py", line 65, in test_parse_subject
- result1 = parse_subject([('direction', 'good') ,('noun','pizza'),('verb', 'taste')],('verb', 'taste'))
- File "/Users/admin/mystuff/test/ex49.py", line 62, in parse_subject
- verb = parse_verb(word_list)
- File "/Users/admin/mystuff/test/ex49.py", line 46, in parse_verb
- raise ParserError("Expected a verb next.")
- ParserError: Expected a verb next.
复制代码 在parse_subject中:- def parse_subject(word_list, subj):
- verb = parse_verb(word_list)
- obj = parse_object(word_list)
- return Sentence(subj, verb, obj)
复制代码 verb里面要求传入的list第一个是verb, 而obj中要求第一个传入的是noun或者direction
所以在测试代码中:- def test_parse_subject():
- result = parse_subject([('verb', 'taste'), ('direction', 'good'),('noun','pizza')],('noun','pizza'))
- assert_equal(result.subject, 'pizza')
- assert_equal(result.verb, 'taste')
- assert_equal(result.object, 'good')
- result1 = parse_subject([ ('direction', 'good'),('noun','pizza'),('verb', 'taste')],('none', 'pizza'))
- assert_equal(result.subject, 'pizza')
- assert_equal(result.verb, 'taste')
- assert_equal(result.object, 'good')
复制代码 result中第一个传入的是verb,为了测试 verb = parse_verb(word_list) 所以没有报错
result1中第一个传入的direction 为了测试 obj = parse_object(word_list) 但是报错了- Traceback (most recent call last):
- File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
- self.test(*self.arg)
- File "/Users/admin/mystuff/test/ex49_tests.py", line 65, in test_parse_subject
- result1 = parse_subject([('direction', 'good'),('verb', 'taste'),('noun','pizza')],('noun','pizza'))
- File "/Users/admin/mystuff/test/ex49.py", line 62, in parse_subject
- verb = parse_verb(word_list)
- File "/Users/admin/mystuff/test/ex49.py", line 46, in parse_verb
- raise ParserError("Expected a verb next.")
- ParserError: Expected a verb next.
复制代码 如果我将传入的list中第一个改为verb,即[('verb', 'taste'), ('direction', 'good'),('noun','pizza')],('noun','pizza')
它就不会报错了,请问这是为什么呢?(即使obj中同样要求第一个传入的是noun或者direction)
然后关于最后一个报错:- Traceback (most recent call last):
- File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
- self.test(*self.arg)
- File "/Users/admin/mystuff/test/ex49_tests.py", line 77, in test_parse_sentence
- result1 = parse_sentence([('noun','pizza'),('verb','taste'),('direction','good')])
- File "/Users/admin/mystuff/test/ex49.py", line 74, in parse_sentence
- return parse_sentence(word_list, subj)
- TypeError: parse_sentence() takes exactly 1 argument (2 given)
复制代码 parse_sentence() 中只能传入一个参数,而我的测试代码中 result1 = parse_sentence([('noun','pizza'),('verb','taste'),('direction','good')])只有一个list,并且第一个为('noun','pizza'),符合- if start == 'noun':
- subj = match(word_list, 'noun')
- return parse_sentence(word_list, subj)
复制代码 但是它还是显示的2 given
对于 result = parse_sentence([('verb','taste'),('noun','pizza'),('direction','good')]),我传入的也是的一个list,只是开头第一个是verb符合-
- elif start == 'verb':
- # assume the subject is the player then
- return parse_subject(word_list, ('noun', 'player'))
复制代码 如果删除result1,只对result1进行测试就没有报错了。请问result1中的TypeError: parse_sentence() takes exactly 1 argument (2 given)究竟是哪里出错了呢?
作者: crossin先生 时间: 2017-8-29 23:19
请先看看教程里关于函数的参数传递。你对函数传递理解有问题,以你最后一个为例:- return parse_subject(word_list, ('noun', 'player'))
复制代码 这个parse_subject 传了两个参数进去,报错说你 parse_sentence() takes exactly 1 argument (2 given) 这应该很明白吧
要不然就是你没看明白报错在说什么,仔细对照下报错给你的行数。
先把这个问题导致的错误都解决掉,剩下的还有想不通的我们再看。别一贴就是一整屏的代码,这个给人压力有点大,别人也没信心帮你看了
作者: Electro01 时间: 2017-8-30 00:19
crossin先生 发表于 2017-8-29 23:19
请先看看教程里关于函数的参数传递。你对函数传递理解有问题,以你最后一个为例:这个parse_subject 传了两 ...
嗯,好的。谢谢啦,TypeError这个问题已经解决了
但是就是不明白给进去的是list[('verb', 'taste'),('direction','good'),('noun','pizza')]第一个的word_type就是‘verb’并且与其word_type - 'verb'相同
按理说应该是匹配的,然而skipA的值是None
这个地方我没有想明白
作者: crossin先生 时间: 2017-8-30 09:43
Electro01 发表于 2017-8-30 00:19
嗯,好的。谢谢啦,TypeError这个问题已经解决了
但是就是不明白给进去的是list[('verb', 'taste'),('dir ...
skip 这个函数你本身就没有给任何返回值啊
还是函数相关的概念
另外多输出,一步步查看结果
作者: Electro01 时间: 2017-8-30 12:40
crossin先生 发表于 2017-8-30 09:43
skip 这个函数你本身就没有给任何返回值啊
还是函数相关的概念
好哒 明白了 谢谢~
欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) |
Powered by Discuz! X2.5 |