2012-11-21 12 views
5

मैं जोर देकर unittest का उपयोग कर रहा हूं कि मेरी स्क्रिप्ट सही SystemExit कोड उठाती है।Unittest: दाएं सिस्टमएक्सिट कोड

से http://docs.python.org/3.3/library/unittest.html#unittest.TestCase.assertRaises

with self.assertRaises(SomeException) as cm: 
    do_something() 

the_exception = cm.exception 
self.assertEqual(the_exception.error_code, 3) 

उदाहरण के आधार पर मैं कोडित इस:

with self.assertRaises(SystemExit) as cm: 
    do_something() 

the_exception = cm.exception 
self.assertEqual(the_exception.error_code, 3) 

बहरहाल, यह काम नहीं करता। निम्नलिखित त्रुटि ऊपर आता है:

AttributeError: 'SystemExit' object has no attribute 'error_code' 

उत्तर

8

SystemExit BaseException और नहीं StandardError से सीधे निकला है, इस प्रकार यह विशेषता error_code जरूरत नहीं है।

error_code के बजाय आपको विशेषता code का उपयोग करना होगा। उदाहरण इस तरह दिखेगा:

with self.assertRaises(SystemExit) as cm: 
    do_something() 

the_exception = cm.exception 
self.assertEqual(the_exception.code, 3) 
संबंधित मुद्दे