10

我正在学习 Python the Hard Way,我正在练习 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

4 回答 4

9

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

前:

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

后:

self.assertIn('images/checkbox-checked.png', webelement)
于 2017-09-19T20:19:01.960 回答
5

assertEqual 是unittest.TestCase类的方法,因此您只能在从该类继承的对象上使用它。检查单元测试文档

于 2013-07-22T15:09:32.377 回答
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
...

所以,你应该使用assertEqualfrom unittest

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

在此处阅读文档

于 2020-09-05T07:39:34.247 回答
1

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

确保assertEqual在里面unittest.Testcase

于 2020-09-05T07:06:00.263 回答