3 \V@sdZddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z m Z m Z dZ ejdejZGdd d e jZd d Zd d ZddZddZddZGdddeZeZdddZe jfddZde je jfddZde je jfddZ dS) zLoading unittests.N)fnmatch)casesuiteutilTz[_a-z]\w*\.py$cs,eZdZdZfddZfddZZS) _FailedTestNcs||_tt|j|dS)N) _exceptionsuperr__init__)selfZ method_name exception) __class__'/usr/lib64/python3.6/unittest/loader.pyr sz_FailedTest.__init__cs*|jkrttj|Sfdd}|S)Ncs jdS)N)rr)r rr testFailure!sz,_FailedTest.__getattr__..testFailure)_testMethodNamer r __getattr__)r namer)r )r rrs  z_FailedTest.__getattr__)__name__ __module__ __qualname__rr r __classcell__rr)r rrs rcCs"d|tjf}t|t|||S)Nz#Failed to import test module: %s %s) traceback format_exc_make_failed_test ImportError)r suiteClassmessagerrr_make_failed_import_test&srcCsdtjf}t||||S)NzFailed to call load_tests: %s)rrr)rr rrrrr_make_failed_load_tests+srcCst||}||f|fS)N)r) methodnamer rrtestrrrr0s rcCs<tjt|dd}||i}tdtjf|}|||fS)NcSsdS)Nr)r rrr testSkipped5sz'_make_skipped_test..testSkippedZ ModuleSkipped)rskipstrtypeTestCase)r r rr"attrsZ TestClassrrr_make_skipped_test4sr(cCs*|jjdr|ddStjj|dS)Nz $py.class ri)lowerendswithospathsplitext)r-rrr_jython_aware_splitext<s r/cseZdZdZdZeejZe j Z dZ fddZ ddZddd d Zd!d d Zd"d dZddZd#ddZddZddZddZddZd$ddZd%dd ZZS)& TestLoaderz This class is responsible for loading tests according to various criteria and returning them wrapped in a TestSuite r!Ncs tt|jg|_t|_dS)N)r r0r errorsset_loading_packages)r )r rrr LszTestLoader.__init__cCsHt|tjrtd|j|}| r4t|dr4dg}|jt||}|S)z;Return a suite of all test cases contained in testCaseClasszYTest cases should not be derived from TestSuite. Maybe you meant to derive from TestCase?ZrunTest) issubclassr TestSuite TypeErrorgetTestCaseNameshasattrrmap)r testCaseClassZ testCaseNamesZ loaded_suiterrrloadTestsFromTestCaseSs  z TestLoader.loadTestsFromTestCase)patternc Os2t|dksd|kr,tjdt|jddt|dkrRt|d}tdj|t|dkrxt|d}tdj|g}x@t|D]4}t ||}t |t rt |t jr|j|j|qWt |dd} |j|}| dk r.y | |||Stk r,} z$t|j| |j\} } |jj| | Sd} ~ XnX|S) z>Return a suite of all test cases contained in the given modulerZuse_load_testsz(use_load_tests is deprecated and ignoredNrzCloadTestsFromModule() takes 1 positional argument but {} were givenz=loadTestsFromModule() got an unexpected keyword argument '{}' load_tests)lenwarningswarnDeprecationWarningpopr6formatsorteddirgetattr isinstancer%r4rr&appendr;r Exceptionrrr1) r moduler<argsZkwsZ complainttestsrobjr=e error_case error_messagerrrloadTestsFromModuleas4           zTestLoader.loadTestsFromModulecCs>|jd}d\}}|dkr|dd}xb|rydj|}t|}PWq(tk r|j}t||j\}}|s|jj||SYq(Xq(W|dd}|} x|D]} y| t | | } } Wqt k r2} z\t | dddk r|dk r|jj||St | | |jdt j f\}}|jj||SWYdd} ~ XqXqWt| tjrP|j| St| trtt| tjrt|j| St| tjrt| trt| tjr|d }| |} tt | |tjs|j| gSnt| tjr| St| r.| }t|tjr|St|tjr|j|gStd| |fn td| dS) aSReturn a suite of all test 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. .Nr__path__zFailed to access attribute: %sz"calling %s returned %s, not a testz$don't know how to make test from: %s)NN)splitjoin __import__rrBrrr1rHrFAttributeErrorrrrrGtypes ModuleTyperQr%r4rr&r; FunctionTyperr5callabler6)r rrJpartsrOrPZ parts_copy module_nameZnext_attributerMpartparentrNZinstr!rrrloadTestsFromNamesl             zTestLoader.loadTestsFromNamecsfdd|D}j|S)zReturn a suite of all test cases found using the given sequence of string specifiers. See 'loadTestsFromName()'. csg|]}j|qSr)ra).0r)rJr rr sz1TestLoader.loadTestsFromNames..)r)r namesrJZsuitesr)rJr rloadTestsFromNamesszTestLoader.loadTestsFromNamescCs@||jfdd}tt|t|}|jr<|jtj|jd|S)zLReturn a sorted sequence of method names found within testCaseClass cSs|j|ott||S)N) startswithr\rF)Zattrnamer:prefixrrr isTestMethods z1TestLoader.getTestCaseNames..isTestMethod)key)testMethodPrefixlistfilterrEsortTestMethodsUsingsort functools cmp_to_key)r r:rhZ testFnNamesrrrr7s zTestLoader.getTestCaseNamestest*.pyc CsRd}|dkr|jdk r|j}n|dkr.d}|}tjj|}|tjkrRtjjd|||_d}d}g}tjjtjj|rtjj|}||krtjjtjj|d }nxy t |Wnt k rd}YnRXtj |}|j dd} ytjjtjj |j}Wntk ry |j} Wntk r8d} YnX| r| jdkr| jdk rd}x|jD]T} | r| j| rqd| j |jjdtjjd|_|j|j| |ddqdWn*|jtjkrtddntd j|dYnX|r |s|j| |_tjj|n tjj||r2t d ||sHt|j||}|j |S) a%Find and return all test modules from the specified start directory, recursing into subdirectories to find them and return all tests found within them. Only test files that match the pattern will be loaded. (Using shell style pattern matching.) All test modules must be importable from the top level of the project. If the start directory is not the top level directory then the top level directory must be specified separately. If a test package name (directory with '__init__.py') matches the pattern then the package will be checked for a 'load_tests' function. If this exists then it will be called with (loader, tests, pattern) unless the package has already had load_tests called from the same discovery invocation, in which case the package module object is not scanned for tests - this ensures that when a package uses discover to further discover child tests that infinite recursion does not happen. If load_tests exists then discovery does *not* recurse into the package, load_tests is responsible for loading all tests in the package. The pattern is deliberately not stored as a loader attribute so that packages can continue discovery themselves. top_level_dir is stored so load_tests does not need to pass this argument in to loader.discover(). Paths are sorted before being imported to ensure reproducible execution order even on filesystems with non-alphabetical ordering like ext3/4. FNTrz __init__.pyrR) namespacez2Can not use builtin modules as dotted module namesz$don't know how to discover from {!r}z%Start directory is not importable: %r)!_top_level_dirr,r-abspathsysinsertisdirisfilerVrWrmodulesrUdirname__file__rX__spec__loadersubmodule_search_locationsrSrfrreplacesepextend _find_testsbuiltin_module_namesr6rC _get_directory_containing_moduleremoverkr) r start_dirr<Z top_level_dirZset_implicit_topZis_not_importable is_namespacerLZ the_moduleZtop_partspecr-rrrdiscoversv               zTestLoader.discovercCsRtj|}tjj|j}tjj|jjdrBtjj tjj |Stjj |SdS)Nz __init__.py) ruryr,r-rtr{basenamer*rfrz)r r^rJ full_pathrrrrXs  z+TestLoader._get_directory_containing_modulecCsl||jkrdSttjj|}tjj||j}tjj| sDtd|jd sXtd|j tjj d}|S)NrRzPath must be within the projectz..) rsr/r,r-normpathrelpathisabsAssertionErrorrfrr)r r-Z_relpathrrrr_get_name_from_pathds zTestLoader._get_name_from_pathcCst|tj|S)N)rWrury)r rrrr_get_module_from_namepsz TestLoader._get_module_from_namecCs t||S)N)r)r r-rr<rrr _match_pathtszTestLoader._match_pathFc cs|j|}|dkrD||jkrD|j|||\}}|dk r<|V|sDdSttj|}x||D]t}tjj||} |j| ||\}}|dk r|V|rX|j| }|jj|z|j | ||EdHWd|jj |XqXWdS)z/Used by discovery. Yields test suites it loads.rRN) rr3_find_test_pathrDr,listdirr-rVaddrdiscard) r rr<rrrrLZshould_recursepathsr-rrrrrxs*    zTestLoader._find_testscCsPtjj|}tjj|rFtj|s(d S|j|||s:d S|j|}y|j|}WnXt j k r}zt |||j dfSd}~Xnt ||j \}} |jj| |dfStjjt|d|} ttjj| } ttjj|} | j| jkr0tjj| } ttjj|}tjj|}d}t||| |f|j||ddfSntjj|rH| rztjjtjj|d rzd Sd}d}|j|}y|j|}WnZt j k r}zt |||j dfSd}~Xnzt ||j \}} |jj| |dfSt|dd}|jj|z(|j||d}|dk r0|dfS|dfS|jj|Xnd SdS) zUsed by discovery. Loads tests from a single file, or a directories' __init__.py when passed the directory. Returns a tuple (None_or_tests_from_file, should_recurse). NFr{zW%r module incorrectly imported from %r. Expected %r. Is this module globally installed?)r<z __init__.pyr=T)NF)NF)NF)NF)r,r-rrxVALID_MODULE_NAMEmatchrrrrZSkipTestr(rrr1rHrtrFr/realpathr*rzrrQrwrVr3rr)r rr<rrrrrJrNrOrPZmod_filerZfullpath_noextZ module_dirZmod_nameZ expected_dirmsgr=rLpackagerrrrsl           zTestLoader._find_test_path)N)N)rqN)F)F)rrr__doc__rj staticmethodr three_way_cmprmrr5rrsr r;rQrarer7rrrrrrrrrr)r rr0Bs$  ( N  n   "r0cCs t}||_||_|r||_|S)N)r0rmrjr)rg sortUsingrr}rrr _makeLoaders rcCst||j|S)N)rr7)r:rgrrrrr7sr7r!cCst|||j|S)N)rr;)r:rgrrrrr makeSuites rcCst|||j|S)N)rrQ)rJrgrrrrr findTestCasess r)N)!rr,rerurrYror?rrrrZ __unittestcompile IGNORECASErr&rrrrr(r/objectr0ZdefaultTestLoaderrrr7r5rrrrrrs8 '