【发布时间】:2016-12-22 14:04:14
【问题描述】:
我正在做一个练习,我在 Learn Python the Hard Way(ex 48) 中遇到过。目的是通过参考我们的词典对用户输入进行分组。我正在使用鼻子来测试我的脚本,但我得到了多个错误。当我进行鼻子测试时,我得到了 6 次失败中的 5 次。我不明白为什么我会收到这些错误。有什么帮助吗?
错误
FAIL: tests.ex48_tests.test_verbs
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\python\lib\site-packages\nose\case.py", line 198, in runTest
self.test(*self.arg)
File "C:\Python\projects\skeleton2\tests\ex48_tests.py", line 13, in test_verbs
assert_equal(scan("go").result, [('verb', 'go')])
AssertionError: <bound method scan.result of <ex48.lexicon.scan object at 0x03A8F3F0>> != [('verb', 'go')]
======================================================================
FAIL: tests.ex48_tests.test_stops
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\python\lib\site-packages\nose\case.py", line 198, in runTest
self.test(*self.arg)
File "C:\Python\projects\skeleton2\tests\ex48_tests.py", line 21, in test_stops
assert_equal(scan("the").result(), [('stop', 'the')])
AssertionError: Lists differ: [('stop', 'the'), ('error', 'the')] != [('stop', 'the')]
First list contains 1 additional elements.
First extra element 1:
('error', 'the')
- [('stop', 'the'), ('error', 'the')]
+ [('stop', 'the')]
======================================================================
FAIL: tests.ex48_tests.test_noun
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\python\lib\site-packages\nose\case.py", line 198, in runTest
self.test(*self.arg)
File "C:\Python\projects\skeleton2\tests\ex48_tests.py", line 29, in test_noun
assert_equal(scan("bear").result(), [('noun', 'bear')])
AssertionError: Lists differ: [('noun', 'bear'), ('error', 'bear')] != [('noun', 'bear')]
First list contains 1 additional elements.
First extra element 1:
('error', 'bear')
- [('noun', 'bear'), ('error', 'bear')]
+ [('noun', 'bear')]
======================================================================
FAIL: tests.ex48_tests.test_numbers
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\python\lib\site-packages\nose\case.py", line 198, in runTest
self.test(*self.arg)
File "C:\Python\projects\skeleton2\tests\ex48_tests.py", line 35, in test_numbers
assert_equal(scan("1234").result(), [('number', 1234)])
AssertionError: Lists differ: [('error', '1234')] != [('number', 1234)]
First differing element 0:
('error', '1234')
('number', 1234)
- [('error', '1234')]
? --- - -
+ [('number', 1234)]
? ++++
======================================================================
FAIL: tests.ex48_tests.test_errors
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\python\lib\site-packages\nose\case.py", line 198, in runTest
self.test(*self.arg)
File "C:\Python\projects\skeleton2\tests\ex48_tests.py", line 45, in test_errors
('noun', 'princess')])
AssertionError: Lists differ: [('no[20 chars]r', 'bear'), ('error', 'IAS'), ('noun', 'princ[24 chars]ss')] != [('no[20 chars]r', 'IAS'), ('noun', 'princess')]
First differing element 1:
('error', 'bear')
('error', 'IAS')
First list contains 2 additional elements.
First extra element 3:
('noun', 'princess')
+ [('noun', 'bear'), ('error', 'IAS'), ('noun', 'princess')]
- [('noun', 'bear'),
- ('error', 'bear'),
- ('error', 'IAS'),
- ('noun', 'princess'),
- ('error', 'princess')]
----------------------------------------------------------------------
Ran 6 tests in 0.027s
FAILED (failures=5)
lexicon.py
class scan(object):
dirs = ['north','south','east','west','down','up','left','right','back']
verbs = ['go','stop','kill','eat']
stops = ['the','in','of','from','at','it']
nouns = ['door','princess','bear','cabinet']
numbers = ['0','1','2','3','4','5','6','7','8','9']
def __init__(self, user_input):
self.user_input = user_input
def result(self):
words = self.user_input.split()
results = []
for item in words:
if item in scan.dirs:
result = ('direction', item.lower())
results.append(result)
if item in scan.verbs:
result = ('verb', item.lower())
results.append(result)
if item in scan.stops:
result = ('stop', item.lower())
results.append(result)
if item in scan.nouns:
result =('noun', item.lower())
results.append(result)
if item in scan.numbers:
result = ('number', int(item))
results.append(result)
if item not in (scan.dirs or scan.verbs or scan.stops or
scan.nouns or scan.numbers):
result = ('error', item)
results.append(result)
return results
lexicon_test.py
from nose.tools import *
from ex48.lexicon import scan
def test_direction():
assert_equal(scan('north').result(), [('direction', 'north')])
result = scan("north east south").result()
assert_equal(result, [('direction', 'north'),
('direction', 'east'),
('direction', 'south')])
def test_verbs():
assert_equal(scan("go").result, [('verb', 'go')])
result = scan("go kill eat").result()
assert_equal(result, [('verb', 'go'),
('verb', 'eat')
('verb', 'kill')])
def test_stops():
assert_equal(scan("the").result(), [('stop', 'the')])
result = scan("the in of").result()
assert_equal(result, [('stop', 'the'),
('stop', ' in'),
('stop', 'of')])
def test_noun():
assert_equal(scan("bear").result(), [('noun', 'bear')])
result = scan("bear princess").result()
assert_equal(result, [('noun', 'bear'),
('noun', 'princess')])
def test_numbers():
assert_equal(scan("1234").result(), [('number', 1234)])
result = scan("3 91234").result()
assert_equal(result, [('number', 3),
('number', 91234)])
def test_errors():
assert_equal(scan("ASDFADFASDF").result(), [('error', 'ASDFADFASDF')])
result = scan("bear IAS princess").result()
assert_equal(result, [('noun', 'bear'),
('error', 'IAS'),
('noun', 'princess')])
【问题讨论】:
-
在检查
if item in scan.<some list>之前你不想lower这个词吗? -
将帖子缩减为一个失败的问题并包含错误消息。总是提供一个最小失败的例子,单靠努力可能会帮助你找到你的错误。 stackoverflow.com/help/mcve
-
if item not in (scan.dirs or scan.verbs or scan.stops or scan.nouns or scan.numbers):没有做你想做的事。 -
如果您查看 test_error(),您会注意到 IAS 保留了它的 case 样式。所以不能降低check前的词主要是因为error category.@depperm
-
>>> obj = scan("piGs can Fly") >>> obj.result() [('error', 'piGs'), ('error', 'can'), ('error', 'Fly')] 当我在 IDLE 中运行代码时,我得到了正确的结果,但现在当我使用 nosetests @PM2Ring 运行时得到正确的结果
标签: python python-3.x unit-testing user-input nose