2013-07-22 4 views
7

私は、Python苦労して学び、次のよ定義されていないと私は運動47上だ - 自動テスト(http://learnpythonthehardway.org/book/ex47.htmlPythonのNameErrorは:グローバル名「assertEqualは」

私は(の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が与えられているのか分かりませんし、どうすれば修正できるでしょうか。

+12

はunittestモジュールの 'assertEqual'一部ではないですか?ノーズはまだ 'assert_equal'を使います。 – Blender

+1

あなたは正しいですよ。あなたが述べたように 'assertEqual'を' assert_equal'に変更しただけで完璧に動作します。ありがとうございました! – auro

答えて

3

assertEqualはunittest.TestCaseクラスのメソッドなので、そのクラスから継承したオブジェクトに対してのみ使用できます。 the unittest documentationを確認してください。

+1

サンプルはユニットテストではなく、ノーズを使用します。 – Fredrik

+0

名前エラーの理由。 nose.toolsにassertEqual関数はありません – Joop

+5

確かに、彼が使用しているライブラリのドキュメントを指し示すのではなく、使用していないライブラリのドキュメントを指して質問に答えると、やや後方に私が当たります... – Fredrik

1

Pythonのセレンテストスクリプトで2番目のモジュールと同様の問題がありました。 「自己」を含めて解決した。 'assertIn'の前に。後

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

::前

self.assertIn('images/checkbox-checked.png', webelement) 
関連する問題