Python NameError:未定义全局名称'assertEqual'

我正在学习Python的难题,我正在练习47 - 自动测试(http://learnpythonthehardway.org/book/ex47.html)

我使用的是Python3(与本书使用的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是unittest.TestCase类的一个方法,因此您只能在从该类继承的对象上使用它。 检查单元测试文档。


在python硒测试脚本中遇到与第二个模块类似的问题。 通过包含'自我'来解决它。 在'assertIn'之前。

之前:

assertIn('images/checkbox-checked.png', ET)

后:

self.assertIn('images/checkbox-checked.png', webelement)
链接地址: http://www.djcxy.com/p/72869.html

上一篇: Python NameError: global name 'assertEqual' is not defined

下一篇: {} idiom in Javascript work?