2013-07-22 9 views
7

मैं जानें अजगर हार्ड रास्ता अनुसरण कर रहा हूँ से परिभाषित नहीं है और मैं व्यायाम 47 पर हूँ - स्वचालित परीक्षण (http://learnpythonthehardway.org/book/ex47.html)अजगर NameError: वैश्विक नाम 'assertEqual'

मैं python3 उपयोग कर रहा हूँ (बनाम की पुस्तक के उपयोग पायथन 2.x) और मुझे एहसास है कि assert_equals (जो पुस्तक में उपयोग किया जाता है) बहिष्कृत है। मैं assertEqual का उपयोग कर रहा हूँ।

मैं एक टेस्ट केस का निर्माण करने की कोशिश कर रहा हूँ, लेकिन किसी कारण से, जब cmd में nosetests का उपयोग कर, मैं त्रुटि मिलती है:

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) 

मैं खोज की कोशिश की है: NameError: global name 'assertEqual' is not defined

यहाँ कोड है गिटहब किसी भी समाधान के लिए, और मुझे यकीन नहीं है कि यह मुझे नेमरर क्यों दे रहा है और मैं इसे ठीक करने के बारे में कैसे जाउंगा।

+12

नहीं है 'assertEqual' unittest का हिस्सा है? नाक अभी भी 'assert_equal' का उपयोग करता है। – Blender

+1

वाह आप सही हैं। मैंने अभी तक 'assertEqual' को' assert_equal' में बदल दिया है जैसा कि आपने कहा है और यह बेकार ढंग से काम करता है। धन्यवाद! – auro

उत्तर

3

assertEqual unittest.TestCase कक्षा का एक तरीका है, इसलिए आप केवल उस वर्ग से प्राप्त वस्तुओं पर इसका उपयोग कर सकते हैं। the unittest documentation देखें।

+1

नमूना नाक का उपयोग करता है, इकाई परीक्षण नहीं। – Fredrik

+0

बिल्कुल नाम त्रुटि के कारण। nose.tools का कोई दावा नहीं है एक्वाल फ़ंक्शन – Joop

+5

निश्चित रूप से, लेकिन लाइब्रेरी के लिए दस्तावेज़ों को इंगित करके एक प्रश्न का उत्तर देकर वह लाइब्रेरी के लिए दस्तावेज़ों को इंगित करने के बजाय उपयोग नहीं कर रहा है, जिसका उपयोग वह मुझे कुछ हद तक पीछे छोड़ देता है ... – Fredrik

1

एक पाइथन सेलेनियम परीक्षण स्क्रिप्ट में दूसरे मॉड्यूल के साथ एक ही समस्या थी। इसे 'स्वयं' सहित हल किया गया। 'assertIn' से पहले।

से पहले:

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

के बाद:

self.assertIn('images/checkbox-checked.png', webelement) 
संबंधित मुद्दे