आदेश यकीन है कि मेरी मॉड्यूल से त्रुटि संदेश जानकारीपूर्ण हैं बनाने के लिए, मैं सभी त्रुटि संदेश assertRaises द्वारा पकड़ा देखना चाहेंगे()। आज मैं प्रत्येक assertRaises() के लिए यह करता हूं, लेकिन परीक्षण कोड में उनमें से बहुत सारे हैं, यह बहुत कठिन हो जाता है।कैसे त्रुटि संदेश Python2.7 में assertRaises() unittest में से पकड़ा दिखाने के लिए?
कैसे मैं सभी assertRaises के लिए त्रुटि संदेश() मुद्रित कर सकते हैं? मैंने http://docs.python.org/library/unittest.html पर प्रलेखन का अध्ययन किए बिना यह पता लगाने के लिए प्रलेखन का अध्ययन किया है। क्या मैं किसी भी तरह assertRaises() विधि monkeypatch कर सकते हैं? मैं सभी assertRaises() परीक्षण कोड में लाइनों को बदलने के लिए नहीं पसंद करते हैं, जैसा कि मैंने सबसे अधिक बार परीक्षण कोड मानक तरीका का उपयोग करें।
मुझे लगता है कि इस सवाल का Python unittest: how do I test the argument in an Exceptions?
से संबंधित है यह मैं कैसे यह आज क्या है।
#!/usr/bin/env python
def fail():
raise ValueError('Misspellled errrorr messageee')
और परीक्षण कोड: उदाहरण के लिए:
#!/usr/bin/env python
import unittest
import failure
class TestFailureModule(unittest.TestCase):
def testFail(self):
self.assertRaises(ValueError, failure.fail)
if __name__ == '__main__':
unittest.main()
त्रुटि संदेश की जाँच करने के लिए, मैं बस() assertRaises में त्रुटि प्रकार उदाहरण IOError के लिए करने के लिए बदल जाते हैं। फिर मैं त्रुटि संदेश देख सकता हूं:
E
======================================================================
ERROR: testFail (__main__.TestFailureModule)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_failure.py", line 8, in testFail
self.assertRaises(IOError, failure.fail)
File "/usr/lib/python2.7/unittest/case.py", line 471, in assertRaises
callableObj(*args, **kwargs)
File "/home/jonas/Skrivbord/failure.py", line 4, in fail
raise ValueError('Misspellled errrorr messageee')
ValueError: Misspellled errrorr messageee
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
कोई सुझाव? /जोनास
संपादित करें:
रॉबर्ट Rossney से संकेत के साथ मैं समस्या को हल करने में कामयाब रहे। यह मुख्य रूप से वर्तनी त्रुटियों के लिए नहीं है, लेकिन यह सुनिश्चित करने के लिए कि त्रुटि संदेश मॉड्यूल के उपयोगकर्ता के लिए वास्तव में सार्थक हैं। unittest की सामान्य कार्यप्रणाली (यह मैं इसे कैसे समय के सबसे अधिक उपयोग करें) SHOW_ERROR_MESSAGES = झूठी स्थापना द्वारा हासिल की है।
मैं बस assertRaises() विधि, जैसा कि नीचे देखा ओवरराइड। यह आकर्षण की तरह काम करता है!
SHOW_ERROR_MESSAGES = True
class NonexistantError(Exception):
pass
class ExtendedTestCase(unittest.TestCase):
def assertRaises(self, excClass, callableObj, *args, **kwargs):
if SHOW_ERROR_MESSAGES:
excClass = NonexistantError
try:
unittest.TestCase.assertRaises(self, excClass, callableObj, *args, **kwargs)
except:
print '\n ' + repr(sys.exc_info()[1])
जिसके परिणामस्वरूप उत्पादन का एक अंश:
testNotIntegerInput (__main__.TestCheckRegisteraddress) ...
TypeError('The registeraddress must be an integer. Given: 1.0',)
TypeError("The registeraddress must be an integer. Given: '1'",)
TypeError('The registeraddress must be an integer. Given: [1]',)
TypeError('The registeraddress must be an integer. Given: None',)
ok
testCorrectNumberOfBytes (__main__.TestCheckResponseNumberOfBytes) ... ok
testInconsistentLimits (__main__.TestCheckNumerical) ...
ValueError('The maxvalue must not be smaller than minvalue. Given: 45 and 47, respectively.',)
ValueError('The maxvalue must not be smaller than minvalue. Given: 45.0 and 47.0, respectively.',)
ok
testWrongValues (__main__.TestCheckRegisteraddress) ...
ValueError('The registeraddress is too small: -1, but minimum value is 0.',)
ValueError('The registeraddress is too large: 65536, but maximum value is 65535.',)
ok
testTooShortString (__main__.TestCheckResponseWriteData) ...
ValueError("The payload is too short: 2, but minimum value is 4. Given: '\\x00X'",)
ValueError("The payload is too short: 0, but minimum value is 4. Given: ''",)
ValueError("The writedata is too short: 1, but minimum value is 2. Given: 'X'",)
ValueError("The writedata is too short: 0, but minimum value is 2. Given: ''",)
ok
testKnownValues (__main__.TestCreateBitPattern) ... ok
testNotIntegerInput (__main__.TestCheckSlaveaddress) ...
TypeError('The slaveaddress must be an integer. Given: 1.0',)
TypeError("The slaveaddress must be an integer. Given: '1'",)
TypeError('The slaveaddress must be an integer. Given: [1]',)
TypeError('The slaveaddress must be an integer. Given: None',)
ok
आप तर्क जांच करने की आवश्यकता है, तो assertRaises उपयोग करने के लिए क्यों जारी रखें? क्यों न केवल अपवाद को पकड़ें और 'try' और' वगैरह 'का उपयोग करके इसकी जांच करें? –