我正在学习《笨办法学Python》,现在进行到第47个练习——自动化测试(http://learnpythonthehardway.org/book/ex47.html)。
我使用的是Python3(与书中使用的Python 2.x不同),我意识到assert_equals(书中使用的方法)已经被弃用。我现在使用assertEqual代替。
我试图构建一个测试用例,但是当我在命令提示符中使用nosetests时,出现了以下错误:NameError: global name 'assertEqual' is not defined
以下是代码:
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. """)
assertEqual(gold.name, "GoldRoom")
assertEqual(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})
assertEqual(center.go('north'), north)
assertEqual(center.go('south'), south)
def test_map():
start = Room("Start", "You can go west and down a hole")
west = Room("Trees", "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})
assertEqual(start.go('west'), west)
assertEqual(start.go('west').go('east'), start)
assertEqual(start.go('down').go('up'), start)
我已经尝试在 GitHub 上搜索任何解决方案,但不确定为什么会出现 NameError,并且该如何修复它。
我已经尝试在GitHub上搜索任何解决方案,但不确定为什么会出现NameError错误,并且我应该如何解决它。
assertEqual
不是unittest
的一部分吗?但nose
仍然使用assert_equal
。 - BlenderassertEqual
改成了assert_equal
,就像你说的那样,现在它完美无缺地运行了。谢谢! - auro