我下面学习Python的硬道理,而我在练习47 - 自动化测试(http://learnpythonthehardway.org/book/ex47.html)的Python NameError:全局名称“assertEqual便”没有定义
我使用Python3(VS书的使用Python 2.x),我意识到assert_equals(在本书中使用)已被弃用。我正在使用assertEqual。
我想建立一个测试用例,但由于某些原因,使用CMD 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,我将如何去解决它。
是不是'的单元测试assertEqual'一部分?鼻子仍然使用'assert_equal'。 – Blender
哇,你是对的。我刚刚将'assertEqual'更改为'assert_equal',就像你说的那样,它完美无瑕。谢谢! – auro