Python NameError: global name 'assertEqual' undefined

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

I am using Python3 (and using the Python 2.x book) and I understand that assert_equals (which is used in the book) is deprecated. I am using assertEqual.

I am trying to create a test case, but for some reason when using nosetests in cmd I get an 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 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 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 tried looking for GitHub for any solutions, and I'm just not sure why it gives me a NameError and how I can fix it.

+7
python nameerror
source share
2 answers

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

+3
source share

Had a similar problem with the second module in the selenium python script test. I solved it by including "I". before "assertIn".

Before:

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

After:

 self.assertIn('images/checkbox-checked.png', webelement) 
+1
source share

All Articles