10

I'm following Learn Python the Hard Way and I'm on Exercise 47 - Automated Testing (http://learnpythonthehardway.org/book/ex47.html)

I am using Python3 (vs the book's use of Python 2.x) and I realize that assert_equals (which is used in the book) is deprecated. I am using assertEqual.

I am trying to build a test case but for some reason, when using nosetests in cmd, I get the error: NameError: global name 'assertEqual' is not defined

Here is the code:

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)

I've tried searching GitHub for any solutions, and I'm just not sure why it's giving me the NameError and how would I go about fixing it.

abhi
  • 1,760
  • 1
  • 24
  • 40
auro
  • 119
  • 1
  • 1
  • 6
  • 12
    Isn't `assertEqual` part of unittest? nose still uses `assert_equal`. – Blender Jul 22 '13 at 03:35
  • 1
    Wow you're right. I just changed `assertEqual` to `assert_equal` like you stated and it works flawlessly. Thank you! – auro Jul 22 '13 at 03:40

4 Answers4

10

Had a similar problem with the second module in a python selenium test script. Solved it by including 'self.' before the 'assertIn'.

Before:

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

After:

self.assertIn('images/checkbox-checked.png', webelement)
user8636702
  • 101
  • 1
  • 2
5

assertEqual is a method of unittest.TestCase class, so you can only use it on objects that inherit from that class. Check the unittest documentation.

charlesreid1
  • 4,360
  • 4
  • 30
  • 52
Joop
  • 7,840
  • 9
  • 43
  • 58
  • exactly the reason for the NameError. nose.tools have no assertEqual function – Joop Jul 23 '13 at 11:58
  • 6
    Sure, but answering a question by pointing to the docs for a library he's not using instead of pointing to the docs for the library he's using strikes me as somewhat backwards... – Fredrik Jul 23 '13 at 17:36
1

You can use assertEqual in python 3 with the help of unittest Library.

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)`

Make sure assertEqual is inside unittest.Testcase

weAreStarsDust
  • 2,634
  • 3
  • 10
  • 22
1

Why there is NameError?

Because nose.tools haven't got assertEqual() method. Possibly, you are mixing nose.tools with unittest.

How to avoid it in your case?

As someone said (in comments) nose has got 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, {})

But, officially it is deprecated. Any usage of it causes DeprecationWarning:

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

So, you should use assertEqual from 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, {})

Read the docs here

ISD
  • 984
  • 1
  • 6
  • 21