B t `c@sDdZddlZddlZddlZddlZddlZddlmZddlm Z m Z ddl m Z m Z ddlmZddlmZddlmZmZmZdd lmZmZdd lmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#dd l$m%Z%m&Z&m'Z'dd lm(Z(m)Z)ddl*Z*e+e,Z-ej.j/Z0ej.j1Z2ej.j3Z4ej.j5Z6ej.j7Z8d dgZ9Gdd d ej:Z:e:Z;dS)z Test Loader ----------- nose's test loader implements the same basic functionality as its superclass, unittest.TestLoader, but extends it by more liberal interpretations of what may be a test and how a test may be named. N) isfunction)unbound_methodismethod)FunctionTestCaseMethodTestCase)Failure)Config)Importeradd_path remove_path)defaultSelector TestAddress) func_lineno getpackageisclass isgenerator ispackageregex_last_key resolve_nametransplant_functransplant_class test_address)ContextSuiteFactory ContextList LazySuite) sort_list cmp_to_key TestLoaderdefaultTestLoadercseZdZdZdZdZdZdZdZd&ddZ ddZ ddZ d d Z d d Z d dZddZddZd'ddZd(ddZd)ddZfddZddZd*ddZd+d d!Zd"d#Zd$d%ZZS),raATest loader that extends unittest.TestLoader to: * Load tests from test-like functions and classes that are not unittest.TestCase subclasses * Find and load test modules in a directory * Support tests that are generators * Support easy extensions of or changes to that behavior through plugins NcCs|dkrt}|dkr t|d}|dkr.|j}|dkr@t|}nt|rP||}||_||_tt||_||_ |j rt ||t |d|_ tg|_tj|dS)aInitialize a test loader. Parameters (all optional): * config: provide a `nose.config.Config`_ or other config class instance; if not provided a `nose.config.Config`_ with default values is used. * importer: provide an importer instance that implements `importFromPath`. If not provided, a `nose.importer.Importer`_ is used. * workingDir: the directory to which file and module names are relative. If not provided, assumed to be the current working directory. * selector: a selector class or instance. If a class is provided, it will be instantiated with one argument, the current config. If not provided, a `nose.selector.Selector`_ is used. N)config)rr workingDirr rrimporter op_normpath op_abspathselectoraddPathsr r suiteClassset _visitedPathsunittestr__init__)selfrr!r r$r,//tmp/pip-unpacked-wheel-cjhnoqsi/nose/loader.pyr*<s&     zTestLoader.__init__cCsf|jjrtj||S||jfdd}tt|t|}|sLt |drLdg}|j rbt |t |j |S)z_Override to select with selector, unless config.getTestCaseNamesCompat is True cSs6t||d}t|r t||}n t|s,dS||S)NF)getattrrrr wantMethod)attrclsselitemr,r,r-wantedms   z+TestLoader.getTestCaseNames..wantedZrunTest) rZgetTestCaseNamesCompatr)rgetTestCaseNamesr$listfilterdirhasattrZsortTestMethodsUsingrr)r+ testCaseClassr4casesr,r,r-r5fszTestLoader.getTestCaseNamescCs|dkr dS||jkS)NF)r()r+pathr,r,r- _haveVisited~szTestLoader._haveVisitedcCs|dk r|j|dS)N)r(add)r+r<r,r,r-_addVisitedPathszTestLoader._addVisitedPathc #std|jj}||jjr2t|j}t|}t |t jj x|D]}| drfqVt t||t}d}|rd}j}n$t}|r| drqVj}t} |rVsV|r||drjddVn V|qV| r4jddVqVfdd VqVWg} x||D]} | | q^Wy| r| VWn<tt fk rYn"t!t"#gVYnXjjrx|D]} t$| qW|%|d S) zLoad tests from the directory at path. This is a generator -- each suite of tests from a module or other file is yielded and is expected to be executed before the next file is examined. zload from dir %s.F_z.pyT) discoveredcs S)N)loadTestsFromDirr,) entry_pathr+r,r-z-TestLoader.loadTestsFromDir..N)&logdebugrpluginsZbeforeDirectoryr%r oslistdirrrZ testMatch startswithr#op_join op_isfiler$ZwantFileop_isdirZ wantDirectoryrr=r?Z beforeContextendswithloadTestsFromNameloadTestsFromFileZ afterContextr&rCappendKeyboardInterrupt SystemExitrsysexc_infor ZafterDirectory) r+r<rIZ paths_addedentriesentryis_filer4is_dir is_packageteststestpr,)rDr+r-rCsd              zTestLoader.loadTestsFromDirc Cstd|yRdd|jj|D}|rBdd|D}||St|dtd|WnPt t fk rxYn8t }|t |d|d|d |d d fd gSd S) zLoad tests from a non-module file. Default is to raise a ValueError; plugins may implement `loadTestsFromFile` to provide a list of tests loaded from the file. zLoad from non-module file %scSsg|]}|qSr,r,).0r^r,r,r- sz0TestLoader.loadTestsFromFile..cSsg|] }|r|qSr,r,)r`_fr,r,r-rasrz!Unable to load tests from file %srN)address)rGrHrrIrRr&openclose ValueErrorrTrUrVrWr)r+filenamer]excr,r,r-rRs"   zTestLoader.loadTestsFromFilecs$|ffdd }j|ddS)zLazy-load tests from a generator function. The generator function may yield either: * a callable, or * a function name resolvable within the same module c 3syHxB|D]8}|\}}t|tjs2t||}t|||dVq WWnJtk r^Yn6t}t |d|d|dt dVYnXdS)N)arg descriptorrrdre)rf) parseGeneratedTest isinstance collectionsCallabler.rrTrVrWrr)gmr^ test_funcrlrk) generatorr+r,r-generates   z3TestLoader.loadTestsFromGenerator..generateF)context can_split)r&)r+rumodulervr,)rur+r-loadTestsFromGenerators z!TestLoader.loadTestsFromGeneratorcsLtdrjj}|}j}t|||ffdd }j|ddS)zLazy-load tests from a generator method. This is more complicated than loading from a generator function, since a generator method may yield: * a function * a bound or unbound method, or * a method name Zim_classc 3syx|D]v}|\}}t|tjs8t|t||}t|rRt|||dVq t|tjrpt|||dVq tt d|Vq WWnJt k rYn6t }t|d|d|dt dVYnXdS)N)rlrm)r^rlz%s is not a callable or methodrrdre)rf)rnrorprqrr.rrr TypeErrorrTrVrWr)rrcr^rtrlrk)rur+r,r-rvs$   z9TestLoader.loadTestsFromGeneratorMethod..generateF)rwrx)r9__self__ __class____name__r.r&)r+rur1instmethodrvr,)rur+r-loadTestsFromGeneratorMethod s  z'TestLoader.loadTestsFromGeneratorMethodFc stdg}g}g}|r(jrxZtD]N}t|d}t|rbj|r||q2t |r2j |r2||q2Wt |ddt |t fdd||D}tdg} |rt jt j|}x| D]~} td| td |t j| t jt j| jjs@|r@t jt j| |rt j| r|| qWx$jj|D]}||qrWt|d S) zLoad all tests from module and return a suite containing them. If the module has been discovered and is not test-like, the suite will be empty by default, though plugins may add their own tests. zLoad from module %sNcSs|jS)N)r)xr,r,r-rEKrFz0TestLoader.loadTestsFromModule..csg|]}j|dqS))parent)makeTest)r`t)ryr+r,r-raMsz2TestLoader.loadTestsFromModule..__path__zLoad tests from module path %s?z!path: %s os.path.realpath(%s): %s)rw)rGrHr$Z wantModuler8r.rZ wantClassrSrZ wantFunctionrrrJr<normcaserealpathrZtraverseNamespacerLisdirextendrCrIloadTestsFromModuler&r) r+ryr<rBr]Z test_classesZ test_funcsr3r^Z module_paths module_pathr,)ryr+r-r5s>          zTestLoader.loadTestsFromModulec std||j}jj||}|r0||St|jd}|r|jrN|j} ||\}}t |rt |dd|j krt |tst||j }t ||j }td|||t |tr||gS|t||g|dSn|jry\|jdkrt|j}nBjj|j|jzj|j|j}Wdjj|j|jXWnNttfk r\Yn4t} |t| d| d| d |d gS|jr|j|Sj||j|d Sn|jrb|j|jrt} | dkr|tt d |jf|d gSj|j| d St!r0t"fddSt#rD$S|tt%d|d gSn|tt d||d gSdS)zLoad tests from the entity with the given name. The name may indicate a file, directory, module, or any object within a module. See `nose.util.split_test_name` for details on test name parsing. zload from %s (%s))r __module__Nzparent %s obj %s module %s)rwrrdre)rf)rBz>Can't find callable %s in file %s: file is not a python module)rycs S)N)rCr,)r<r+r,r-rErFz.TestLoader.loadTestsFromName..zNo such file %szUnresolvable test name %s)&rGrHr&rrIrQr r callresolverr.rrorrrrryrjrZ beforeImportr!ZimportFromPathZ afterImportrTrUrVrWZtotuplerrrirOrrNrROSError) r+nameryrBsuite plug_testsaddrrobjrkpackager,)r<r+r-rQis                 zTestLoader.loadTestsFromNamecCsP|jj||}|r@|\}}|r@|||tj|||gStj|||S)zSLoad tests from all names, returning a suite containing all tests. )rrIloadTestsFromNamesr&r)r)r+namesryZplug_resrr,r,r-rszTestLoader.loadTestsFromNamescshg}|jj}x||D]}||qW|s>tt||S|ddtt||D||S)z6Load tests from a unittest.TestCase subclass. cSsg|]}|qSr,r,)r`caser,r,r-rasz4TestLoader.loadTestsFromTestCase..)rrIloadTestsFromTestCaserSsuperrrr&)r+r:r;rIr)r~r,r-rsz TestLoader.loadTestsFromTestCasecsbjfdd}fddt|tD}x jjD]}||q>Wt|dS)a!Load tests from a test class that is *not* a unittest.TestCase subclass. In this case, we can't depend on the class's `__init__` taking method name arguments, so we have to compose a MethodTestCase for each method in the class that looks testlike. cSs6t||d}t|r t||}n t|s,dS||S)NF)r.rrrr/)r0r1r2r3r,r,r-r4s   z1TestLoader.loadTestsFromTestClass..wantedcsg|]}t|qSr,)rr.)r`r)r1r+r,r-rasz5TestLoader.loadTestsFromTestClass..)rw) r$r7r8rrIloadTestsFromTestClassrSr&r)r+r1r4r;r^r,)r1r+r-rs  z!TestLoader.loadTestsFromTestClassc Csy |||Sttfk r&Yn\t}y t|}Wn$tk rVYnd}YnXt|d|d|d|dSdS)Nrrdre)rf) _makeTestrTrUrVrWrr)r+rrrkrr,r,r-r s   zTestLoader.makeTestc Csg}y t|}Wn$tk r&Ynd}YnXx"|jj||D]}||qHWy|rj||SWnBttfk rYn*t }t |d|d|d|dSt |r|rt |t jst||}t |tjr|St|r0|r |j|jkr t||j}t|tjr$||S||Snt|r|dkrJ|j}t|tjrb||jSt|rx|||St|Sn\t |r|r|j|jkrt||j}t|r|||St |Snt t!d||dSdS)zVGiven a test object and its parent, return a test case or test suite. Nrrdre)rfzCan't make a test from %s)"rrTrrIrrSr&rUrVrWrrrotypes ModuleTyperr)ZTestCaserrrr issubclassrrrr~rrrrrzrr{)r+rrrrr^rkr,r,r-rsX                   zTestLoader._makeTestcCsL|}|d}x|D]}|t||d}}qW|dkrDttd|}||fS)z#Resolve name within module r@NzNo such test %s)splitr.rri)r+rryrpartspartrr,r,r-rSs  zTestLoader.resolvecCsdt|ts|t}}nDt|dkr6|dt}}n&t|dksFt|d|dd}}||fS)zGiven the yield value of a test generator, return a func and args. This is used in the two loadTestsFromGenerator* methods. rdrN)rotuplelenAssertionError)r+r^rtrlr,r,r-rn_s  zTestLoader.parseGeneratedTest)NNNN)NF)NF)N)N)N)rr __qualname____doc__rr!r r$r&r*r5r=r?rCrRrzrrrQrrrrrrrn __classcell__r,r,)r~r-r-s0 )M, 4 l    ; )s:   0 E