<s> from __future__ import division , absolute_import <EOL> import sys <EOL> import traceback <EOL> from zope . interface import implementer <EOL> from twisted . python . compat import _PY3 <EOL> from twisted . python . failure import Failure <EOL> from twisted . trial . unittest import SynchronousTestCase , PyUnitResultAdapter <EOL> from twisted . trial . itrial import IReporter , ITestCase <EOL> import unittest as pyunit <EOL> class TestPyUnitTestCase ( SynchronousTestCase ) : <EOL> class PyUnitTest ( pyunit . TestCase ) : <EOL> def test_pass ( self ) : <EOL> pass <EOL> def setUp ( self ) : <EOL> self . original = self . PyUnitTest ( '<STR_LIT>' ) <EOL> self . test = ITestCase ( self . original ) <EOL> def test_callable ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( callable ( self . test ) , <EOL> "<STR_LIT>" % ( self . test , ) ) <EOL> if _PY3 : <EOL> del TestPyUnitTestCase <EOL> class TestPyUnitResult ( SynchronousTestCase ) : <EOL> """<STR_LIT>""" <EOL> class ErrorTest ( SynchronousTestCase ) : <EOL> """<STR_LIT>""" <EOL> ran = False <EOL> def test_foo ( self ) : <EOL> """<STR_LIT>""" <EOL> self . ran = True <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> def test_dontUseAdapterWhenReporterProvidesIReporter ( self ) : <EOL> """<STR_LIT>""" <EOL> @ implementer ( IReporter ) <EOL> class StubReporter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . errors = [ ] <EOL> self . failures = [ ] <EOL> def startTest ( self , test ) : <EOL> """<STR_LIT>""" <EOL> def stopTest ( self , test ) : <EOL> """<STR_LIT>""" <EOL> def addError ( self , test , error ) : <EOL> """<STR_LIT>""" <EOL> self . errors . append ( error ) <EOL> test = self . ErrorTest ( "<STR_LIT>" ) <EOL> result = StubReporter ( ) <EOL> test . run ( result ) <EOL> self . assertIsInstance ( result . errors [ <NUM_LIT:0> ] , Failure ) <EOL> def test_success ( self ) : <EOL> class SuccessTest ( SynchronousTestCase ) : <EOL> ran = False <EOL> def test_foo ( s ) : <EOL> s . ran = True <EOL> test = SuccessTest ( '<STR_LIT>' ) <EOL> result = pyunit . TestResult ( ) <EOL> test . run ( result ) <EOL> self . failUnless ( test . ran ) <EOL> self . assertEqual ( <NUM_LIT:1> , result . testsRun ) <EOL> self . failUnless ( result . wasSuccessful ( ) ) <EOL> def test_failure ( self ) : <EOL> class FailureTest ( SynchronousTestCase ) : <EOL> ran = False <EOL> def test_foo ( s ) : <EOL> s . ran = True <EOL> s . fail ( '<STR_LIT>' ) <EOL> test = FailureTest ( '<STR_LIT>' ) <EOL> result = pyunit . TestResult ( ) <EOL> test . run ( result ) <EOL> self . failUnless ( test . ran ) <EOL> self . assertEqual ( <NUM_LIT:1> , result . testsRun ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result . failures ) ) <EOL> self . failIf ( result . wasSuccessful ( ) ) <EOL> def test_error ( self ) : <EOL> test = self . ErrorTest ( '<STR_LIT>' ) <EOL> result = pyunit . TestResult ( ) <EOL> test . run ( result ) <EOL> self . failUnless ( test . ran ) <EOL> self . assertEqual ( <NUM_LIT:1> , result . testsRun ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result . errors ) ) <EOL> self . failIf ( result . wasSuccessful ( ) ) <EOL> def test_setUpError ( self ) : <EOL> class ErrorTest ( SynchronousTestCase ) : <EOL> ran = False <EOL> def setUp ( self ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> def test_foo ( s ) : <EOL> s . ran = True <EOL> test = ErrorTest ( '<STR_LIT>' ) <EOL> result = pyunit . TestResult ( ) <EOL> test . run ( result ) <EOL> self . failIf ( test . ran ) <EOL> self . assertEqual ( <NUM_LIT:1> , result . testsRun ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result . errors ) ) <EOL> self . failIf ( result . wasSuccessful ( ) ) <EOL> def test_tracebackFromFailure ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> except ZeroDivisionError : <EOL> exc_info = sys . exc_info ( ) <EOL> f = Failure ( ) <EOL> pyresult = pyunit . TestResult ( ) <EOL> result = PyUnitResultAdapter ( pyresult ) <EOL> result . addError ( self , f ) <EOL> self . assertEqual ( pyresult . errors [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' . join ( traceback . format_exception ( * exc_info ) ) ) <EOL> def test_traceback ( self ) : <EOL> """<STR_LIT>""" <EOL> class ErrorTest ( SynchronousTestCase ) : <EOL> exc_info = None <EOL> def test_foo ( self ) : <EOL> try : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> except ZeroDivisionError : <EOL> self . exc_info = sys . exc_info ( ) <EOL> raise <EOL> test = ErrorTest ( '<STR_LIT>' ) <EOL> result = pyunit . TestResult ( ) <EOL> test . run ( result ) <EOL> expected_stack = '<STR_LIT>' . join ( traceback . format_tb ( test . exc_info [ <NUM_LIT:2> ] ) ) <EOL> observed_stack = '<STR_LIT:\n>' . join ( result . errors [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] . splitlines ( ) [ : - <NUM_LIT:1> ] ) <EOL> self . assertEqual ( expected_stack . strip ( ) , <EOL> observed_stack [ - len ( expected_stack ) : ] . strip ( ) ) <EOL> def test_tracebackFromCleanFailure ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> except ZeroDivisionError : <EOL> exc_info = sys . exc_info ( ) <EOL> f = Failure ( ) <EOL> f . cleanFailure ( ) <EOL> pyresult = pyunit . TestResult ( ) <EOL> result = PyUnitResultAdapter ( pyresult ) <EOL> result . addError ( self , f ) <EOL> self . assertEqual ( pyresult . errors [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' . join ( traceback . format_exception ( * exc_info ) ) ) <EOL> def test_trialSkip ( self ) : <EOL> """<STR_LIT>""" <EOL> class SkipTest ( SynchronousTestCase ) : <EOL> def test_skip ( self ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> test_skip . skip = "<STR_LIT>" <EOL> test = SkipTest ( '<STR_LIT>' ) <EOL> result = pyunit . TestResult ( ) <EOL> test . run ( result ) <EOL> self . assertEqual ( result . skipped , [ ( test , "<STR_LIT>" ) ] ) <EOL> def test_pyunitSkip ( self ) : <EOL> """<STR_LIT>""" <EOL> class SkipTest ( SynchronousTestCase ) : <EOL> @ pyunit . skip ( "<STR_LIT>" ) <EOL> def test_skip ( self ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> test = SkipTest ( '<STR_LIT>' ) <EOL> result = pyunit . TestResult ( ) <EOL> test . run ( result ) <EOL> self . assertEqual ( result . skipped , [ ( test , "<STR_LIT>" ) ] ) <EOL> def test_skip26 ( self ) : <EOL> """<STR_LIT>""" <EOL> class SkipTest ( SynchronousTestCase ) : <EOL> def test_skip ( self ) : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> test_skip . skip = "<STR_LIT>" <EOL> test = SkipTest ( '<STR_LIT>' ) <EOL> result = pyunit . TestResult ( ) <EOL> test . run ( result ) <EOL> self . assertEqual ( len ( result . failures ) , <NUM_LIT:1> ) <EOL> test2 , reason = result . failures [ <NUM_LIT:0> ] <EOL> self . assertIdentical ( test , test2 ) <EOL> self . assertIn ( "<STR_LIT>" , reason ) <EOL> if sys . version_info [ : <NUM_LIT:2> ] < ( <NUM_LIT:2> , <NUM_LIT:7> ) : <EOL> message = "<STR_LIT>" <EOL> test_trialSkip . skip = message <EOL> test_pyunitSkip . skip = message <EOL> del message <EOL> else : <EOL> test_skip26 . skip = "<STR_LIT>" </s>
