编程自己常见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>
大功告成!!!