编程自己常见error(4)

本文分享了在Python游戏开发中遇到的路径测试错误及解决过程。错误源于使用列表而非字典作为路径存储,导致无法使用update方法。通过将列表更改为字典,成功解决了测试错误,确保了游戏路径的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编程自己常见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>

大功告成!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值