Python名称错误:全局名称'assertEqual'未定义。

12

我正在学习《笨办法学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错误,并且我应该如何解决它。


12
assertEqual 不是 unittest 的一部分吗?但 nose 仍然使用 assert_equal - Blender
1
哇,你说得对。我刚把assertEqual改成了assert_equal,就像你说的那样,现在它完美无缺地运行了。谢谢! - auro
4个回答

10

我曾在Python的Selenium测试脚本中遇到类似的问题。通过在'assertIn'前面加上'self.'来解决了这个问题。

修改前:


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

之后:

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

5

assertEqualunittest.TestCase类的一个方法,因此只能在从该类继承的对象上使用。请查看unittest文档


确切的原因是NameError。nose.tools没有assertEqual函数。 - Joop
6
当回答一个问题时,指向一个对应库的文档却不是被使用的库,这让我觉得有些反常。 - Fredrik

1
你可以使用Python 3的unittest库来使用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)`

请确认 assertEqualunittest.Testcase 内。

1

为什么会出现NameError错误?

因为nose.tools没有assertEqual()方法。可能是你混淆了nose.toolsunittest

如何避免这种情况?

正如某人在评论中所说,noseassert_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
...

所以,你应该使用来自unittestassertEqual
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, {})

在这里阅读文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接