Python NameError:未定义全局名称“assertEqual”

时间:2013-07-22 03:32:25

标签: python nameerror

我正在努力学习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以及如何修复它。

4 个答案:

答案 0 :(得分:4)

assertEqual是unittest.TestCase类的方法,因此您只能在从该类继承的对象上使用它。检查the unittest documentation

答案 1 :(得分:2)

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

在:

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

后:

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

答案 2 :(得分:0)

您可以借助shift库在python 3中使用assertEqual

import unittest

class TestBalanceCheck(unittest.TestCase):

    def test(self,sol):
        self.assertEqual(sol('[](){([[[]]])}('),False)
        self.assertEqual(sol('[{{{(())}}}]((()))'),True)
        self.assertEqual(sol('[[[]])]'),False)
        print('ALL TEST CASES PASSED')
    
t = TestBalanceCheck()
t.test(balance_check)`

确保assertEqual位于unittest.Testcase

答案 3 :(得分:0)

为什么有NameError

因为nose.tools还没有assertEqual()方法。可能是您将nose.toolsunittest混合在一起。

如何避免这种情况发生?

就像有人说的那样(在评论中) nose得到了assert_equal

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, {})

但是,正式弃用了它。任何使用它都会导致DeprecationWarning

...
Asserts something ...
.../test.py:123:    
DeprecationWarning: Please use assertEqual instead.
  assert_equals(a, b)
ok
...

因此,您应该使用unittest中的assertEqual

import unittest
from ex47.game import Room

class TestGame(unittest.TestCase):
    def test_room(self):
        gold = Room("GoldRoom",
            """ This room has gold in it you can grab. There's a
                door to the north. """)
        self.assertEqual(gold.name, "GoldRoom")
        self.assertEqual(gold.paths, {})

Read the docs here