<s> import logging <EOL> from nose2 . tests . _common import TestCase <EOL> from nose2 . plugins import logcapture <EOL> from nose2 import session <EOL> log = logging . getLogger ( __name__ ) <EOL> class StubLogging ( object ) : <EOL> def __init__ ( self , name = None ) : <EOL> self . name = name <EOL> self . handlers = [ ] <EOL> self . level = None <EOL> def getLogger ( self , _name = None ) : <EOL> return self <EOL> def addHandler ( self , handler ) : <EOL> self . handlers . append ( handler ) <EOL> def setLevel ( self , level ) : <EOL> self . level = level <EOL> def debug ( self , message , * arg ) : <EOL> for handler in self . handlers : <EOL> handler . emit ( StubRecord ( message % arg ) ) <EOL> class StubRecord ( object ) : <EOL> def __init__ ( self , message ) : <EOL> self . message = message <EOL> self . name = '<STR_LIT>' <EOL> self . levelname = '<STR_LIT>' <EOL> self . exc_info = None <EOL> self . exc_text = None <EOL> self . stack_info = None <EOL> def getMessage ( self ) : <EOL> return self . message <EOL> class LogCaptureUnitTest ( TestCase ) : <EOL> tags = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> self . session = session . Session ( ) <EOL> self . plugin = logcapture . LogCapture ( session = self . session ) <EOL> self . logging = logcapture . logging <EOL> logcapture . logging = StubLogging ( ) <EOL> def tearDown ( self ) : <EOL> logcapture . logging = self . logging <EOL> def event ( self , error = True , failed = False ) : <EOL> e = Event ( ) <EOL> e . metadata = { } <EOL> return e <EOL> def test_buffer_cleared_after_each_test ( self ) : <EOL> self . plugin . startTestRun ( None ) <EOL> self . plugin . startTest ( None ) <EOL> logcapture . logging . getLogger ( '<STR_LIT:test>' ) . debug ( "<STR_LIT:hello>" ) <EOL> assert self . plugin . handler . buffer <EOL> self . plugin . setTestOutcome ( self . event ( ) ) <EOL> assert self . plugin . handler . buffer <EOL> self . plugin . stopTest ( None ) <EOL> assert not self . plugin . handler . buffer <EOL> def test_buffered_logs_attached_to_event ( self ) : <EOL> self . plugin . startTestRun ( None ) <EOL> self . plugin . startTest ( None ) <EOL> logcapture . logging . getLogger ( '<STR_LIT:test>' ) . debug ( "<STR_LIT:hello>" ) <EOL> assert self . plugin . handler . buffer <EOL> e = self . event ( ) <EOL> self . plugin . setTestOutcome ( e ) <EOL> assert '<STR_LIT>' in e . metadata , "<STR_LIT>" % e . metadata <EOL> class Event : <EOL> pass </s>
