Kod źródłowy modułu django.test.runner

importctypesimportitertoolsimportloggingimportmultiprocessingimportosimportpickleimporttextwrapimportunittestimportwarningsfromimportlibimportimport_modulefromioimportStringIOfromdjango.core.managementimportcall_commandfromdjango.dbimportconnectionsfromdjango.testimportSimpleTestCase,TestCasefromdjango.test.utilsimport(setup_databasesas_setup_databases,setup_test_environment,teardown_databasesas_teardown_databases,teardown_test_environment,)fromdjango.utils.datastructuresimportOrderedSetfromdjango.utils.deprecationimportRemovedInDjango21Warningtry:importtblib.pickling_supportexceptImportError:tblib=NoneclassDebugSQLTextTestResult(unittest.TextTestResult):def__init__(self,stream,descriptions,verbosity):self.logger=logging.getLogger('django.db.backends')self.logger.setLevel(logging.DEBUG)super().__init__(stream,descriptions,verbosity)defstartTest(self,test):self.debug_sql_stream=StringIO()self.handler=logging.StreamHandler(self.debug_sql_stream)self.logger.addHandler(self.handler)super().startTest(test)defstopTest(self,test):super().stopTest(test)self.logger.removeHandler(self.handler)ifself.showAll:self.debug_sql_stream.seek(0)self.stream.write(self.debug_sql_stream.read())self.stream.writeln(self.separator2)defaddError(self,test,err):super().addError(test,err)self.debug_sql_stream.seek(0)self.errors[-1]=self.errors[-1]+(self.debug_sql_stream.read(),)defaddFailure(self,test,err):super().addFailure(test,err)self.debug_sql_stream.seek(0)self.failures[-1]=self.failures[-1]+(self.debug_sql_stream.read(),)defaddSubTest(self,test,subtest,err):super().addSubTest(test,subtest,err)iferrisnotNone:self.debug_sql_stream.seek(0)errors=self.failuresifissubclass(err[0],test.failureException)elseself.errorserrors[-1]=errors[-1]+(self.debug_sql_stream.read(),)defprintErrorList(self,flavour,errors):fortest,err,sql_debuginerrors:self.stream.writeln(self.separator1)self.stream.writeln("%s: %s"%(flavour,self.getDescription(test)))self.stream.writeln(self.separator2)self.stream.writeln("%s"%err)self.stream.writeln(self.separator2)self.stream.writeln("%s"%sql_debug)classRemoteTestResult:""" Record information about which tests have succeeded and which have failed. The sole purpose of this class is to record events in the child processes so they can be replayed in the master process. As a consequence it doesn't inherit unittest.TestResult and doesn't attempt to implement all its API. The implementation matches the unpythonic coding style of unittest2. """def__init__(self):iftblibisnotNone:tblib.pickling_support.install()self.events=[]self.failfast=Falseself.shouldStop=Falseself.testsRun=0@propertydeftest_index(self):returnself.testsRun-1def_confirm_picklable(self,obj):""" Confirm that obj can be pickled and unpickled as multiprocessing will need to pickle the exception in the child process and unpickle it in the parent process. Let the exception rise, if not. """pickle.loads(pickle.dumps(obj))def_print_unpicklable_subtest(self,test,subtest,pickle_exc):print("""Subtest failed: test: {} subtest: {}Unfortunately, the subtest that failed cannot be pickled, so the paralleltest runner cannot handle it cleanly. Here is the pickling error:> {}You should re-run this test with --parallel=1 to reproduce the failurewith a cleaner failure message.""".format(test,subtest,pickle_exc))defcheck_picklable(self,test,err):# Ensure that sys.exc_info() tuples are picklable. This displays a# clear multiprocessing.pool.RemoteTraceback generated in the child# process instead of a multiprocessing.pool.MaybeEncodingError, making# the root cause easier to figure out for users who aren't familiar# with the multiprocessing module. Since we're in a forked process,# our best chance to communicate with them is to print to stdout.try:self._confirm_picklable(err)exceptExceptionasexc:original_exc_txt=repr(err[1])original_exc_txt=textwrap.fill(original_exc_txt,75,initial_indent=' ',subsequent_indent=' ')pickle_exc_txt=repr(exc)pickle_exc_txt=textwrap.fill(pickle_exc_txt,75,initial_indent=' ',subsequent_indent=' ')iftblibisNone:print("""{} failed:{}Unfortunately, tracebacks cannot be pickled, making it impossible for theparallel test runner to handle this exception cleanly.In order to see the traceback, you should install tblib: pip install tblib""".format(test,original_exc_txt))else:print("""{} failed:{}Unfortunately, the exception it raised cannot be pickled, making it impossiblefor the parallel test runner to handle it cleanly.Here's the error encountered while trying to pickle the exception:{}You should re-run this test with the --parallel=1 option to reproduce thefailure and get a correct traceback.""".format(test,original_exc_txt,pickle_exc_txt))raisedefcheck_subtest_picklable(self,test,subtest):try:self._confirm_picklable(subtest)exceptExceptionasexc:self._print_unpicklable_subtest(test,subtest,exc)raisedefstop_if_failfast(self):ifself.failfast:self.stop()defstop(self):self.shouldStop=TruedefstartTestRun(self):self.events.append(('startTestRun',))defstopTestRun(self):self.events.append(('stopTestRun',))defstartTest(self,test):self.testsRun+=1self.events.append(('startTest',self.test_index))defstopTest(self,test):self.events.append(('stopTest',self.test_index))defaddError(self,test,err):self.check_picklable(test,err)self.events.append(('addError',self.test_index,err))self.stop_if_failfast()defaddFailure(self,test,err):self.check_picklable(test,err)self.events.append(('addFailure',self.test_index,err))self.stop_if_failfast()defaddSubTest(self,test,subtest,err):# Follow Python 3.5's implementation of unittest.TestResult.addSubTest()# by not doing anything when a subtest is successful.iferrisnotNone:# Call check_picklable() before check_subtest_picklable() since# check_picklable() performs the tblib check.self.check_picklable(test,err)self.check_subtest_picklable(test,subtest)self.events.append(('addSubTest',self.test_index,subtest,err))self.stop_if_failfast()defaddSuccess(self,test):self.events.append(('addSuccess',self.test_index))defaddSkip(self,test,reason):self.events.append(('addSkip',self.test_index,reason))defaddExpectedFailure(self,test,err):# If tblib isn't installed, pickling the traceback will always fail.# However we don't want tblib to be required for running the tests# when they pass or fail as expected. Drop the traceback when an# expected failure occurs.iftblibisNone:err=err[0],err[1],Noneself.check_picklable(test,err)self.events.append(('addExpectedFailure',self.test_index,err))defaddUnexpectedSuccess(self,test):self.events.append(('addUnexpectedSuccess',self.test_index))self.stop_if_failfast()classRemoteTestRunner:""" Run tests and record everything but don't display anything. The implementation matches the unpythonic coding style of unittest2. """resultclass=RemoteTestResultdef__init__(self,failfast=False,resultclass=None):self.failfast=failfastifresultclassisnotNone:self.resultclass=resultclassdefrun(self,test):result=self.resultclass()unittest.registerResult(result)result.failfast=self.failfasttest(result)returnresultdefdefault_test_processes():"""Default number of test processes when using the --parallel option."""# The current implementation of the parallel test runner requires# multiprocessing to start subprocesses with fork().ifmultiprocessing.get_start_method()!='fork':return1try:returnint(os.environ['DJANGO_TEST_PROCESSES'])exceptKeyError:returnmultiprocessing.cpu_count()_worker_id=0def_init_worker(counter):""" Switch to databases dedicated to this worker. This helper lives at module-level because of the multiprocessing module's requirements. """global_worker_idwithcounter.get_lock():counter.value+=1_worker_id=counter.valueforaliasinconnections:connection=connections[alias]settings_dict=connection.creation.get_test_db_clone_settings(str(_worker_id))# connection.settings_dict must be updated in place for changes to be# reflected in django.db.connections. If the following line assigned# connection.settings_dict = settings_dict, new threads would connect# to the default database instead of the appropriate clone.connection.settings_dict.update(settings_dict)connection.close()def_run_subsuite(args):""" Run a suite of tests with a RemoteTestRunner and return a RemoteTestResult. This helper lives at module-level and its arguments are wrapped in a tuple because of the multiprocessing module's requirements. """runner_class,subsuite_index,subsuite,failfast=argsrunner=runner_class(failfast=failfast)result=runner.run(subsuite)returnsubsuite_index,result.eventsclassParallelTestSuite(unittest.TestSuite):""" Run a series of tests in parallel in several processes. While the unittest module's documentation implies that orchestrating the execution of tests is the responsibility of the test runner, in practice, it appears that TestRunner classes are more concerned with formatting and displaying test results. Since there are fewer use cases for customizing TestSuite than TestRunner, implementing parallelization at the level of the TestSuite improves interoperability with existing custom test runners. A single instance of a test runner can still collect results from all tests without being aware that they have been run in parallel. """# In case someone wants to modify these in a subclass.init_worker=_init_workerrun_subsuite=_run_subsuiterunner_class=RemoteTestRunnerdef__init__(self,suite,processes,failfast=False):self.subsuites=partition_suite_by_case(suite)self.processes=processesself.failfast=failfastsuper().__init__()defrun(self,result):""" Distribute test cases across workers. Return an identifier of each test case with its result in order to use imap_unordered to show results as soon as they're available. To minimize pickling errors when getting results from workers: - pass back numeric indexes in self.subsuites instead of tests - make tracebacks picklable with tblib, if available Even with tblib, errors may still occur for dynamically created exception classes such Model.DoesNotExist which cannot be unpickled. """counter=multiprocessing.Value(ctypes.c_int,0)pool=multiprocessing.Pool(processes=self.processes,initializer=self.init_worker.__func__,initargs=[counter])args=[(self.runner_class,index,subsuite,self.failfast)forindex,subsuiteinenumerate(self.subsuites)]test_results=pool.imap_unordered(self.run_subsuite.__func__,args)whileTrue:ifresult.shouldStop:pool.terminate()breaktry:subsuite_index,events=test_results.next(timeout=0.1)exceptmultiprocessing.TimeoutError:continueexceptStopIteration:pool.close()breaktests=list(self.subsuites[subsuite_index])foreventinevents:event_name=event[0]handler=getattr(result,event_name,None)ifhandlerisNone:continuetest=tests[event[1]]args=event[2:]handler(test,*args)pool.join()returnresult

[dokumentacja]@classmethoddefadd_arguments(cls,parser):parser.add_argument('-t','--top-level-directory',action='store',dest='top_level',default=None,help='Top level of project for unittest discovery.',)parser.add_argument('-p','--pattern',action='store',dest='pattern',default="test*.py",help='The test matching pattern. Defaults to test*.py.',)parser.add_argument('-k','--keepdb',action='store_true',dest='keepdb',help='Preserves the test DB between runs.')parser.add_argument('-r','--reverse',action='store_true',dest='reverse',help='Reverses test cases order.',)parser.add_argument('--debug-mode',action='store_true',dest='debug_mode',help='Sets settings.DEBUG to True.',)parser.add_argument('-d','--debug-sql',action='store_true',dest='debug_sql',help='Prints logged SQL queries on failure.',)parser.add_argument('--parallel',dest='parallel',nargs='?',default=1,type=int,const=default_test_processes(),metavar='N',help='Run tests using up to N parallel processes.',)parser.add_argument('--tag',action='append',dest='tags',help='Run only tests with the specified tag. Can be used multiple times.',)parser.add_argument('--exclude-tag',action='append',dest='exclude_tags',help='Do not run tests with the specified tag. Can be used multiple times.',

[dokumentacja]defbuild_suite(self,test_labels=None,extra_tests=None,**kwargs):suite=self.test_suite()test_labels=test_labelsor['.']extra_tests=extra_testsor[]discover_kwargs={}ifself.patternisnotNone:discover_kwargs['pattern']=self.patternifself.top_levelisnotNone:discover_kwargs['top_level_dir']=self.top_levelforlabelintest_labels:kwargs=discover_kwargs.copy()tests=Nonelabel_as_path=os.path.abspath(label)# if a module, or "module.ClassName[.method_name]", just run thoseifnotos.path.exists(label_as_path):tests=self.test_loader.loadTestsFromName(label)elifos.path.isdir(label_as_path)andnotself.top_level:# Try to be a bit smarter than unittest about finding the# default top-level for a given directory path, to avoid# breaking relative imports. (Unittest's default is to set# top-level equal to the path, which means relative imports# will result in "Attempted relative import in non-package.").# We'd be happy to skip this and require dotted module paths# (which don't cause this problem) instead of file paths (which# do), but in the case of a directory in the cwd, which would# be equally valid if considered as a top-level module or as a# directory path, unittest unfortunately prefers the latter.top_level=label_as_pathwhileTrue:init_py=os.path.join(top_level,'__init__.py')ifos.path.exists(init_py):try_next=os.path.dirname(top_level)iftry_next==top_level:# __init__.py all the way down? give up.breaktop_level=try_nextcontinuebreakkwargs['top_level_dir']=top_levelifnot(testsandtests.countTestCases())andis_discoverable(label):# Try discovery if path is a package or directorytests=self.test_loader.discover(start_dir=label,**kwargs)# Make unittest forget the top-level dir it calculated from this# run, to support running tests from two different top-levels.self.test_loader._top_level_dir=Nonesuite.addTests(tests)fortestinextra_tests:suite.addTest(test)ifself.tagsorself.exclude_tags:suite=filter_tests_by_tags(suite,self.tags,self.exclude_tags)suite=reorder_suite(suite,self.reorder_by,self.reverse)ifself.parallel>1:parallel_suite=self.parallel_test_suite(suite,self.parallel,self.failfast)# Since tests are distributed across processes on a per-TestCase# basis, there's no need for more processes than TestCases.parallel_units=len(parallel_suite.subsuites)ifself.parallel>parallel_units:self.parallel=parallel_units# If there's only one TestCase, parallelization isn't needed.ifself.parallel>1:suite=parallel_suite

[dokumentacja]defteardown_databases(self,old_config,**kwargs):"""Destroy all the non-mirror databases."""_teardown_databases(old_config,verbosity=self.verbosity,parallel=self.parallel,keepdb=self.keepdb,

[dokumentacja]defrun_tests(self,test_labels,extra_tests=None,**kwargs):""" Run the unit tests for all the test labels in the provided list. Test labels should be dotted Python paths to test modules, test classes, or test methods. A list of 'extra' tests may also be provided; these tests will be added to the test suite. Return the number of tests that failed. """self.setup_test_environment()suite=self.build_suite(test_labels,extra_tests)old_config=self.setup_databases()self.run_checks()result=self.run_suite(suite)self.teardown_databases(old_config)self.teardown_test_environment()

returnself.suite_result(suite,result)defis_discoverable(label):""" Check if a test label points to a python package or file directory. Relative labels like "." and ".." are seen as directories. """try:mod=import_module(label)except(ImportError,TypeError):passelse:returnhasattr(mod,'__path__')returnos.path.isdir(os.path.abspath(label))defreorder_suite(suite,classes,reverse=False):""" Reorder a test suite by test type. `classes` is a sequence of types All tests of type classes[0] are placed first, then tests of type classes[1], etc. Tests with no match in classes are placed last. If `reverse` is True, sort tests within classes in opposite order but don't reverse test classes. """class_count=len(classes)suite_class=type(suite)bins=[OrderedSet()foriinrange(class_count+1)]partition_suite_by_type(suite,classes,bins,reverse=reverse)reordered_suite=suite_class()foriinrange(class_count+1):reordered_suite.addTests(bins[i])returnreordered_suitedefpartition_suite_by_type(suite,classes,bins,reverse=False):""" Partition a test suite by test type. Also prevent duplicated tests. classes is a sequence of types bins is a sequence of TestSuites, one more than classes reverse changes the ordering of tests within bins Tests of type classes[i] are added to bins[i], tests with no match found in classes are place in bins[-1] """suite_class=type(suite)ifreverse:suite=reversed(tuple(suite))fortestinsuite:ifisinstance(test,suite_class):partition_suite_by_type(test,classes,bins,reverse=reverse)else:foriinrange(len(classes)):ifisinstance(test,classes[i]):bins[i].add(test)breakelse:bins[-1].add(test)defpartition_suite_by_case(suite):"""Partition a test suite by test case, preserving the order of tests."""groups=[]suite_class=type(suite)fortest_type,test_groupinitertools.groupby(suite,type):ifissubclass(test_type,unittest.TestCase):groups.append(suite_class(test_group))else:foritemintest_group:groups.extend(partition_suite_by_case(item))returngroupsdefsetup_databases(*args,**kwargs):warnings.warn('`django.test.runner.setup_databases()` has moved to ''`django.test.utils.setup_databases()`.',RemovedInDjango21Warning,stacklevel=2,)return_setup_databases(*args,**kwargs)deffilter_tests_by_tags(suite,tags,exclude_tags):suite_class=type(suite)filtered_suite=suite_class()fortestinsuite:ifisinstance(test,suite_class):filtered_suite.addTests(filter_tests_by_tags(test,tags,exclude_tags))else:test_tags=set(getattr(test,'tags',set()))test_fn_name=getattr(test,'_testMethodName',str(test))test_fn=getattr(test,test_fn_name,test)test_fn_tags=set(getattr(test_fn,'tags',set()))all_tags=test_tags.union(test_fn_tags)matched_tags=all_tags.intersection(tags)if(matched_tagsornottags)andnotall_tags.intersection(exclude_tags):filtered_suite.addTest(test)returnfiltered_suite