<s> import os <EOL> import traceback <EOL> from datetime import datetime <EOL> from itertools import groupby <EOL> from unittest import TextTestResult <EOL> from xml . sax . saxutils import XMLGenerator <EOL> from xml . sax . xmlreader import AttributesImpl <EOL> from django . utils . encoding import smart_text <EOL> STDOUT_LINE = '<STR_LIT>' <EOL> STDERR_LINE = '<STR_LIT>' <EOL> class TestInfo ( object ) : <EOL> class RESULT ( object ) : <EOL> SUCCESS = <NUM_LIT:0> <EOL> ERROR = <NUM_LIT:1> <EOL> FAILURE = <NUM_LIT:2> <EOL> EXPECTED_FAILURE = <NUM_LIT:3> <EOL> UNEXPECTED_SUCCESS = <NUM_LIT:4> <EOL> SKIPPED = <NUM_LIT:5> <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:result>' , '<STR_LIT>' ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> for slot_name in self . __slots__ : <EOL> setattr ( self , slot_name , None ) <EOL> for key , value in kwargs . items ( ) : <EOL> setattr ( self , key , value ) <EOL> class XMLTestResult ( TextTestResult ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( XMLTestResult , self ) . __init__ ( * args , ** kwargs ) <EOL> self . testInfos = [ ] <EOL> self . currentTestInfo = None <EOL> def startTestRun ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( XMLTestResult , self ) . startTestRun ( ) <EOL> def startTest ( self , test ) : <EOL> """<STR_LIT>""" <EOL> self . currentTestInfo = TestInfo ( test_method = test , <EOL> start_time = datetime . now ( ) ) <EOL> super ( XMLTestResult , self ) . startTest ( test ) <EOL> def stopTest ( self , test ) : <EOL> """<STR_LIT>""" <EOL> self . currentTestInfo . end_time = datetime . now ( ) <EOL> self . currentTestInfo . stdout = self . _stdout_buffer . getvalue ( ) <EOL> self . currentTestInfo . stderr = self . _stderr_buffer . getvalue ( ) <EOL> self . testInfos . append ( self . currentTestInfo ) <EOL> super ( XMLTestResult , self ) . stopTest ( test ) <EOL> def addError ( self , test , err ) : <EOL> """<STR_LIT>""" <EOL> self . currentTestInfo . result = TestInfo . RESULT . ERROR <EOL> self . currentTestInfo . err = err <EOL> super ( XMLTestResult , self ) . addError ( test , err ) <EOL> def addFailure ( self , test , err ) : <EOL> """<STR_LIT>""" <EOL> self . currentTestInfo . result = TestInfo . RESULT . FAILURE <EOL> self . currentTestInfo . err = err <EOL> super ( XMLTestResult , self ) . addFailure ( test , err ) <EOL> def addSuccess ( self , test ) : <EOL> """<STR_LIT>""" <EOL> self . currentTestInfo . result = TestInfo . RESULT . SUCCESS <EOL> super ( XMLTestResult , self ) . addSuccess ( test ) <EOL> def addSkip ( self , test , reason ) : <EOL> """<STR_LIT>""" <EOL> self . currentTestInfo . result = TestInfo . RESULT . SKIPPED <EOL> self . currentTestInfo . reason = reason <EOL> super ( XMLTestResult , self ) . addSkip ( test , reason ) <EOL> def addExpectedFailure ( self , test , err ) : <EOL> """<STR_LIT>""" <EOL> self . currentTestInfo . result = TestInfo . RESULT . EXPECTED_FAILURE <EOL> self . currentTestInfo . err = err <EOL> super ( XMLTestResult , self ) . addExpectedFailure ( test , err ) <EOL> def addUnexpectedSuccess ( self , test ) : <EOL> """<STR_LIT>""" <EOL> self . currentTestInfo . result = TestInfo . RESULT . UNEXPECTED_SUCCESS <EOL> super ( XMLTestResult , self ) . addUnexpectedSuccess ( test ) <EOL> def _exc_info_to_string ( self , err , test ) : <EOL> """<STR_LIT>""" <EOL> exctype , value , tb = err <EOL> while tb and self . _is_relevant_tb_level ( tb ) : <EOL> tb = tb . tb_next <EOL> if exctype is test . failureException : <EOL> length = self . _count_relevant_tb_levels ( tb ) <EOL> msgLines = traceback . format_exception ( exctype , value , tb , length ) <EOL> else : <EOL> msgLines = traceback . format_exception ( exctype , value , tb ) <EOL> if self . buffer : <EOL> output = self . _stdout_buffer . getvalue ( ) <EOL> error = self . _stderr_buffer . getvalue ( ) <EOL> if output : <EOL> if not output . endswith ( '<STR_LIT:\n>' ) : <EOL> output += '<STR_LIT:\n>' <EOL> msgLines . append ( STDOUT_LINE % output ) <EOL> if error : <EOL> if not error . endswith ( '<STR_LIT:\n>' ) : <EOL> error += '<STR_LIT:\n>' <EOL> msgLines . append ( STDERR_LINE % error ) <EOL> return '<STR_LIT>' . join ( msgLines ) <EOL> def test_method_name ( self , test ) : <EOL> """<STR_LIT>""" <EOL> test_id = test . id ( ) <EOL> return test_id . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> def test_case_name ( self , test ) : <EOL> """<STR_LIT>""" <EOL> testcase = type ( test ) <EOL> module = testcase . __module__ + '<STR_LIT:.>' <EOL> if module == '<STR_LIT>' : <EOL> module = '<STR_LIT>' <EOL> return module + testcase . __name__ <EOL> def dump_xml ( self , output_dir ) : <EOL> """<STR_LIT>""" <EOL> self . buffer = False <EOL> if not os . path . exists ( output_dir ) : <EOL> os . makedirs ( output_dir ) <EOL> with open ( os . path . join ( output_dir , '<STR_LIT>' ) , '<STR_LIT:w>' ) as output : <EOL> document = XMLGenerator ( output , '<STR_LIT:utf-8>' ) <EOL> document . startDocument ( ) <EOL> document . startElement ( '<STR_LIT>' , AttributesImpl ( { } ) ) <EOL> suites = groupby ( <EOL> self . testInfos , key = lambda test_info : self . test_case_name ( test_info . test_method ) <EOL> ) <EOL> for suite_name , suite in suites : <EOL> document . startElement ( '<STR_LIT>' , <EOL> AttributesImpl ( { '<STR_LIT:name>' : suite_name } ) ) <EOL> for test_info in suite : <EOL> document . startElement ( '<STR_LIT>' , AttributesImpl ( { <EOL> '<STR_LIT>' : suite_name , <EOL> '<STR_LIT:name>' : self . test_method_name ( test_info . test_method ) , <EOL> '<STR_LIT:time>' : '<STR_LIT>' % ( <EOL> test_info . end_time - test_info . start_time <EOL> ) . total_seconds ( ) <EOL> } ) ) <EOL> if test_info . result == TestInfo . RESULT . ERROR : <EOL> document . startElement ( '<STR_LIT:error>' , AttributesImpl ( { <EOL> '<STR_LIT:message>' : smart_text ( test_info . err [ <NUM_LIT:1> ] ) <EOL> } ) ) <EOL> document . characters ( self . _exc_info_to_string ( test_info . err , test_info . test_method ) ) <EOL> document . endElement ( '<STR_LIT:error>' ) <EOL> elif test_info . result == TestInfo . RESULT . FAILURE : <EOL> document . startElement ( '<STR_LIT>' , AttributesImpl ( { <EOL> '<STR_LIT:message>' : smart_text ( test_info . err [ <NUM_LIT:1> ] ) <EOL> } ) ) <EOL> document . characters ( <EOL> self . _exc_info_to_string ( test_info . err , test_info . test_method ) . decode ( '<STR_LIT:utf-8>' ) <EOL> ) <EOL> document . endElement ( '<STR_LIT>' ) <EOL> elif test_info . result == TestInfo . RESULT . UNEXPECTED_SUCCESS : <EOL> document . startElement ( '<STR_LIT:error>' , AttributesImpl ( { <EOL> '<STR_LIT:message>' : '<STR_LIT>' <EOL> } ) ) <EOL> document . endElement ( '<STR_LIT:error>' ) <EOL> elif test_info . result == TestInfo . RESULT . SKIPPED : <EOL> document . startElement ( '<STR_LIT>' , AttributesImpl ( { } ) ) <EOL> document . characters ( test_info . reason ) <EOL> document . endElement ( '<STR_LIT>' ) <EOL> if test_info . stdout : <EOL> document . startElement ( '<STR_LIT>' , AttributesImpl ( { } ) ) <EOL> document . characters ( test_info . stdout ) <EOL> document . endElement ( '<STR_LIT>' ) <EOL> if test_info . stderr : <EOL> document . startElement ( '<STR_LIT>' , AttributesImpl ( { } ) ) <EOL> document . characters ( test_info . stderr ) <EOL> document . endElement ( '<STR_LIT>' ) <EOL> document . endElement ( '<STR_LIT>' ) <EOL> document . endElement ( '<STR_LIT>' ) <EOL> document . endElement ( '<STR_LIT>' ) <EOL> document . endDocument ( ) </s>
