mò %U²Ic@sàdZdZdZddd!ZdkZdkZdkZdkZdkZddd d d d d dgZ e i dddgƒei d d*jod+\Z Z d„ZneZd„ZdZdfd„ƒYZdfd„ƒYZd fd„ƒYZd efd„ƒYZd fd„ƒYZeƒZdd„Zed„Zdeed„Zdeed „Zd!fd"„ƒYZd#efd$„ƒYZ d fd%„ƒYZ!d&fd'„ƒYZ"e"Z#e$d(joe#d)dƒndS(,sw Python unit testing framework, based on Erich Gamma's JUnit and Kent Beck's Smalltalk testing framework. This module contains the core framework classes that form the basis of specific test cases and suites (TestCase, TestSuite etc.), and also a text-based utility class for running the tests and reporting the results (TextTestRunner). Simple usage: import unittest class IntegerArithmenticTestCase(unittest.TestCase): def testAdd(self): ## test method names begin 'test*' self.assertEquals((1 + 2), 3) self.assertEquals(0 + 1, 1) def testMultiply(self): self.assertEquals((0 * 10), 0) self.assertEquals((5 * 8), 40) if __name__ == '__main__': unittest.main() Further information is available in the bundled documentation, and from http://pyunit.sourceforge.net/ Copyright (c) 1999-2003 Steve Purcell This module is free software, and you may redistribute it and/or modify it under the same terms as Python itself, so long as this copyright message and disclaimer are retained in their original form. IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. s Steve Purcells stephen_purcell at yahoo dot coms#Revision: 1.63 $i iþÿÿÿNt TestResulttTestCaset TestSuitetTextTestRunnert TestLoadertFunctionTestCasetmaintdefaultTestLoadertgetTestCaseNamest makeSuitet findTestCasesiiicCsŠdk}t|ƒtitifjoNxC|D];}|tjo ti}n|i||ƒodSq/q/WdSn|i||ƒSdS(Nii( t __builtin__ttypetclsinfottypest TupleTypetListTypetclst ClassTypet isinstancetobj(RR R R((t%/data/zmath/lib/python2.4/unittest.pyRHs   cCsd|i|ifS(Ns%s.%s(Rt __module__t__name__(R((Rt _strclassZscBsztZdZd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z d „Z d „Z d „Z d „ZRS( sÙHolder for test result information. Test results are automatically managed by the TestCase and TestSuite classes, and do not need to be explicitly manipulated by writers of tests. Each instance holds the total number of tests run, and collections of failures and errors that occurred among those test runs. The collections contain tuples of (testcase, exceptioninfo), where exceptioninfo is the formatted traceback of the error that occurred. cCs(g|_g|_d|_d|_dS(Ni(tselftfailuresterrorsttestsRunt shouldStop(R((Rt__init__js   cCs|id|_dS(s-Called when the given test is about to be runiN(RR(Rttest((Rt startTestpscCsdS(s'Called when the given test has been runN((RR((RtstopTesttscCs&|ii||i||ƒfƒdS(smCalled when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info(). N(RRtappendRt_exc_info_to_stringterr(RRR$((RtaddErrorxscCs&|ii||i||ƒfƒdS(sdCalled when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info().N(RRR"RR#R$(RRR$((Rt addFailure~scCsdS(s-Called when a test has completed successfullyN((RR((Rt addSuccessƒscCs-t|iƒt|iƒjo djnS(s.Tells whether or not this result was a successiN(tlenRRR(R((Rt wasSuccessful‡scCs t|_dS(s*Indicates that the tests should be abortedN(tTrueRR(R((Rtstop‹scCs•|\}}}x%|o|i|ƒo |i}qW||ijo2|i |ƒ}di t i ||||ƒƒSndi t i |||ƒƒS(s>Converts a sys.exc_info()-style tuple of values into a string.tN(R$texctypetvaluettbRt_is_relevant_tb_levelttb_nextRtfailureExceptiont_count_relevant_tb_levelstlengthtjoint tracebacktformat_exception(RR$RR.R-R4R/((RR#�s#cCs|iiidƒS(Nt __unittest(R/ttb_framet f_globalsthas_key(RR/((RR0›scCs=d}x0|o(|i|ƒ o|d7}|i}q W|S(Nii(R4R/RR0R1(RR/R4((RR3žs  cCs2dt|iƒ|it|iƒt|iƒfS(Ns!<%s run=%i errors=%i failures=%i>(RRt __class__RR(RR(R((Rt__repr__¥s(RRt__doc__RR R!R%R&R'R)R+R#R0R3R=(((RR_s           cBs0tZdZeZdd„Zd„Zd„Zd„Zd„Z d„Z d„Z d „Z d „Z ed „Zd „Zd „Zd„Zed„Zed„Zed„Zd„Zed„Zed„Zded„Zded„ZeZZeZZeZZ eZ!Z"eZ#eZ$Z%eZ&RS(s«A class whose instances are single test cases. By default, the test code itself should be placed in a method named 'runTest'. If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute. Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively. If it is necessary to override the __init__ method, the base class __init__ method must always be called. It is important that subclasses should not change the signature of their __init__ method, since instances of the classes are instantiated automatically by parts of the framework in order to be run. trunTestcCsXy(||_t||ƒ}|i|_Wn)tj otd|i |f‚nXdS(sÅCreate an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name. sno such test method in %s: %sN( t methodNameRt_TestCase__testMethodNametgetattrt testMethodR>t_TestCase__testMethodDoctAttributeErrort ValueErrorR<(RR@RC((RRÆs cCsdS(sAHook method for setting up the test fixture before exercising it.N((R((RtsetUpÓscCsdS(sAHook method for deconstructing the test fixture after testing it.N((R((RttearDown×scCsdS(Ni((R((RtcountTestCasesÛscCstƒS(N(R(R((RtdefaultTestResultÞscCs.|i}|o|idƒdiƒpdS(såReturns a one-line description of the test, or None if no description has been provided. The default implementation of this method returns the first line of the specified test method's docstring. s iN(RRDtdoctsplittstriptNone(RRK((RtshortDescriptionás cCsdt|iƒ|ifS(Ns%s.%s(RRR<RA(R((RtidëscCsd|it|iƒfS(Ns%s (%s)(RRARR<(R((Rt__str__îscCsdt|iƒ|ifS(Ns<%s testMethod=%s>(RRR<RA(R((RR=ñscCst|djo|iƒ}n|i|ƒt||iƒ}z#y|iƒWn6t j o ‚n"|i ||i ƒƒdSnXt }y|ƒt}Wn]|ij o|i||i ƒƒn3t j o ‚n|i ||i ƒƒnXy|iƒWn8t j o ‚n$|i ||i ƒƒt }nX|o|i|ƒnWd|i|ƒXdS(N(tresultRNRRJR RBRARCRGtKeyboardInterruptR%t_TestCase__exc_infotFalsetokR*R2R&RHR'R!(RRRRVRC((RtrunõsB    cOs|i||ŽS(N(RRWtargstkwds(RRXRY((Rt__call__scCs+|iƒt||iƒƒ|iƒdS(s6Run the test without collecting errors in a TestResultN(RRGRBRARH(R((Rtdebugs cCsGtiƒ\}}}tid djo|||fSn|||fS(s¡Return a version of sys.exc_info() with the traceback frame minimised; usually the top level of the traceback frame is not needed. itjavaN(tsystexc_infoR-texcvalueR/tplatform(RR-R/R_((Rt __exc_info!s cCs|i|‚dS(s)Fail immediately, with the given message.N(RR2tmsg(RRb((Rtfail+scCs|o|i|‚ndS(s(Fail the test if the expression is true.N(texprRR2Rb(RRdRb((RtfailIf/scCs|p|i|‚ndS(s,Fail the test unless the expression is true.N(RdRR2Rb(RRdRb((Rt failUnless3scOshy|||ŽWn|j o dSn;Xt|dƒo |i}n t|ƒ}|i d|‚dS(siFail unless an exception of class excClass is thrown by callableObj when invoked with arguments args and keyword arguments kwargs. If a different type of exception is thrown, it will not be caught, and the test case will be deemed to have suffered an error, exactly as for an unexpected exception. NRs %s not raised( t callableObjRXtkwargstexcClassthasattrRtexcNametstrRR2(RRiRgRXRhRk((RtfailUnlessRaises7s   cCs2||jp!|i|pd||f‚ndS(s[Fail if the two objects are unequal as determined by the '==' operator. s%r != %rN(tfirsttsecondRR2Rb(RRnRoRb((RtfailUnlessEqualHs cCs2||jo!|i|pd||f‚ndS(sYFail if the two objects are equal as determined by the '==' operator. s%r == %rN(RnRoRR2Rb(RRnRoRb((Rt failIfEqualPs icCsBt|||ƒdjo$|i|pd|||f‚ndS(sKFail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero. Note that decimal places (from zero) are usually not the same as significant digits (measured from the most signficant digit). is%r != %r within %r placesN(troundRoRntplacesRR2Rb(RRnRoRsRb((RtfailUnlessAlmostEqualXscCsBt|||ƒdjo$|i|pd|||f‚ndS(sIFail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero. Note that decimal places (from zero) are usually not the same as significant digits (measured from the most signficant digit). is%r == %r within %r placesN(RrRoRnRsRR2Rb(RRnRoRsRb((RtfailIfAlmostEqualds('RRR>tAssertionErrorR2RRGRHRIRJRORPRQR=RNRWRZR[RTRcReRfRmRpRqRtRut assertEqualt assertEqualstassertNotEqualtassertNotEqualstassertAlmostEqualtassertAlmostEqualstassertNotAlmostEqualtassertNotAlmostEqualst assertRaisestassert_t assertTruet assertFalse(((RRªs<         #              cBshtZdZfd„Zd„ZeZd„Zd„Zd„Zd„Z d„Z d„Z d „Z RS( s³A test suite is a composite test consisting of a number of TestCases. For use, create an instance of TestSuite, then add test case instances. When all tests have been added, the suite can be passed to a test runner, such as TextTestRunner. It will run the individual test cases in the order in which they were added, aggregating the results. When subclassing, do not forget to call the base class constructor. cCsg|_|i|ƒdS(N(Rt_teststaddTeststtests(RR…((RR‹s cCsdt|iƒ|ifS(Ns <%s tests=%s>(RRR<Rƒ(R((RR=�scCs t|iƒS(N(titerRRƒ(R((Rt__iter__”scCs.d}x!|iD]}||iƒ7}qW|S(Ni(tcasesRRƒRRI(RRRˆ((RRI—s  cCs|ii|ƒdS(N(RRƒR"R(RR((RtaddTest�scCs"x|D]}|i|ƒqWdS(N(R…RRR‰(RR…R((RR„ scCs1x*|iD]}|ioPn||ƒq W|S(N(RRƒRRRR(RRRR((RRW¤s   cOs|i||ŽS(N(RRWRXRY(RRXRY((RRZ«scCs"x|iD]}|iƒq WdS(s7Run the tests without collecting errors in a TestResultN(RRƒRR[(RR((RR[®s ( RRR>RR=RQR‡RIR‰R„RWRZR[(((RR‚s         cBs_tZdZeeed„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z RS( sGA test case that wraps a test function. This is useful for slipping pre-existing test functions into the PyUnit framework. Optionally, set-up and tidy-up functions can be supplied. As with TestCase, the tidy-up ('tearDown') function will always be called if the set-up ('setUp') function ran successfully. cCs5ti|ƒ||_||_||_||_ dS(N( RRRRGt_FunctionTestCase__setUpFuncRHt_FunctionTestCase__tearDownFuncttestFunct_FunctionTestCase__testFunct descriptiont_FunctionTestCase__description(RRŒRGRHRŽ((RR¼s     cCs"|idj o|iƒndS(N(RRŠRN(R((RRGÄscCs"|idj o|iƒndS(N(RR‹RN(R((RRHÈscCs|iƒdS(N(RR�(R((RR?ÌscCs |iiS(N(RR�R(R((RRPÏscCsdt|iƒ|iifS(Ns%s (%s)(RRR<R�R(R((RRQÒscCsdt|iƒ|ifS(Ns<%s testFunc=%s>(RRR<R�(R((RR=ÕscCsL|idj o |iSn|ii}|o|idƒdiƒpdS(Ns i(RR�RNR�R>RKRLRM(RRK((RROØs  ( RRR>RNRRGRHR?RPRQR=RO(((RR³s       cBsStZdZdZeZeZd„Zd„Z e d„Z e d„Z d„Z RS(swThis class is responsible for loading tests according to various criteria and returning them wrapped in a Test RcCsjt|tƒotdƒ‚n|i|ƒ}| ot|dƒo dg}n|it ||ƒƒS(s<Return a suite of all tests cases contained in testCaseClasssYTest cases should not be derived from TestSuite. Maybe you meant to derive from TestCase?R?N( t issubclasst testCaseClassRt TypeErrorRRt testCaseNamesRjt suiteClasstmap(RR‘R“((RtloadTestsFromTestCaseës cCs|g}xft|ƒD]X}t||ƒ}t|tti fƒo*t |t ƒo|i |i|ƒƒqqW|i|ƒS(s?Return a suite of all tests cases contained in the given moduleN(R…tdirtmoduletnameRBRRR RRR�RR"RR–R”(RR˜R…RR™((RtloadTestsFromModuleôs )c Cs®|idƒ}|djoi|}xQ|oIytdi|ƒƒ}PWq&tj o|d=|p‚qrq&Xq&W|d}n|}x$|D]}|t ||ƒ}}q’Wt |ƒtijo|i|ƒSnÒt|t tifƒo!t|tƒo|i|ƒSn˜t |ƒtijo||iƒSnqt|tƒo|SnYt|ƒo>|ƒ}t|ttfƒptd||f‚n|Sntd|‚dS(sTReturn a suite of all tests cases given a string specifier. The name may resolve either to a module, a test case class, a test method within a test case class, or a callable object which returns a TestCase or TestSuite instance. The method optionally resolves the names relative to a given module. t.iÿÿÿÿis"calling %s returned %s, not a tests$don't know how to make test from: %sN(R™RLtpartsR˜RNt parts_copyt __import__R5t ImportErrorRtpartRBtparentR Rt ModuleTypeRRšRRR�RR–tUnboundMethodTypeRRtcallableRRF( RR™R˜RR¡R�RœRR ((RtloadTestsFromNameþs@ )  cCs:g}|D]}||i||ƒq ~}|i|ƒS(s‚Return a suite of all tests cases found using the given sequence of string specifiers. See 'loadTestsFromName()'. N(t_[1]tnamesR™RR¥R˜tsuitesR”(RR§R˜R¨R™R¦((RtloadTestsFromNames(s-cCs•||id„}t|t|ƒƒ}xI|iD]>}x5|i |ƒD]$}||jo|i |ƒqGqGWq1W|i o|i |i ƒn|S(sLReturn a sorted sequence of method names found within testCaseClass cCs#|i|ƒott||ƒƒS(N(tattrnamet startswithtprefixR¤RBR‘(RªR‘R¬((Rt isTestMethod2sN(R‘RttestMethodPrefixR­tfilterR—t testFnNamest __bases__t baseclassRt testFnNameR"tsortTestMethodsUsingtsort(RR‘R­R³R°R²((RR/s   (RRR>R®tcmpR´RR”R–RšRNR¥R©R(((RRãs  * cCs3tƒ}||_||_|o ||_n|S(N(Rtloadert sortUsingR´R¬R®R”(R¬R¸R”R·((Rt _makeLoaderFs     cCst||ƒi|ƒS(N(R¹R¬R¸RR‘(R‘R¬R¸((RRMsRcCst|||ƒi|ƒS(N(R¹R¬R¸R”R–R‘(R‘R¬R¸R”((RR PscCst|||ƒi|ƒS(N(R¹R¬R¸R”RšR˜(R˜R¬R¸R”((RR Sst_WritelnDecoratorcBs,tZdZd„Zd„Zed„ZRS(s@Used to decorate file-like objects with a handy 'writeln' methodcCs ||_dS(N(tstreamR(RR»((RR]scCst|i|ƒS(N(RBRR»tattr(RR¼((Rt __getattr__`scCs)|o|i|ƒn|idƒdS(Ns (targRtwrite(RR¾((Rtwritelncs(RRR>RR½RNRÀ(((RRº[s   t_TextTestResultcBsjtZdZddZddZd„Zd„Zd„Zd„Zd„Z d „Z d „Z d „Z RS( shA test result class that can print formatted text results to a stream. Used by TextTestRunner. t=iFt-cCsAti|ƒ||_|dj|_|dj|_||_dS(Ni(RRRR»t verbositytshowAlltdotst descriptions(RR»RÇRÄ((RRps   cCs3|io|iƒp t|ƒSn t|ƒSdS(N(RRÇRRORl(RR((RtgetDescriptionws cCsKti||ƒ|io-|ii|i|ƒƒ|iidƒndS(Ns ... (RR RRRÅR»R¿RÈ(RR((RR }s cCsPti||ƒ|io|iidƒn|io|iidƒndS(NRVR›( RR'RRRÅR»RÀRÆR¿(RR((RR'ƒs   cCsSti|||ƒ|io|iidƒn|io|ii dƒndS(NtERRORtE( RR%RRR$RÅR»RÀRÆR¿(RRR$((RR%Šs   cCsSti|||ƒ|io|iidƒn|io|ii dƒndS(NtFAILtF( RR&RRR$RÅR»RÀRÆR¿(RRR$((RR&‘s   cCsO|ip |io|iiƒn|id|iƒ|id|iƒdS(NRÉRË(RRÆRÅR»RÀtprintErrorListRR(R((Rt printErrors˜scCsxxq|D]i\}}|ii|iƒ|iid||i|ƒfƒ|ii|i ƒ|iid|ƒqWdS(Ns%s: %ss%s( RRR$RR»RÀt separator1tflavourRÈt separator2(RRÐRRR$((RRÍžs  #( RRR>RÏRÑRRÈR R'R%R&RÎRÍ(((RRÁhs          cBs5tZdZeiddd„Zd„Zd„ZRS(sÉA test runner class that displays results in textual form. It prints out the names of tests as they are run, errors as they occur, and a summary of the results at the end of the test run. icCs%t|ƒ|_||_||_dS(N(RºR»RRÇRÄ(RR»RÇRÄ((RR¬s cCst|i|i|iƒS(N(RÁRR»RÇRÄ(R((Rt _makeResult±sc Cs[|iƒ}tiƒ}||ƒtiƒ}||}|iƒ|i i |i ƒ|i }|i i d||djodpd|fƒ|i i ƒ|iƒpž|i idƒtt|i|ifƒ\}}|o|i id|ƒn|o3|o|i idƒn|i id|ƒn|i i d ƒn|i i d ƒ|S( s&Run the given test case or test suite.sRan %d test%s in %.3fsitsR,sFAILED (s failures=%ds, s errors=%dt)tOKN(RRÒRRttimet startTimeRtstopTimet timeTakenRÎR»RÀRÑRRWR)R¿R•R(RRtfailedterrored( RRRWRÙRÚRØRRR×RÛ((RRW´s.       1  !(RRR>R]tstderrRRÒRW(((RR¦s  t TestProgramcBsStZdZdZdeeeed„Zed„Zd„Zd„Z d„Z RS(sA command-line program that runs a set of tests; this is primarily for making test modules conveniently executable. s Usage: %(progName)s [options] [test] [...] Options: -h, --help Show this message -v, --verbose Verbose output -q, --quiet Minimal output Examples: %(progName)s - run default set of tests %(progName)s MyTestSuite - run suite 'MyTestSuite' %(progName)s MyTestCase.testSomething - run MyTestCase.testSomething %(progName)s MyTestCase - run all 'test*' test methods in MyTestCase t__main__cCsÚt|ƒtdƒjoFt|ƒ|_x=|idƒdD]}t|i|ƒ|_q<Wn ||_|djo t i}nd|_ ||_ ||_ ||_ tii|dƒ|_|i|ƒ|iƒdS(NR,R›ii(R R˜RžRRLR RBtargvRNR]RÄt defaultTestt testRunnert testLoadertostpathtbasenametprogNamet parseArgstrunTests(RR˜RàRßRáRâR ((RRçs        cCs0|o |GHn|i|iGHtidƒdS(Ni(RbRtUSAGEt__dict__R]texit(RRb((Rt usageExitùs cCsCdk}y|i|dddddgƒ\}}xc|D][\}}|djo|iƒn|djo d |_n|djo d|_q;q;Wt |ƒd jo0|i djo |i i |iƒ|_dSnt |ƒd jo ||_n|i f|_|iƒWn%|ij o}|i|ƒnXdS(NithHvqthelptverbosetquiets-hs-Hs--helps-qs--quietis-vs --verbosei(s-hs-Hs--help(s-qs--quiet(s-vs --verbose(tgetoptRßtoptionsRXtoptR.RRìRÄR(RàRNRâRšR˜Rt testNamest createTeststerrorRb(RRßRóRXR.RbRñRò((RRçþs*      # cCs"|ii|i|iƒ|_dS(N(RRâR©RôR˜R(R((RRõscCsV|idjotd|iƒ|_n|ii|iƒ}ti |i ƒ ƒdS(NRÄ( RRáRNRRÄRWRRRR]RëR)(RRR((RRès( RRR>RéRNRRRìRçRõRè(((RRÝÔs    RÞR˜(ii(ii(%R>t __author__t __email__t __version__RÖR]R6RãRt__all__textendt version_infoRUR*RR t __metaclass__RR8RRRRRRRNR¹R¶RR R RºRÁRRÝRR(RýRRøRRRRÁR8RRùR*RRRÝRR RºR÷R]RRRRRURúR¹R6R RÖRã((Rt?-sB         KØ10\    >.K