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 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 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 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=[]): """ 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(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 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 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()
#!/usr/bin/python """ Test the Cog distribution. http://nedbatchelder.com/code/cog Copyright 2004-2012, Ned Batchelder. """ import unittest, sys bCoverage = ('-cover' in sys.argv or '--cover' in sys.argv) if bCoverage: import coverage coverage.use_cache(0) coverage.exclude("#pragma: no cover") coverage.exclude("raise CogInternalError\(") coverage.start() testmodules = [ 'cogapp.test_makefiles', 'cogapp.test_whiteutils', 'cogapp.test_cogapp', ] # We don't need to import these modules to run the tests. But loadTestsFromNames # doesn't show import errors. These imports do. exec("import " + ("\nimport ".join(testmodules))) suite = unittest.TestSuite() for t in testmodules:
import unittest import coverage from alexa import alexa_etl, top_list coverage.exclude('if __name__ == .__main__.:') class alexaTest(unittest.TestCase): def test_etl(self): a = alexa_etl() first = a.next() # first should have a rank of 1 and the second part should be a domain self.assertEqual(first[0], 1, \ "First item in alexa_etl did not have a rank of 1, " + "it had a rank of %d" % first[0]) # simple check to see that the first result is a domain # i.e. has a dot in it self.assertTrue(first[1].count('.') > 0, "alexa_etl did not return a url") def test_top_list(self): a = top_list(10) self.assertEqual(len(a), 10, "top_list did not return 10 items") self.assertEqual(a[0][0], 1, "first item was not ranked #1") self.assertEqual( a[9][0], 10, "tenth item was not ranked #10, rank of %d provided" % a[9][0])
import unittest import coverage from alexa import alexa_etl, top_list coverage.exclude('if __name__ == .__main__.:') class alexaTest(unittest.TestCase): def test_etl(self): a = alexa_etl() first = a.next() # first should have a rank of 1 and the second part should be a domain self.assertEqual(first[0], 1, \ "First item in alexa_etl did not have a rank of 1, " + "it had a rank of %d" % first[0]) # simple check to see that the first result is a domain # i.e. has a dot in it self.assertTrue(first[1].count('.') > 0, "alexa_etl did not return a url") def test_top_list(self): a = top_list(10) self.assertEqual(len(a), 10, "top_list did not return 10 items") self.assertEqual(a[0][0], 1, "first item was not ranked #1") self.assertEqual(a[9][0], 10, "tenth item was not ranked #10, rank of %d provided" % a[9][0])
cases and suite within that file or module. See the sample init and testCase files for more information. """ import unittest try: import coverage except ImportError: coverage = None import sys if coverage: coverage.erase() coverage.start() coverage.exclude('if __name__ == "__main__":') import dabo.ui dabo.ui.loadUI('wx') import db import biz import lib import ui suiteList = [db.suite(), biz.suite(), lib.suite(), ui.suite()] #import any tests for the main dabo folder import Test_dColors
try: cursor.execute(data) transaction.commit_unless_managed() except Exception,err: print 'DB Schema load error: ',err else: print 'DB Schema loaded.' else: print 'No Schema found.' print 'Start running tests.' ##### coverage.use_cache(0) for e in default_settings.COVERAGE_CODE_EXCLUDES: coverage.exclude(e) coverage.start() ##### nose.run(argv=nose_argv) ##### coverage.stop() ##### print 'Testing ended.' # Clean up django. connection.creation.destroy_test_db(old_db_name, verbosity)
# Coverage-test coverage.py! import coverage import test_coverage import unittest import sys print "Testing under Python version:\n", sys.version coverage.erase() coverage.start() coverage.exclude("#pragma: no cover") # Re-import coverage to get it coverage tested! covmod = sys.modules['coverage'] del sys.modules['coverage'] import coverage sys.modules['coverage'] = coverage = covmod suite = unittest.TestSuite() suite.addTest(unittest.defaultTestLoader.loadTestsFromNames(["test_coverage"])) testrunner = unittest.TextTestRunner() testrunner.run(suite) coverage.stop() coverage.report("coverage.py")
cases and suite within that file or module. See the sample init and testCase files for more information. """ import unittest try: import coverage except ImportError: coverage = None import sys if coverage: coverage.erase() coverage.start() coverage.exclude('if __name__ == "__main__":') import dabo.ui dabo.ui.loadUI('wx') import db import biz import lib import ui suiteList = [db.suite(), biz.suite(), lib.suite(), ui.suite()] #import any tests for the main dabo folder import Test_dColors suiteList.append(unittest.TestLoader().loadTestsFromModule(Test_dColors)) import Test_dObject
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 main(self, *args): # # Strip arguments from the command line, so our # args do not confuse another app or library. # sys.argv = [ sys.argv[0] ] # # If they asked for a list of the tests, print that # first. # if self.list_mode: module_tree = self.getModuleTree(args) success = self.listTests(module_tree) # # If they asked for a list of test categories, # print that here. # elif self.list_categories_mode: module_tree = self.getModuleTree(args) success = self.listCategories(module_tree) # # If they asked to have tests run, do that # last. # elif self.run_mode: if coverage and self.code_coverage: # # Possibly override the coverage filename # if self.coverage_filename: self.statusMessage('Writing coverage output to %s' % self.coverage_filename) import os os.environ['COVERAGE_FILE'] = self.coverage_filename # # Clean up in case we have previous coverage data # coverage.erase() # # Add exclude patterns # for pattern in self.coverage_exclude_patterns: coverage.exclude(pattern) # # Start code coverage counter # coverage.start() # # Get the module tree. This needs to be done *after* # coverage monitoring is started so we monitor those # modules, too. # module_tree = self.getModuleTree(args) # # Run the tests # result = self.runTests(module_tree) if coverage and self.code_coverage: # # Stop coverage counter and save its results # coverage.stop() coverage.the_coverage.save() # # Report our success/failure # success = result.wasSuccessful() return success
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(list(modules.values()), show_missing=1) if excludes: message = "The following packages or modules were excluded:" print("") print(message) for e in excludes: print(e) print("") if errors: message = "There were problems with the following packages " message += "or modules:" print("") print(message) for e in errors: print(e) print("") 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(list(modules.values()), outdir) print("") print("HTML reports were output to '%s'" %outdir) datafile = settings.COVERAGE_REPORT_DATA_FILE if datafile: covered_files = coverage._the_coverage.data.lines.keys() covered_arcs = coverage._the_coverage.data.arcs.keys() modfiles = [] for module in modules.values(): modfiles.append(module.__file__.replace('.pyc', '.py')) for filename in covered_files: if not filename in modfiles and filename in coverage._the_coverage.data.lines: del coverage._the_coverage.data.lines[filename] for filename in covered_arcs: if not filename in modfiles and filename in coverage._the_coverage.data.arcs: del coverage._the_coverage.data.arcs[filename] coverage._the_coverage.data.usefile(True) coverage._the_coverage.data.write_file(datafile) return results
#!/usr/bin/env python """ Inyoka Testrunner ~~~~~~~~~~~~~~~~~ Note, that running the tests is not available via manage-inyoka.py, cause we import some stuff there and that does kill coverage. :copyright: 2009-2011 by the Inyoka Team, see AUTHORS for more details. :license: GNU GPL, see LICENSE for more details. """ import nose, os, sys import coverage coverage.erase() coverage.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]') coverage.exclude('__repr__.*') coverage.coverage(auto_data=True, branch=True) coverage.start() # set the configuration file if 'INYOKA_CONFIG' not in os.environ: os.environ['INYOKA_CONFIG'] = 'inyoka_tests.ini' from inyoka.core.test import InyokaPlugin, FuturePlugin from inyoka.core.database import refresh_engine def run_suite(module='inyoka'): # We need debug set to True for our tests from inyoka.context import ctx ctx.cfg['debug'] = 0
self.delivery_tag = None if __name__ == '__main__': import sys import doctest try: import coverage except ImportError: print >> sys.stderr, " [*] python-coverage not found" coverage = None if coverage: coverage.erase() coverage.start() coverage.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]') import amqpqueue modules = [amqpqueue] for module in modules: doctest.testmod(module) if coverage: coverage.stop() coverage.report(modules, ignore_errors=1, show_missing=1) coverage.erase()
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. """ ############################## setup_funcs, teardown_funcs = get_test_enviroment_functions() # Prepare django for testing. setup_test_environment(setup_funcs) old_db_name = settings.DATABASE_NAME connection.creation.create_test_db(verbosity, autoclobber=not interactive) # Pretend it's a production environment. settings.DEBUG = False nose_argv = ['nosetests'] if hasattr(settings, 'NOSE_ARGS'): nose_argv.extend(settings.NOSE_ARGS) # Everything after '--' is passed to nose. if '--' in sys.argv: hyphen_pos = sys.argv.index('--') nose_argv.extend(sys.argv[hyphen_pos + 1:]) if verbosity >= 1: print ' '.join(nose_argv) ############################ coverage.use_cache(0) for e in settings.COVERAGE_CODE_EXCLUDES: coverage.exclude(e) coverage.start() nose.run(argv=nose_argv) #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) print >>sys.stdout print >>sys.stdout, "HTML reports were output to '%s'" %outdir ########################### # Clean up django. connection.creation.destroy_test_db(old_db_name, verbosity) teardown_test_environment(teardown_funcs) ########################### return results