3 \8)@sldZddlZddlZddlZddlmZmZddlmZdZ dZ dZ d d Z d d Z Gd ddeZeZdS)zUnittest main programN)loaderrunner)installHandlerTaExamples: %(prog)s test_module - run tests from test_module %(prog)s module.TestClass - run tests from module.TestClass %(prog)s module.Class.test_method - run specified test method %(prog)s path/to/test_file.py - run tests from test_file.py aFExamples: %(prog)s - run default set of tests %(prog)s MyTestSuite - run suite 'MyTestSuite' %(prog)s MyTestCase.testSomething - run MyTestCase.testSomething %(prog)s MyTestCase - run all 'test*' test methods in MyTestCase cCsxtjj|rt|jjdrttjj|rXtjj|tj}tjj|sP|jtj rT|S|}|ddj ddj ddS|S)Nz.py\./) ospathisfilelowerendswithisabsrelpathgetcwd startswithpardirreplace)nameZrel_pathr/usr/lib64/python3.6/main.py _convert_names rcCsdd|DS)NcSsg|] }t|qSr)r).0rrrr .sz"_convert_names..r)namesrrr_convert_names-src @seZdZdZdZdZdZZZZ Z dZ dddde j ddddddf dddd Zdd d Zd d ZddZddZddZddZddZddZdddZddZdS) TestProgramzA command-line program that runs a set of tests; this is primarily for making test modules conveniently executable. Nr__main__TF) tb_localsc Cst|tr@t||_x0|jdddD]} t|j| |_q(Wn||_|dkrTtj}||_||_ | |_ ||_ | |_ | |_ | dkrtj rd|_n| |_||_||_||_tjj|d|_|j||jdS)Nrrdefaultr) isinstancestr __import__modulesplitgetattrsysargvexitfailfast catchbreak verbositybufferr warnoptionswarnings defaultTest testRunner testLoaderr r basenameprogName parseArgsrunTests)selfr%r1r)r2r3r*r-r+r,r.r0r partrrr__init__;s,   zTestProgram.__init__cCs4|r t||jdkr|j|jtjddS)N)print_discovery_parser_initArgParsers _print_helpr(r*)r8msgrrr usageExitas  zTestProgram.usageExitcOsZ|jdkr6t|jjttd|ji|jjn t|jjttd|jidS)Nprog) r%r< _main_parserZ format_help MAIN_EXAMPLESr5r= print_helpMODULE_EXAMPLES)r8argskwargsrrrr?is   zTestProgram._print_helpcCs|j|jdkrpt|dkrD|djdkrD|j|dddS|jj|dd||js|jgdSn|jj|dd||jrt|j|_ t dkrd|_n6|j dkrd|_ n$t |j t r|j f|_ n t|j |_ |jdS)Nrdiscoverr;r)r>r%lenr _do_discoveryrC parse_argstestsr testNames__name__r1r"r#list createTests)r8r)rrrr6rs(       zTestProgram.parseArgscCs4|jdkr|jj|j|_n|jj|j|j|_dS)N)rNr3ZloadTestsFromModuler%testZloadTestsFromNames)r8rrrrQs  zTestProgram.createTestscCs$|j}|j||_|j||_dS)N)_getParentArgParser_getMainArgParserrC_getDiscoveryArgParserr=)r8Z parent_parserrrrr>s zTestProgram._initArgParserscCstjdd}|jddddddd |jd d ddd d d |jddddd|jdkrn|jddddddd|_|jdkr|jddddddd|_|jdkr|jddddddd|_|S)NF)Zadd_helpz-vz --verboser- store_constr;zVerbose output)destactionconsthelpz-qz--quietrz Quiet outputz--localsr store_truez"Show local variables in tracebacks)rWrXrZz-fz --failfastr+zStop on first fail or errorz-cz--catchr,z'Catch Ctrl-C and display results so farz-bz--bufferr.z%Buffer stdout and stderr during tests)argparseArgumentParser add_argumentr+r,r.)r8parserrrrrSs4         zTestProgram._getParentArgParsercCs2tj|gd}|j|_|j|_|jdddd|S)N)parentsrM*z?a list of any number of test modules, classes and test methods.)nargsrZ)r\r]r5rBr?rEr^)r8parentr_rrrrTs zTestProgram._getMainArgParsercCs~tj|gd}d|j|_d|_|jddddd|jd d d d d|jd ddddx"dD]}|j|dtjtjdq\W|S)N)r`z %s discoverzcFor test discovery all test modules must be importable from the top level directory of the project.z-sz--start-directorystartz*Directory to start discovery ('.' default))rWrZz-pz --patternpatternz+Pattern to match tests ('test*.py' default)z-tz--top-level-directorytopzrLr3rIrR)r8r)LoaderrrrrrKs zTestProgram._do_discoverycCs|jr t|jdkrtj|_t|jtryVy"|j|j|j|j |j |j d}Wn.t k r||j|j|j|j |j d}YnXWqt k r|j}YqXn|j}|j |j|_|jrtj|jj dS)N)r-r+r.r0r )r-r+r.r0)r,rr2rZTextTestRunnerr"typer-r+r.r0r TypeErrorZrunrRresultr*r(Z wasSuccessful)r8r2rrrr7s.  zTestProgram.runTests)N)N)rO __module__ __qualname____doc__r%r-r+r,r.r5r0r=rZdefaultTestLoaderr:rAr?r6rQr>rSrTrUrKr7rrrrr1s&#    r)ror(r\r rrZsignalsrZ __unittestrDrFrrobjectrmainrrrrs  T