【问题标题】:Python NameError: global name 'assertEqual' is not definedPython NameError:未定义全局名称“assertEqual”
【发布时间】:2013-07-20 17:06:30
【问题描述】:

我正在关注 Learn Python the Hard Way,我正在练习第 47 题 - 自动化测试 (http://learnpythonthehardway.org/book/ex47.html)

我正在使用 Python3(与书中使用的 Python 2.x 相比)并且我意识到 assert_equals(在书中使用)已被弃用。我正在使用 assertEqual。

我正在尝试构建一个测试用例,但由于某种原因,在 cmd 中使用鼻子测试时,我收到错误: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
  • 哇,你说得对。我刚刚将assertEqual 更改为assert_equal,就像你说的那样,它完美无缺。谢谢!

标签: python nameerror


【解决方案1】:

在 python selenium 测试脚本中的第二个模块也有类似的问题。通过包含“自我”来解决它。在“assertIn”之前。

之前:

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

之后:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 正是 NameError 的原因。 nose.tools 没有 assertEqual 函数
    • 当然,但是通过指向他不使用的库的文档而不是指向他正在使用的库的文档来回答问题让我有些倒退......
    【解决方案3】:

    为什么会有NameError

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

    在你的情况下如何避免它?

    正如某人所说(在 cmets 中) 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
    ...
    

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

    Read the docs here

    【讨论】:

      【解决方案4】:

      您可以借助unittest 库在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)`
      

      确保assertEqualunittest.Testcase

      【讨论】:

        猜你喜欢
        • 2013-08-23
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        • 2011-04-27
        • 1970-01-01
        相关资源
        最近更新 更多