<s> """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> import os . path <EOL> import re <EOL> import sys <EOL> import time <EOL> import traceback <EOL> import unittest <EOL> from StringIO import StringIO <EOL> from xml . sax . saxutils import escape <EOL> from StringIO import StringIO <EOL> class _TestInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , test , time ) : <EOL> _pieces = test . id ( ) . split ( '<STR_LIT:.>' ) <EOL> ( self . _class , self . _method ) = ( '<STR_LIT:.>' . join ( _pieces [ : - <NUM_LIT:1> ] ) , _pieces [ - <NUM_LIT:1> ] ) <EOL> self . _time = time <EOL> self . _error = None <EOL> self . _failure = None <EOL> def print_report ( self , stream ) : <EOL> """<STR_LIT>""" <EOL> stream . write ( '<STR_LIT>' % { <EOL> "<STR_LIT:class>" : self . _class , <EOL> "<STR_LIT>" : self . _method , <EOL> "<STR_LIT:time>" : self . _time , <EOL> } ) <EOL> if self . _failure != None : <EOL> self . _print_error ( stream , '<STR_LIT>' , self . _failure ) <EOL> if self . _error != None : <EOL> self . _print_error ( stream , '<STR_LIT:error>' , self . _error ) <EOL> stream . write ( '<STR_LIT>' ) <EOL> def _print_error ( self , stream , tagname , error ) : <EOL> """<STR_LIT>""" <EOL> text = escape ( str ( error [ <NUM_LIT:1> ] ) ) <EOL> stream . write ( '<STR_LIT:\n>' ) <EOL> stream . write ( '<STR_LIT>' % ( tagname , issubclass ( error [ <NUM_LIT:0> ] , Exception ) and error [ <NUM_LIT:0> ] . __name__ or str ( error [ <NUM_LIT:0> ] ) , text ) ) <EOL> tb_stream = StringIO ( ) <EOL> traceback . print_tb ( error [ <NUM_LIT:2> ] , None , tb_stream ) <EOL> stream . write ( escape ( tb_stream . getvalue ( ) ) ) <EOL> stream . write ( '<STR_LIT>' % tagname ) <EOL> stream . write ( '<STR_LIT:U+0020>' ) <EOL> def create_success ( test , time ) : <EOL> """<STR_LIT>""" <EOL> return _TestInfo ( test , time ) <EOL> def create_failure ( test , time , failure ) : <EOL> """<STR_LIT>""" <EOL> info = _TestInfo ( test , time ) <EOL> info . _failure = failure <EOL> return info <EOL> def create_error ( test , time , error ) : <EOL> """<STR_LIT>""" <EOL> info = _TestInfo ( test , time ) <EOL> info . _error = error <EOL> return info <EOL> class _XMLTestResult ( unittest . TestResult ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , classname ) : <EOL> unittest . TestResult . __init__ ( self ) <EOL> self . _test_name = classname <EOL> self . _start_time = None <EOL> self . _tests = [ ] <EOL> self . _error = None <EOL> self . _failure = None <EOL> def startTest ( self , test ) : <EOL> unittest . TestResult . startTest ( self , test ) <EOL> self . _error = None <EOL> self . _failure = None <EOL> self . _start_time = time . time ( ) <EOL> def stopTest ( self , test ) : <EOL> time_taken = time . time ( ) - self . _start_time <EOL> unittest . TestResult . stopTest ( self , test ) <EOL> if self . _error : <EOL> info = create_error ( test , time_taken , self . _error ) <EOL> elif self . _failure : <EOL> info = create_failure ( test , time_taken , self . _failure ) <EOL> else : <EOL> info = create_success ( test , time_taken ) <EOL> self . _tests . append ( info ) <EOL> def addError ( self , test , err ) : <EOL> unittest . TestResult . addError ( self , test , err ) <EOL> self . _error = err <EOL> def addFailure ( self , test , err ) : <EOL> unittest . TestResult . addFailure ( self , test , err ) <EOL> self . _failure = err <EOL> def print_report ( self , stream , time_taken , out , err ) : <EOL> """<STR_LIT>""" <EOL> stream . write ( '<STR_LIT>' % { "<STR_LIT:e>" : len ( self . errors ) , "<STR_LIT:f>" : len ( self . failures ) } ) <EOL> stream . write ( '<STR_LIT>' % { <EOL> "<STR_LIT:n>" : self . _test_name , <EOL> "<STR_LIT:t>" : self . testsRun , <EOL> "<STR_LIT:time>" : time_taken , <EOL> } ) <EOL> for info in self . _tests : <EOL> info . print_report ( stream ) <EOL> stream . write ( '<STR_LIT>' % out ) <EOL> stream . write ( '<STR_LIT>' % err ) <EOL> stream . write ( '<STR_LIT>' ) <EOL> class XMLTestRunner ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . _stream = kwargs . get ( '<STR_LIT>' ) <EOL> self . _filename = kwargs . get ( '<STR_LIT:filename>' ) <EOL> self . _path = "<STR_LIT:.>" <EOL> def run ( self , test ) : <EOL> """<STR_LIT>""" <EOL> class_ = test . __class__ <EOL> classname = class_ . __module__ + "<STR_LIT:.>" + class_ . __name__ <EOL> if self . _stream == None : <EOL> filename = "<STR_LIT>" % classname <EOL> if self . _filename : <EOL> filename = self . _filename <EOL> stream = file ( os . path . join ( self . _path , filename ) , "<STR_LIT:w>" ) <EOL> stream . write ( '<STR_LIT>' ) <EOL> else : <EOL> stream = self . _stream <EOL> result = _XMLTestResult ( classname ) <EOL> start_time = time . time ( ) <EOL> old_stdout = sys . stdout <EOL> old_stderr = sys . stderr <EOL> sys . stdout = StringIO ( ) <EOL> sys . stderr = StringIO ( ) <EOL> try : <EOL> test ( result ) <EOL> try : <EOL> out_s = sys . stdout . getvalue ( ) <EOL> except AttributeError : <EOL> out_s = "<STR_LIT>" <EOL> try : <EOL> err_s = sys . stderr . getvalue ( ) <EOL> except AttributeError : <EOL> err_s = "<STR_LIT>" <EOL> finally : <EOL> sys . stdout = old_stdout <EOL> sys . stderr = old_stderr <EOL> time_taken = time . time ( ) - start_time <EOL> result . print_report ( stream , time_taken , out_s , err_s ) <EOL> if self . _stream == None : <EOL> stream . close ( ) <EOL> return result <EOL> def _set_path ( self , path ) : <EOL> self . _path = path <EOL> path = property ( lambda self : self . _path , _set_path , None , <EOL> """<STR_LIT>""" ) <EOL> class XMLTestRunnerTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . _stream = StringIO ( ) <EOL> def _try_test_run ( self , test_class , expected ) : <EOL> """<STR_LIT>""" <EOL> runner = XMLTestRunner ( self . _stream ) <EOL> runner . run ( unittest . makeSuite ( test_class ) ) <EOL> got = self . _stream . getvalue ( ) <EOL> got = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , got ) <EOL> got = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , got ) <EOL> got = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , got ) <EOL> self . assertEqual ( expected , got ) <EOL> def test_no_tests ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestTest ( unittest . TestCase ) : <EOL> pass <EOL> self . _try_test_run ( TestTest , """<STR_LIT>""" ) <EOL> def test_success ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestTest ( unittest . TestCase ) : <EOL> def test_foo ( self ) : <EOL> pass <EOL> self . _try_test_run ( TestTest , """<STR_LIT>""" ) <EOL> def test_failure ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestTest ( unittest . TestCase ) : <EOL> def test_foo ( self ) : <EOL> self . assert_ ( False ) <EOL> self . _try_test_run ( TestTest , """<STR_LIT>""" ) <EOL> def test_error ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestTest ( unittest . TestCase ) : <EOL> def test_foo ( self ) : <EOL> raise IndexError ( ) <EOL> self . _try_test_run ( TestTest , """<STR_LIT>""" ) <EOL> def test_stdout_capture ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestTest ( unittest . TestCase ) : <EOL> def test_foo ( self ) : <EOL> print ( "<STR_LIT>" ) <EOL> self . _try_test_run ( TestTest , """<STR_LIT>""" ) <EOL> def test_stderr_capture ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestTest ( unittest . TestCase ) : <EOL> def test_foo ( self ) : <EOL> sys . stderr . write ( '<STR_LIT>' ) <EOL> self . _try_test_run ( TestTest , """<STR_LIT>""" ) <EOL> class NullStream ( object ) : <EOL> """<STR_LIT>""" <EOL> def write ( self , buffer ) : <EOL> pass <EOL> def test_unittests_changing_stdout ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestTest ( unittest . TestCase ) : <EOL> def test_foo ( self ) : <EOL> sys . stdout = XMLTestRunnerTest . NullStream ( ) <EOL> runner = XMLTestRunner ( self . _stream ) <EOL> runner . run ( unittest . makeSuite ( TestTest ) ) <EOL> def test_unittests_changing_stderr ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestTest ( unittest . TestCase ) : <EOL> def test_foo ( self ) : <EOL> sys . stderr = XMLTestRunnerTest . NullStream ( ) <EOL> runner = XMLTestRunner ( self . _stream ) <EOL> runner . run ( unittest . makeSuite ( TestTest ) ) <EOL> class XMLTestProgram ( unittest . TestProgram ) : <EOL> def runTests ( self ) : <EOL> if self . testRunner is None : <EOL> self . testRunner = XMLTestRunner ( ) <EOL> unittest . TestProgram . runTests ( self ) <EOL> main = XMLTestProgram <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( module = None ) </s>
