def run_tests(*args, **kwargs): """Custom test runner. Follows the django.test.simple.run_tests() interface.""" coverage.use_cache(0) # Do not cache any of the coverage.py stuff coverage.start() test_results = django_test_runner(*args, **kwargs) # Stop code coverage after tests have completed coverage.stop() # Print code metrics header print '' print '----------------------------------------------------------------------' print ' Unit Test Code Coverage Results' print '----------------------------------------------------------------------' # Report code coverage metrics coverage_modules = [] for module in settings.COVERAGE_MODULES: coverage_modules.append(__import__(module, globals(), locals(), [''])) coverage.report(coverage_modules, show_missing=1) # Print code metrics footer print '----------------------------------------------------------------------' return test_results
def run(self): testfiles = [] testdirs = ["koan"] for d in testdirs: testdir = os.path.join(os.getcwd(), "tests", d) for t in _glob.glob(os.path.join(testdir, '*.py')): if t.endswith('__init__.py'): continue testfile = '.'.join( ['tests', d, os.path.splitext(os.path.basename(t))[0]]) testfiles.append(testfile) tests = unittest.TestLoader().loadTestsFromNames(testfiles) runner = unittest.TextTestRunner(verbosity=1) if coverage: coverage.erase() coverage.start() result = runner.run(tests) if coverage: coverage.stop() sys.exit(int(bool(len(result.failures) > 0 or len(result.errors) > 0)))
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ Custom test runner. Follows the django.test.simple.run_tests() interface. """ # Start code coverage before anything else if necessary if hasattr(settings, 'COVERAGE_MODULES'): coverage.use_cache(0) # Do not cache any of the coverage.py stuff coverage.start() test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests) # Stop code coverage after tests have completed if hasattr(settings, 'COVERAGE_MODULES'): coverage.stop() # Print code metrics header print '' print '----------------------------------------------------------------------' print ' Unit Test Code Coverage Results' print '----------------------------------------------------------------------' # Report code coverage metrics if hasattr(settings, 'COVERAGE_MODULES'): coverage_modules = [] for module in settings.COVERAGE_MODULES: coverage_modules.append(__import__(module, globals(), locals(), [''])) coverage.report(coverage_modules, show_missing=1) # Print code metrics footer print '----------------------------------------------------------------------' return test_results
def run(self): testfiles = [] testdirs = ["koan"] for d in testdirs: testdir = os.path.join(os.getcwd(), "tests", d) for t in _glob.glob(os.path.join(testdir, '*.py')): if t.endswith('__init__.py'): continue testfile = '.'.join(['tests', d, os.path.splitext(os.path.basename(t))[0]]) testfiles.append(testfile) tests = unittest.TestLoader().loadTestsFromNames(testfiles) runner = unittest.TextTestRunner(verbosity=1) if coverage: coverage.erase() coverage.start() result = runner.run(tests) if coverage: coverage.stop() sys.exit(int(bool(len(result.failures) > 0 or len(result.errors) > 0)))
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ Test runner which displays a code coverage report at the end of the run. """ django_test_runner = get_runner(settings) coverage.use_cache(0) coverage.start() results = django_test_runner(test_labels, verbosity, interactive, extra_tests) coverage.stop() coverage_modules = [] if test_labels: for label in test_labels: # Don't report coverage if you're only running a single # test case. if '.' not in label: app = get_app(label) coverage_modules.extend(get_all_coverage_modules(app)) else: for app in get_apps(): coverage_modules.extend(get_all_coverage_modules(app)) if coverage_modules: coverage.report(coverage_modules, show_missing=1) return results
def _setup(): global _logfile def report(): if run: run() if success.count and not failure.count: print("ok") # only clean out tempfiles if test passed import os, os.path, tempfile for file in _tempfiles: try: os.remove(file) except OSError: pass # report? temp_root = os.path.join(tempfile.gettempdir(), 'ccinfra-tool-tests') try: os.rmdir(temp_root) except OSError: pass if "--coverage" in sys.argv: import coverage coverage.stop() # The coverage module messes up when used from inside an # atexit handler. Do an explicit save to make sure that # we actually flush the coverage cache. coverage.the_coverage.save() import atexit, sys atexit.register(report) if "--coverage" in sys.argv: import coverage coverage.start() if "--log" in sys.argv: _logfile = open("test.log", "a")
def main_coverage(TESTS): modulenames = MODULE_NAMES coverage.erase() coverage.start() coverage.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]') modules = [] for modulename in modulenames: print modulenames mod = my_import(modulename) modules.append(mod) if 'unittest' in TESTS: print "***** Unittest *****" test_args = {'verbosity': 1} suite = unittest.TestLoader().loadTestsFromNames(TEST_NAMES) unittest.TextTestRunner(**test_args).run(suite) if 'doctest' in TESTS: t0 = time.time() print "\n***** Doctest *****" for mod in modules: doctest.testmod(mod, verbose=VERBOSE) td = time.time() - t0 print " Tests took %.3f seconds" % (td, ) print "\n***** Coverage Python *****" coverage.stop() coverage.report(modules, ignore_errors=1, show_missing=1) coverage.erase()
def _run_with_coverage(self): import coverage coverage.use_cache(False) coverage.start() try: self._run_tests() finally: coverage.stop() modules = [ m for _, m in sys.modules.items() if m is not None and hasattr(m, '__file__') and os.path.splitext(m.__file__)[-1] in ('.py', '.pyc') ] # Generate summary file buf = StringIO() coverage.report(modules, file=buf) buf.seek(0) fileobj = open(self.coverage_summary, 'w') try: filter_coverage(buf, fileobj) finally: fileobj.close() if self.coverage_dir: if not os.path.exists(self.coverage_dir): os.makedirs(self.coverage_dir) coverage.annotate(modules, directory=self.coverage_dir, ignore_errors=True)
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]): """Custom test runner. Follows the django.test.simple.run_tests() interface.""" coverage.use_cache(0) # Do not cache any of the coverage.py stuff coverage.start() test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests) coverage.stop() coverage_modules = [] if test_labels: for label in test_labels: # Don't report coverage if you're only running a single # test case. if '.' not in label: app = get_app(label) coverage_modules.extend(get_coverage_modules(app)) else: for app in get_apps(): coverage_modules.extend(get_coverage_modules(app)) if coverage_modules: # Print code metrics header print '' print '----------------------------------------------------------------------' print ' Unit Test Code Coverage Results' print '----------------------------------------------------------------------' coverage.report(coverage_modules, show_missing=1) # Print code metrics footer print '----------------------------------------------------------------------' return test_results
def _setup(): global _logfile def report(): if run: run() if success.count and not failure.count: print("ok") # only clean out tempfiles if test passed import os, os.path, tempfile for file in _tempfiles: try: os.remove(file) except OSError: pass # report? temp_root = os.path.join(tempfile.gettempdir(), 'pillow-tests') try: os.rmdir(temp_root) except OSError: pass if "--coverage" in sys.argv: import coverage coverage.stop() # The coverage module messes up when used from inside an # atexit handler. Do an explicit save to make sure that # we actually flush the coverage cache. coverage.the_coverage.save() import atexit, sys atexit.register(report) if "--coverage" in sys.argv: import coverage coverage.start() if "--log" in sys.argv: _logfile = open("test.log", "a")
def doctest(self): import doctest, coverage # (f,t) = doctest.testmod(eval('isconf.Kernel')) # doctest.master.summarize() # sys.exit(f) modules = [] olddir = os.getcwd() os.chdir('lib/python') os.path.walk('.',getmods,modules) os.chdir(olddir) print modules # modules=[rpc822] fail=0 total=0 coverage.erase() coverage.start() for mod in modules: (f,t) = doctest.testmod(mod,report=0) fail += f total += t doctest.master.summarize() coverage.stop() for mod in modules: coverage.analysis(mod) coverage.report(modules) sys.exit(fail)
def run_tests(self, test_labels, verbosity=1, interactive=True, extra_tests=[]): coveragemodules = getattr(settings, 'COVERAGE_MODULES', []) if coveragemodules: coverage.start() self.setup_test_environment() suite = self.build_suite(test_labels, extra_tests) old_config = self.setup_databases() result = self.run_suite(suite) if coveragemodules: coverage.stop() coveragedir = getattr(settings, 'COVERAGE_DIR', './build/coverage') if not os.path.exists(coveragedir): os.makedirs(coveragedir) modules = [] for module_string in coveragemodules: module = __import__(module_string, globals(), locals(), [""]) modules.append(module) f,s,m,mf = coverage.analysis(module) fp = file(os.path.join(coveragedir, module_string + ".html"), "wb") colorize.colorize_file(f, outstream=fp, not_covered=mf) fp.close() coverage.report(modules, show_missing=0) coverage.erase() self.teardown_databases(old_config) self.teardown_test_environment() return len(result.failures) + len(result.errors)
def handle(self, *test_labels, **options): """ Run pylint and test with coverage and xml reports """ patch_for_test_db_setup() verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive', True) excludes = options.get('excludes', '').split(',') excludes = [ exclude.strip() for exclude in excludes ] tasks = getattr(settings, 'HUDSON_TASKS', ['pylint', 'coverage', 'tests']) output_dir=options.get('output_dir') if not path.exists(output_dir): os.makedirs(output_dir) if not test_labels: test_labels = Command.test_labels() if verbosity > 0: print "Testing and covering the following apps:\n %s" % pprint.pformat(test_labels, ) #TODO: Make lint work and with external rc file if 'pylint' in tasks: pylint().handle(output_file=path.join(output_dir,'pylint.report'), *test_labels) if 'coverage' in tasks: coverage.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]') coverage.start() failures = 0 if 'tests' in tasks: test_runner = XmlDjangoTestSuiteRunner(output_dir=output_dir, interactive=interactive, verbosity=verbosity) failures = test_runner.run_tests(test_labels) #save coverage report if 'coverage' in tasks: coverage.stop() modules = [ module for name, module in sys.modules.items() \ if hasattr(module, "__file__") and self.want_module(module, test_labels, excludes) ] morfs = [ self.src(m.__file__) for m in modules if self.src(m.__file__).endswith(".py")] if verbosity > 0: if excludes: print "Excluding any module containing of these words:" pprint.pprint(excludes) print "Coverage being generated for:" pprint.pprint(morfs) if 'coverage' in tasks: coverage._the_coverage.xml_report(morfs, outfile=path.join(output_dir,'coverage.xml')) if failures: sys.exit(bool(failures))
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]): """Custom test runner. Follows the django.test.simple.run_tests() interface.""" # Start code coverage before anything else if necessary coverage_modules = [] for module_name in settings.COVERAGE_MODULES: app_name = module_name.split(".")[0] if (not test_labels) or (app_name in test_labels): coverage_modules.append(__import__(module_name, globals(), locals(), [''])) if hasattr(settings, 'COVERAGE_MODULES'): coverage.use_cache(0) # Do not cache any of the coverage.py stuff coverage.start() test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests) # Stop code coverage after tests have completed if hasattr(settings, 'COVERAGE_MODULES'): coverage.stop() # Print code metrics header print '' print '----------------------------------------------------------------------' print ' Unit Test Code Coverage Results' print '----------------------------------------------------------------------' coverage.report(coverage_modules, show_missing=1) # Print code metrics footer print '----------------------------------------------------------------------' else: print "No coverage modules defined in settings.py" return test_results
def run(self): try: # Use system 'coverage' if available import coverage use_coverage = True except: use_coverage = False tests = unittest.TestLoader().loadTestsFromNames(self._testfiles) t = unittest.TextTestRunner(verbosity=1) if use_coverage: coverage.erase() coverage.start() if hasattr(unittest, "installHandler"): try: unittest.installHandler() except: print "installHandler hack failed" try: result = t.run(tests) except KeyboardInterrupt: sys.exit(1) if use_coverage: coverage.stop() sys.exit(int(bool(len(result.failures) > 0 or len(result.errors) > 0)))
def main(): from optparse import OptionParser usage = ('Usage: %prog [option] [modules to be tested]\n' 'Modules names have to be given in the form utils.mail (without ' 'zine.)\nIf no module names are given, all tests are run') parser = OptionParser(usage=usage) parser.add_option('-c', '--coverage', action='store_true', dest='coverage', help='show coverage information (slow!)') parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False, help='show which tests are run') options, args = parser.parse_args(sys.argv[1:]) modnames = ['zine.' + modname for modname in args] if options.coverage: if coverage is not None: use_coverage = True else: sys.stderr.write("coverage information requires Ned Batchelder's " "coverage.py to be installed!\n") sys.exit(1) else: use_coverage = False if use_coverage: coverage.erase() coverage.start() s, covermods = suite(modnames, True) else: s = suite(modnames) TextTestRunner(verbosity=options.verbose + 1).run(s) if use_coverage: coverage.stop() print '\n\n' + '=' * 25 + ' coverage information ' + '=' * 25 coverage.report(covermods)
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]): """Custom test runner. Follows the django.test.simple.run_tests() interface.""" do_coverage = hasattr(settings, 'COVERAGE_MODULES') and settings.COVERAGE_MODULES # Start code coverage before anything else if necessary if do_coverage: coverage.use_cache(0) # Do not cache any of the coverage.py stuff coverage.start() test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests) if do_coverage: coverage.stop() # Print code metrics header print print '-' * 60 print ' Unit Test Code Coverage Results' print '-' * 60 coverage_modules = [] for module in settings.COVERAGE_MODULES: coverage_modules.append(__import__(module, globals(), locals(), [''])) coverage.report(coverage_modules, show_missing=1) # Print code metrics footer print '-' * 60 return test_results
def _run_with_coverage(self): import coverage coverage.use_cache(False) coverage.start() try: self._run_tests() finally: coverage.stop() modules = [m for _, m in sys.modules.items() if m is not None and hasattr(m, '__file__') and os.path.splitext(m.__file__)[-1] in ('.py', '.pyc')] # Generate summary file buf = StringIO() coverage.report(modules, file=buf) buf.seek(0) fileobj = open(self.coverage_summary, 'w') try: filter_coverage(buf, fileobj) finally: fileobj.close() if self.coverage_dir: if not os.path.exists(self.coverage_dir): os.makedirs(self.coverage_dir) coverage.annotate(modules, directory=self.coverage_dir, ignore_errors=True)
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]): # This doesn't work with Django 1.4 from django.test.simple import run_tests as django_test_runner import coverage coverage.use_cache(0) coverage.start() test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests) coverage.stop() coverage_modules = [m.__file__ for k, m in sys.modules.iteritems() if m and k.split('.')[0] in test_labels and 'test' not in k] print print '='*80 print 'Coverage results for %s' % ', '.join(test_labels) print '='*80 coverage.report(coverage_modules, show_missing=1) coverage_html_dir = getattr(settings, 'COVERAGE_HTML_DIR', None) if coverage_html_dir is not None: coverage._the_coverage.html_report(coverage_modules, coverage_html_dir) return test_results
def handle(self, *args, **options): """ Run pylint and test with coverage and xml reports """ output_dir=options.get('output_dir') if not path.exists(output_dir): os.makedirs(output_dir) app_labels = Command.app_list() # pylint pylint().handle(*app_labels, output_file=path.join(output_dir,'pylint.report')) #coverage coverage.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]') coverage.start() #tests test_runner = XmlDjangoTestSuiteRunner(output_dir=output_dir) failures = test_runner.run_tests(app_labels) #save coverage report coverage.stop() modules = [module for name, module in sys.modules.items() \ if module and any([name.startswith(label) for label in app_labels])] morfs = [ m.__file__ for m in modules if hasattr(m, '__file__') ] coverage._the_coverage.xml_report(morfs, outfile=path.join(output_dir,'coverage.xml'))
def run_tests(verbosity): "Run test suite" # list all the files in the top level directory file_list = os.listdir(os.path.join(os.path.abspath( os.path.dirname(os.path.realpath(__file__))))) # list all the files in the tests directory test_list = os.listdir(os.path.join(os.path.abspath( os.path.dirname(os.path.realpath(__file__))), 'tests')) code_modules = [] # loop over all the file names for file_name in file_list: extension = os.path.splitext(file_name)[-1] # if they are python files or the test runner if extension == '.py' and file_name != 'test.py': # work out the module name code_module_name = os.path.splitext(file_name)[0:-1][0] # now import the module module = __import__(code_module_name, globals(), locals(), code_module_name) # and add it to the list of available modules code_modules.append(module) test_modules = [] # loop over all the file names for file_name in test_list: extension = os.path.splitext(file_name)[-1] # if they are python files if extension == '.py': # work out the module name test_module_name = "tests." + os.path.splitext(file_name)[0:-1][0] # now import the module module = __import__(test_module_name, globals(), locals(), test_module_name) # and add it to the list of available modules test_modules.append(module) # populate a test suite from the individual tests from the list of modules suite = unittest.TestSuite(map( unittest.defaultTestLoader.loadTestsFromModule, test_modules)) # set up the test runner runner = unittest.TextTestRunner(verbosity=int(verbosity)) # set up coverage reporting coverage.use_cache(0) coverage.start() # run the tests runner.run(suite) # stop coverage reporting coverage.stop() # output coverage report coverage.report(code_modules, show_missing=1)
def run_tests(self, test_labels, extra_tests=None, **kwargs): self.enable_coverage = hasattr(settings, u'COVERAGE_MODULES') if self.enable_coverage: coverage.use_cache(0) coverage.start() super(SelectiveTestSuiteRunnerWithCoverage, self)\ .run_tests(test_labels, extra_tests=None, **kwargs)
def begin(self): log.debug("Coverage begin") import coverage self.skipModules = sys.modules.keys()[:] if self.coverErase: log.debug("Clearing previously collected coverage statistics") coverage.erase() coverage.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]') coverage.start()
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ Run the unit tests for all the test labels in the provided list. Labels must be of the form: - app.TestClass.test_method Run a single specific test method - app.TestClass Run all the test methods in a given class - app Search for doctests and unittests in the named application. When looking for tests, the test runner will look in the models and tests modules for the application. A list of 'extra' tests may also be provided; these tests will be added to the test suite. If the settings file has an entry for COVERAGE_MODULES or test_labels is true it will prints the coverage report for modules/apps Returns number of tests that failed. """ do_coverage = hasattr(settings, 'COVERAGE_MODULES') or bool(test_labels) if do_coverage: coverage.erase() coverage.start() from django.test import simple retval = simple.run_tests(test_labels, verbosity, interactive, extra_tests) if do_coverage: coverage.stop() # Print code metrics header print '' print '----------------------------------------------------------------------' print ' Unit Test Code Coverage Results' print '----------------------------------------------------------------------' # try to import all modules for the coverage report. modules = [] if getattr(settings, 'COVERAGE_MODULES', None): modules = [__import__(module, {}, {}, ['']) for module in settings.COVERAGE_MODULES] elif test_labels: modules = [] for label in test_labels: label = label.split('.')[0] #remove test class or test method from label pkg = _get_app_package(label) modules.extend(_package_modules(*pkg)) coverage.report(modules, show_missing=1) return retval
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ Test runner which displays a code coverage report at the end of the run. """ coverage.use_cache(0) for e in settings.COVERAGE_CODE_EXCLUDES: coverage.exclude(e) coverage.start() results = base_run_tests(test_labels, verbosity, interactive, extra_tests) coverage.stop() coverage_modules = [] if test_labels: for label in test_labels: label = label.split('.')[0] app = get_app(label) coverage_modules.append(_get_app_package(app)) else: for app in get_apps(): coverage_modules.append(_get_app_package(app)) coverage_modules.extend(settings.COVERAGE_ADDITIONAL_MODULES) packages, modules, excludes, errors = get_all_modules( coverage_modules, settings.COVERAGE_MODULE_EXCLUDES, settings.COVERAGE_PATH_EXCLUDES) outdir = settings.COVERAGE_REPORT_HTML_OUTPUT_DIR if outdir is None: coverage.report(modules.values(), show_missing=1) if excludes: print >>sys.stdout print >>sys.stdout, "The following packages or modules were excluded:", for e in excludes: print >>sys.stdout, e, print >>sys.stdout if errors: print >>sys.stdout print >>sys.stderr, "There were problems with the following packages or modules:", for e in errors: print >>sys.stderr, e, print >>sys.stdout else: outdir = os.path.abspath(outdir) if settings.COVERAGE_CUSTOM_REPORTS: html_report(outdir, modules, excludes, errors) else: coverage._the_coverage.html_report(modules.values(), outdir) coverage._the_coverage.xml_report(modules.values(), os.path.join(outdir,'coverage.xml') ) print >>sys.stdout print >>sys.stdout, "HTML reports were output to '%s'" %outdir return results
def run_tests(self, test_labels, extra_tests=None, **kwargs): coverage.use_cache(settings.COVERAGE_USE_CACHE) for e in settings.COVERAGE_CODE_EXCLUDES: coverage.exclude(e) coverage.start() results = super(CoverageRunner, self).run_tests(test_labels, extra_tests, **kwargs) coverage.stop() coverage_modules = [] if test_labels: for label in test_labels: label = label.split('.')[0] app = get_app(label) coverage_modules.append(self._get_app_package(app)) else: for app in get_apps(): coverage_modules.append(self._get_app_package(app)) coverage_modules.extend(settings.COVERAGE_ADDITIONAL_MODULES) packages, modules, excludes, errors = get_all_modules( coverage_modules, settings.COVERAGE_MODULE_EXCLUDES, settings.COVERAGE_PATH_EXCLUDES) if settings.COVERAGE_USE_STDOUT: coverage.report(modules.values(), show_missing=1) if excludes: message = "The following packages or modules were excluded:" print >>sys.stdout print >>sys.stdout, message, for e in excludes: print >>sys.stdout, e, print >>sys.stdout if errors: message = "There were problems with the following packages " message += "or modules:" print >>sys.stdout print >>sys.stderr, message, for e in errors: print >>sys.stderr, e, print >>sys.stdout outdir = settings.COVERAGE_REPORT_HTML_OUTPUT_DIR if outdir: outdir = os.path.abspath(outdir) if settings.COVERAGE_CUSTOM_REPORTS: html_report(outdir, modules, excludes, errors) else: coverage._the_coverage.html_report(modules.values(), outdir) print >>sys.stdout print >>sys.stdout, "HTML reports were output to '%s'" %outdir return results
def run_tests(with_coverage=False): # needed to avoid confusion involving atexit handlers import logging if sys.argv[1:]: # test only files given on cmdline files = [ entry + '.py' for entry in sys.argv[1:] if entry.startswith('test_') ] else: files = [ entry for entry in os.listdir(testdir) if (entry.startswith('test_') and entry.endswith('.py')) ] files.sort() WIDTH = 85 print >>sys.stderr, \ ('Pygments %s Test Suite running%s, stand by...' % (pygments.__version__, with_coverage and " with coverage analysis" or "")).center(WIDTH) print >> sys.stderr, ('(using Python %s)' % sys.version.split()[0]).center(WIDTH) print >> sys.stderr, '=' * WIDTH if with_coverage: coverage.erase() coverage.start() for testfile in files: globs = {'__file__': join(testdir, testfile)} try: execfile(join(testdir, testfile), globs) except Exception, exc: raise err(testfile, 'execfile', exc) continue sys.stderr.write(testfile[:-3] + ': ') try: runner = QuietTestRunner(testfile[:-3]) # make a test suite of all TestCases in the file tests = [] for name, thing in globs.iteritems(): if name.endswith('Test'): tests.append((name, unittest.makeSuite(thing))) tests.sort() suite = unittest.TestSuite() suite.addTests([x[1] for x in tests]) runner.run(suite) except Exception, exc: err(testfile, 'running test', exc)
def nose_start(LOG, REPO_PATH): # test names """ Find all python modules in REPO_PATH """ WORKING_DIR = getcwd() chdir(REPO_PATH) coverage.erase() coverage.start() nose.run() coverage.stop() LOG.info(coverage.analysis(nose)) LOG.info(coverage.report()) chdir(WORKING_DIR)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ Test runner which displays a code coverage report at the end of the run. """ coverage.use_cache(0) for e in getattr(settings, 'COVERAGE_CODE_EXCLUDES', []): coverage.exclude(e) coverage.start() results = base_run_tests(test_labels, verbosity, interactive, extra_tests) coverage.stop() coverage_modules = [] if test_labels: for label in test_labels: label = label.split('.')[0] app = get_app(label) coverage_modules.append(_get_app_package(app)) else: for app in get_apps(): coverage_modules.append(_get_app_package(app)) coverage_modules.extend( getattr(settings, 'COVERAGE_ADDITIONAL_MODULES', [])) packages, modules, excludes, errors = get_all_modules( coverage_modules, getattr(settings, 'COVERAGE_MODULE_EXCLUDES', []), getattr(settings, 'COVERAGE_PATH_EXCLUDES', [])) coverage.report(modules.values(), show_missing=1) if excludes: print >> sys.stdout print >> sys.stdout, "The following packages or modules were excluded:", for e in excludes: print >> sys.stdout, e, print >> sys.stdout if errors: print >> sys.stdout print >> sys.stderr, "There were problems with the following packages or modules:", for e in errors: print >> sys.stderr, e, print >> sys.stdout outdir = getattr(settings, 'COVERAGE_REPORT_HTML_OUTPUT_DIR', 'test_html') outdir = os.path.abspath(outdir) html_report(outdir, modules, excludes, errors) print >> sys.stdout print >> sys.stdout, "HTML reports were output to '%s'" % outdir return results
def runTests(self): testrunner = TextTestRunnerWithTimings( verbosity=self._options.verbosity, timeTests=self._options.time, nrTestsToReport=self._options.time_reports) if self._options.coverage: coverage.erase() coverage.start() result = testrunner.run(self) if self._options.coverage: coverage.stop() print coverage.report(self.getPyFilesFromDir(os.path.join(projectRoot, 'taskcoachlib'))) return result
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]): import coverage from django.test.simple import run_tests as django_test_runner from django.conf import settings coverage.use_cache(0) # Do not cache any of the coverage.py stuff coverage.start() test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests) coverage.stop() coverage_modules = [] for module in settings.COVERAGE_MODULES: coverage_modules.append(__import__(module, globals(), locals(), [""])) coverage.report(coverage_modules, show_missing=0) return test_results
def run_tests(self, test_labels, extra_tests=[], **kwargs): # Shutup console logs logger = logging.getLogger() for h in logger.handlers: logger.removeHandler(h) import coverage coverage.erase() coverage.start() # If test_labels let django handle it if test_labels: return super(TestRunner, self).run_tests(test_labels, extra_tests) # Validate models s = StringIO() num_errors = get_validation_errors(s) if num_errors: raise Exception("%s error%s found:\n%s" % (num_errors, num_errors != 1 and 's' or '', s.getvalue())) # Use discover to find all the local tests loader = unittest.TestLoader() base = os.path.abspath(os.path.dirname(__file__)) if not extra_tests: extra_tests = [] tests = loader.discover(base) suite = unittest.TestSuite() if extra_tests: suite.addTests(extra_tests) suite.addTests(tests) local_dirs = [x for x in os.listdir(base) if os.path.isdir(os.path.join(base,x))] local_dirs.extend([x for x in os.listdir(os.path.join(base, 'apps')) \ if os.path.isdir(os.path.join(base,'apps', x))]) self.setup_test_environment() old_config = self.setup_databases() result = self.run_suite(suite) self.teardown_databases(old_config) self.teardown_test_environment() result = self.suite_result(suite, result) if not result and self.verbosity >= 1: report_coverage(local_dirs) return result
def runTests(testModules=None, profileOut=None, coverageOutDir=None): if coverageOutDir is not None: if not os.path.exists(coverageOutDir): os.makedirs(coverageOutDir) coverage.erase() coverage.start() alltests = unittest.TestLoader().loadTestsFromNames(testModules) if profileOut is not None: results = Profiler().profile(alltests) results.sort() results.reverse() out = open(profileOut, 'w') for result in results: out.write("%s \t%3.6f\n" % (result[1], result[0])) print "unittest profiling information written in " + profileOut out.close() else: # if we don't add this else the tests are run twice unittest.TextTestRunner().run(alltests) if coverageOutDir is not None: coverage.stop() modules = glob('../*.py') #modules.remove('../__init__.py') for module in modules: f, s, m, mf = coverage.analysis(module) out = file(join(coverageOutDir, os.path.basename(f) + '.html'), 'wb') colorize.colorize_file(f, outstream=out, not_covered=mf) out.close() print coverageReportTxt = file(join(coverageOutDir, "coverage.txt"), 'w') coverage.report(modules, show_missing=False, omit_prefixes=['__'], file=coverageReportTxt) coverageReportTxt.close() coverage.report(modules, show_missing=False, omit_prefixes=['__']) coverage.erase() coverageReportTxt.close() print print "Coverage information updated in " + coverageOutDir print import webbrowser webbrowser.open('file://' + join(getcwd(), coverageOutDir))
def start(clean, packages, target): '''Start the coverage''' # Set the coverage data storing path os.environ['COVERAGE_FILE'] = os.path.join(target, nconst.COVER_OUTPUT_BASE) # Check if clean if clean: ntools.clean(target, nconst.COVER_OUTPUT_BASE) # Start coverage.start() # Load packages for package in packages: # Try to get it try: __import__(package) # Unable to get it, don't care except: pass
def main(): # Create the option parser. optparser = OptionParser(usage='%prog [options] NAME ...', version="Edloper's Doctest Driver, " "version %s" % __version__) action_group = OptionGroup(optparser, 'Actions (default=check)') action_group.add_options([CHECK_OPT, UPDATE_OPT, DEBUG_OPT]) optparser.add_option_group(action_group) reporting_group = OptionGroup(optparser, 'Reporting') reporting_group.add_options([ VERBOSE_OPT, QUIET_OPT, UDIFF_OPT, CDIFF_OPT, NDIFF_OPT, COVERAGE_OPT, CONTINUE_OPT ]) optparser.add_option_group(reporting_group) compare_group = OptionGroup(optparser, 'Output Comparison') compare_group.add_options([ELLIPSIS_OPT, NORMWS_OPT]) optparser.add_option_group(compare_group) # Extract optionflags and the list of file names. optionvals, names = optparser.parse_args() if len(names) == 0: optparser.error("No files specified") optionflags = (optionvals.udiff * REPORT_UDIFF | optionvals.cdiff * REPORT_CDIFF | optionvals.ellipsis * ELLIPSIS | optionvals.normws * NORMALIZE_WHITESPACE) # Check coverage, if requested if optionvals.coverage: coverage.use_cache(True, cache_file=optionvals.coverage) coverage.start() # Perform the requested action. if optionvals.action == 'check': run(names, optionflags, optionvals.verbosity, optionvals.kbinterrupt_continue) elif optionvals.action == 'update': update(names, optionflags, optionvals.verbosity) elif optionvals.action == 'debug': debug(names, optionflags, optionvals.verbosity) else: optparser.error('INTERNAL ERROR: Bad action %s' % optionvals.action) # Check coverage, if requested if optionvals.coverage: coverage.stop()
def begin(self): """Used to stub out nose.plugins.cover.Coverage.begin. The difference is that it loads Melange after coverage starts, so the loading of models, logic and views can be tracked by coverage. """ log.debug("Coverage begin") import coverage self.skipModules = sys.modules.keys()[:] if self.coverErase: log.debug("Clearing previously collected coverage statistics") coverage.erase() coverage.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]') coverage.start() load_melange()
def doit(): srcdir = os.environ['RTTSrcDir'] pyfiles = glob.glob(os.path.join(srcdir, '*.py')) coverage.erase() coverage.start() run.TestFramework() coverage.stop() coverage.analysis(run) coverage.report(pyfiles) coverage.erase()
def main(): # Create the option parser. optparser = OptionParser(usage='%prog [options] NAME ...', version="Edloper's Doctest Driver, " "version %s" % __version__) action_group = OptionGroup(optparser, 'Actions (default=check)') action_group.add_options([CHECK_OPT, UPDATE_OPT, DEBUG_OPT]) optparser.add_option_group(action_group) reporting_group = OptionGroup(optparser, 'Reporting') reporting_group.add_options([VERBOSE_OPT, QUIET_OPT, UDIFF_OPT, CDIFF_OPT, NDIFF_OPT, COVERAGE_OPT, CONTINUE_OPT]) optparser.add_option_group(reporting_group) compare_group = OptionGroup(optparser, 'Output Comparison') compare_group.add_options([ELLIPSIS_OPT, NORMWS_OPT]) optparser.add_option_group(compare_group) # Extract optionflags and the list of file names. optionvals, names = optparser.parse_args() if len(names) == 0: optparser.error("No files specified") optionflags = (optionvals.udiff * REPORT_UDIFF | optionvals.cdiff * REPORT_CDIFF | optionvals.ellipsis * ELLIPSIS | optionvals.normws * NORMALIZE_WHITESPACE) # Check coverage, if requested if optionvals.coverage: coverage.use_cache(True, cache_file=optionvals.coverage) coverage.start() # Perform the requested action. if optionvals.action == 'check': run(names, optionflags, optionvals.verbosity, optionvals.kbinterrupt_continue) elif optionvals.action == 'update': update(names, optionflags, optionvals.verbosity) elif optionvals.action == 'debug': debug(names, optionflags, optionvals.verbosity) else: optparser.error('INTERNAL ERROR: Bad action %s' % optionvals.action) # Check coverage, if requested if optionvals.coverage: coverage.stop()
def run_tests(with_coverage=False): # needed to avoid confusion involving atexit handlers import logging if sys.argv[1:]: # test only files given on cmdline files = [entry + '.py' for entry in sys.argv[1:] if entry.startswith('test_')] else: files = [entry for entry in os.listdir(testdir) if (entry.startswith('test_') and entry.endswith('.py'))] files.sort() WIDTH = 85 print >>sys.stderr, \ ('Pygments %s Test Suite running%s, stand by...' % (pygments.__version__, with_coverage and " with coverage analysis" or "")).center(WIDTH) print >>sys.stderr, ('(using Python %s)' % sys.version.split()[0]).center(WIDTH) print >>sys.stderr, '='*WIDTH if with_coverage: coverage.erase() coverage.start() for testfile in files: globs = {} try: __builtin__.testfile = testfile execfile(join(testdir, testfile), globs) except Exception, exc: raise err(testfile, 'execfile', exc) continue sys.stderr.write(testfile[:-3] + ': ') try: runner = QuietTestRunner(testfile[:-3]) # make a test suite of all TestCases in the file tests = [] for name, thing in globs.iteritems(): if name.endswith('Test'): tests.append((name, unittest.makeSuite(thing))) tests.sort() suite = unittest.TestSuite() suite.addTests([x[1] for x in tests]) runner.run(suite) except Exception, exc: err(testfile, 'running test', exc)
def make_suite(modnames, other_files, use_coverage, specific_mods): topdir = get_topdir() # Make a test suite to put all the tests in. suite = unittest.TestSuite() if use_coverage: # Use the coverage test module to get coverage information. import coverage coverage.erase() coverage.start() coverage.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]') # Add all the doctest tests. modules = [] for modname in modnames: try: # Get the path of the module (to search for associated tests) modpath = os.path.join(*(modname.split('.'))) modpath = canonical_path(modpath) if os.path.isdir(modpath): modpath = os.path.join(modpath, '__init__') # Import the module sys.path.insert(0, topdir) mod = __import__(modname, None, None, ['']) del sys.path[0] # Check that the module imported came from the expected path. if os.path.splitext(mod.__file__)[0] != modpath: print "Couldn't import module `%s`: got module of same name, from wrong path (%r)" % (modname, mod.__file__) continue # Add module to test suite. suite.addTest(doctest.DocTestSuite(mod, setUp=setup_test, tearDown=teardown_test)) modules.append(mod) # Check for additional doctest files moddir, modfilename = os.path.split(modpath) modpath = os.path.join(moddir, "doctests", modfilename + '_doctest%d.txt') num = 1 while os.path.exists(modpath % num): suite.addTest(create_docfile_suite(mod, moddir, modpath % num)) num += 1 except ImportError, e: print "Couldn't import module `%s`: %s" % (modname, e) traceback.print_exc()
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]): coverage_enabled = coverage and hasattr(settings, 'COVERAGE_MODULES') if coverage_enabled: coverage.use_cache(0) coverage.start() test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests) if coverage_enabled: coverage.stop() coverage_modules = [] for module in settings.COVERAGE_MODULES: coverage_modules.append(__import__(module, globals(), locals(), [''])) coverage.report(coverage_modules, show_missing=1) return test_results
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]): """Custom test runner. Follows the django.test.simple.run_tests() interface.""" import os, shutil, sys # Look for coverage.py in __file__/lib as well as sys.path sys.path = [os.path.join(os.path.dirname(__file__), "lib")] + sys.path import coverage from django.test.simple import run_tests as django_test_runner from django.conf import settings # Start code coverage before anything else if necessary #if hasattr(settings, 'COVERAGE_MODULES') and not test_labels: coverage.use_cache(0) # Do not cache any of the coverage.py stuff coverage.start() test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests) # Stop code coverage after tests have completed #if hasattr(settings, 'COVERAGE_MODULES') and not test_labels: coverage.stop() # Print code metrics header print '' print '----------------------------------------------------------------------' print ' Unit Test Code Coverage Results' print '----------------------------------------------------------------------' # Report code coverage metrics coverage_modules = [] if hasattr(settings, 'COVERAGE_MODULES') and (not test_labels or 'cms' in test_labels): for module in settings.COVERAGE_MODULES: coverage_modules.append( __import__(module, globals(), locals(), [''])) coverage.report(coverage_modules, show_missing=1) #Print code metrics footer print '----------------------------------------------------------------------' return test_results
def do_report_work(self, modname): """Create a module named `modname`, then measure it.""" coverage.erase() self.make_file(modname+".py", """\ a = 1 b = 2 if b == 3: c = 4 d = 5 e = 6 f = 7 """) # Import the python file, executing it. coverage.start() self.import_local_file(modname) # pragma: recursive coverage coverage.stop() # pragma: recursive coverage
def run_tests(self, test_labels, extra_tests=[], **kwargs): # Shutup console logs logger = logging.getLogger() for h in logger.handlers: logger.removeHandler(h) import coverage coverage.erase() coverage.start() # If test_labels let django handle it if test_labels: return super(TestRunner, self).run_tests(test_labels, extra_tests) # Use discover to find all the local tests loader = unittest.TestLoader() base = os.path.abspath(os.path.dirname(__file__)) if not extra_tests: extra_tests = [] tests = loader.discover(base) suite = unittest.TestSuite() if extra_tests: suite.addTests(extra_tests) suite.addTests(tests) local_dirs = [x for x in os.listdir(base) if os.path.isdir(os.path.join(base,x))] local_dirs.extend([x for x in os.listdir(os.path.join(base, 'apps')) \ if os.path.isdir(os.path.join(base,'apps', x))]) self.setup_test_environment() old_config = self.setup_databases() result = self.run_suite(suite) self.teardown_databases(old_config) self.teardown_test_environment() result = self.suite_result(suite, result) if not result and self.verbosity >= 1: report_coverage(local_dirs) return result
def run_tests(self, test_labels, extra_tests=None, **kwargs): run_with_coverage = hasattr(settings, 'COVERAGE_MODULES') # Set DEFAULT_TEST_LABELS to run just the tests we want # when we don't give 'test' any arguments if not test_labels: test_labels = getattr(settings, 'DEFAULT_TEST_LABELS', []) # If we've set DEFAULT_TEST_LABELS and we want to run them all, # just say 'test all' elif list(test_labels) == ['all']: test_labels = [] if run_with_coverage: import coverage coverage.use_cache(0) coverage.start() result = super(CoverageRunner, self).run_tests(test_labels, extra_tests, **kwargs) if run_with_coverage: coverage.stop() coverage_modules = [] if test_labels: for label in test_labels: # Don't report coverage if you're only running a single # test case. if '.' not in label: app = get_app(label) coverage_modules.extend(self.get_coverage_modules(app)) else: for app in get_apps(): coverage_modules.extend(self.get_coverage_modules(app)) if coverage_modules: print '' print '----------------------------------------------------------------------' print ' Unit Test Code Coverage Results' print '----------------------------------------------------------------------' coverage.report(coverage_modules, show_missing=1) print '----------------------------------------------------------------------' return result
def test_simple(self): coverage.erase() self.make_file("mycode.py", """\ a = 1 b = 2 if b == 3: c = 4 d = 5 """) # Import the python file, executing it. coverage.start() self.import_local_file("mycode") # pragma: recursive coverage coverage.stop() # pragma: recursive coverage _, statements, missing, missingtext = coverage.analysis("mycode.py") self.assertEqual(statements, [1,2,3,4,5]) self.assertEqual(missing, [4]) self.assertEqual(missingtext, "4")
def run(self, test_names = None, print_only = False, coverage = False): if 'pytagsfs' in sys.modules: raise AssertionError( 'pytagsfs already imported; ' 'this interferes with coverage analysis' ) if coverage: import coverage as _coverage if int(_coverage.__version__.split('.')[0]) < 3: print >>sys.stderr, ( 'warning: coverage versions < 3 ' 'are known to produce imperfect results' ) _coverage.use_cache(False) _coverage.start() try: self.load() suite = TestSuite() runner = TextTestRunner(verbosity = 2) for test in self.tests: if self.should_run_test(test, test_names): if print_only: print test.id() else: suite.addTest(test) if not print_only: return runner.run(suite) finally: if coverage: _coverage.stop() import pytagsfs _coverage.report(self.find_modules(pytagsfs))
def run_tests_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ Test runner which displays a code coverage report at the end of the run. It will not display coverage if: a) The COVERAGE_MODULES setting is not set. b) A specific app is being tested. """ if hasattr(settings, 'COVERAGE_MODULES') and not test_labels: enable_coverage = True else: enable_coverage = False if enable_coverage: coverage.use_cache(0) coverage.start() results = run_tests(test_labels, verbosity, interactive, extra_tests) if enable_coverage: coverage.stop() print '-------------------------------------------------' print 'Coverage' print '-------------------------------------------------' # Report coverage coverage_modules = [] for module in settings.COVERAGE_MODULES: coverage_modules.append( __import__(module, globals(), locals(), [''])) coverage.report(coverage_modules, show_missing=1) print '-------------------------------------------------' return results
def _start_coverage(option, opt_str, value, parser): import sys, atexit, coverage true_out = sys.stdout def _iter_covered_files(): import sqlalchemy for rec in os.walk(os.path.dirname(sqlalchemy.__file__)): for x in rec[2]: if x.endswith('.py'): yield os.path.join(rec[0], x) def _stop(): coverage.stop() true_out.write("\nPreparing coverage report...\n") coverage.report(list(_iter_covered_files()), show_missing=False, ignore_errors=False, file=true_out) atexit.register(_stop) coverage.erase() coverage.start()
def run_tests(self, test_labels, verbosity=1, interactive=True, extra_tests=[]): coveragemodules = getattr(settings, 'COVERAGE_MODULES', []) if coveragemodules: coverage.start() self.setup_test_environment() suite = self.build_suite(test_labels, extra_tests) old_config = self.setup_databases() result = self.run_suite(suite) if coveragemodules: coverage.stop() coveragedir = getattr(settings, 'COVERAGE_DIR', './build/coverage') if not os.path.exists(coveragedir): os.makedirs(coveragedir) modules = [] for module_string in coveragemodules: module = __import__(module_string, globals(), locals(), [""]) modules.append(module) f, s, m, mf = coverage.analysis(module) fp = file(os.path.join(coveragedir, module_string + ".html"), "wb") colorize.colorize_file(f, outstream=fp, not_covered=mf) fp.close() coverage.report(modules, show_missing=0) coverage.erase() self.teardown_databases(old_config) self.teardown_test_environment() return len(result.failures) + len(result.errors)
def run_tests(self, *args, **kwargs): run_with_coverage = hasattr(settings, 'COVERAGE_MODULES') if run_with_coverage: coverage.use_cache(0) coverage.start() result = super(CoverageRunner, self).run_tests(*args, **kwargs) if run_with_coverage: coverage.stop() print '' print '----------------------------------------------------------------------' print ' Unit Test Code Coverage Results' print '----------------------------------------------------------------------' coverage_modules = [] for module in settings.COVERAGE_MODULES: coverage_modules.append(__import__(module, globals(), locals(), [''])) coverage.report(coverage_modules, show_missing=1) print '----------------------------------------------------------------------' return result