編程自己常見error(4)
15、關於寫測試文件時候的路經測試
在學習練習47的時候,我按照文中的測試代碼寫了小腳本以及測試文件,但是總是有錯誤,而且我還是沒有檢查出錯誤來,隔了兩天,我終於看出來了,主要就是經驗不足的小錯誤,但還是值得學一下。
錯誤代碼:(game.py)
class Room(object):
def __init__(self, name, description):
self.name = name
self.description = description
self.paths = []
def go(self, direction):
return self.paths.get(direction, None)
def add_paths(self, paths):
self.paths.update(paths)
測試文件(BLAH_test.py):
from nose.tools import *
from ex47.game import Room
def test_room():
gold = Room("GoldRoom",
"""This room has gold in it you can grab.There's a
door to the north. """ )
assert_equal(gold.name, "GoldRoom")
assert_equal(gold.paths, [])
def test_room_paths():
center = Room("Center","Test room in the center.")
north = Room("North", "Test room in the north.")
south = Room("South", "Test room in the south.")
center.add_paths({'north':north, 'south':south})
assert_equal(center.go('north'), north)
assert_equal(center.go('south'), south)
def test_map():
start = Room("Start ", "You can go west and down a hole. ")
west = Room("Tress ", "There are trees here, you can go east. ")
down = Room("Dungeon ", "It's dark down here,you can go up. ")
start.add_paths({'west': west,
'down': down})
west.add_paths({'east': start})
down.add_paths({'up': start})
assert_equal(start.go('west'),west)
assert_equal(start.go('west').go('east'), start)
assert_equal(start.go('down').go('up'), start)
#def setup():
# print "SETUP!"
#def teardown():
# print "TEAR DOWN!"
#def test_basic():
# print "I RAN!"
顯示結果是:
PS C:\Users\15222\lpthw\ex47\skeleton> nosetests
.EE
======================================================================
ERROR: tests.BLAH_tests.test_room_paths
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose-1.3.7-py2.7.egg\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "C:\Users\15222\lpthw\ex47\skeleton\tests\BLAH_tests.py", line 16, in test_room_paths
center.add_paths({'north':north, 'south':south})
File "C:\Users\15222\lpthw\ex47\skeleton\ex47\game.py", line 12, in add_paths
self.paths.update(paths)
AttributeError: 'list' object has no attribute 'update'
======================================================================
ERROR: tests.BLAH_tests.test_map
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose-1.3.7-py2.7.egg\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "C:\Users\15222\lpthw\ex47\skeleton\tests\BLAH_tests.py", line 26, in test_map
'down': down})
File "C:\Users\15222\lpthw\ex47\skeleton\ex47\game.py", line 12, in add_paths
self.paths.update(paths)
AttributeError: 'list' object has no attribute 'update'
----------------------------------------------------------------------
Ran 3 tests in 0.027s
FAILED (errors=2)
PS C:\Users\15222\lpthw\ex47\skeleton>
報的錯誤是;列表沒有update操作。於是我當時就很蒙,因爲他一直提示的是BLASH_test.py中的錯誤,我就檢查對應的行,但是就是用的花括號,是字典類型,肯定是有update操作的。檢查了一個多小時,沒有檢查出來。
今天我重新看代碼的時候,發現是game.py中的path產生了錯誤,用的是列表(是[ ]),終於發現了這個錯誤。
想法就是:報的錯誤雖然是那裏,但是可能那裏沒有錯,而是和他相關的地方錯了!!
修正後的:
正確代碼:
class Room(object):
def __init__(self, name, description):
self.name = name
self.description = description
self.paths = {}
def go(self, direction):
return self.paths.get(direction, None)
def add_paths(self, paths):
self.paths.update(paths)
測試文件:
from nose.tools import *
from ex47.game import Room
def test_room():
gold = Room("GoldRoom",
"""This room has gold in it you can grab.There's a
door to the north. """ )
assert_equal(gold.name, "GoldRoom")
assert_equal(gold.paths, {})
def test_room_paths():
center = Room("Center","Test room in the center.")
north = Room("North", "Test room in the north.")
south = Room("South", "Test room in the south.")
center.add_paths({'north':north, 'south':south})
assert_equal(center.go('north'), north)
assert_equal(center.go('south'), south)
def test_map():
start = Room("Start ", "You can go west and down a hole. ")
west = Room("Tress ", "There are trees here, you can go east. ")
down = Room("Dungeon ", "It's dark down here,you can go up. ")
start.add_paths({'west': west,
'down': down})
west.add_paths({'east': start})
down.add_paths({'up': start})
assert_equal(start.go('west'),west)
assert_equal(start.go('west').go('east'), start)
assert_equal(start.go('down').go('up'), start)
#def setup():
# print "SETUP!"
#def teardown():
# print "TEAR DOWN!"
#def test_basic():
# print "I RAN!"
顯示結果:
PS C:\Users\15222\lpthw\ex47\skeleton> nosetests
...
----------------------------------------------------------------------
Ran 3 tests in 0.026s
OK
PS C:\Users\15222\lpthw\ex47\skeleton>
大功告成!!!