Ejemplo n.º 1
0
def report_coverage(packages):
    test_re = re.compile('.+test[^%s.]*\..+' % os.sep)
    coverage.stop()
    files = set()
    for name in sys.modules:
        mod = sys.modules.get(name)
        if mod is None:
            continue
        elif not is_in_packages(name, packages):
            continue
        elif is_in_packages(name, ['django']):
            continue

        filename = mod.__file__.replace('.pyc', '.py')

        if test_re.match(filename):
            continue

        st = os.stat(filename)
        if st.st_size > 1:
            files.add(filename)

    if files:
        coverage.report(list(files))
    coverage.erase()
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
def report_coverage(packages):
    test_re = re.compile('.+test[^%s.]*\..+' % os.sep)
    coverage.stop()
    files = set()
    for name in sys.modules:
        mod = sys.modules.get(name)
        if mod == None:
            continue
        elif not is_in_packages(name, packages):
            continue
        elif is_in_packages(name, ['django']):
            continue

        filename = mod.__file__.replace('.pyc', '.py')

        if test_re.match(filename):
            continue

        st = os.stat(filename)
        if st.st_size > 1:
            files.add(filename)

    if files:
        coverage.report(list(files))
    coverage.erase()
Ejemplo n.º 4
0
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()
Ejemplo n.º 5
0
    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)))
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    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)))
Ejemplo n.º 9
0
    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)))
Ejemplo n.º 10
0
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()
Ejemplo n.º 11
0
 def finalize_environment(self):
     root = soup.get_root() + "/conduit"
     modules = []
     for i in range(3):
         modules.extend(glob(root + "/*" * i + ".py"))
     coverage.report(modules, ignore_errors=1, show_missing=0)
     coverage.erase()
Ejemplo n.º 12
0
 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()
Ejemplo n.º 13
0
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
Ejemplo n.º 14
0
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)
Ejemplo n.º 15
0
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)
Ejemplo n.º 16
0
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))
Ejemplo n.º 17
0
 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
Ejemplo n.º 18
0
    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
Ejemplo n.º 19
0
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()
Ejemplo n.º 20
0
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()
Ejemplo n.º 21
0
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()
Ejemplo n.º 22
0
Archivo: run.py Proyecto: alon/polinax
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)
Ejemplo n.º 23
0
    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.
        self.start_import_stop(coverage, modname)
Ejemplo n.º 24
0
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()
Ejemplo n.º 25
0
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('../pytof/*.py')
        modules.remove('../pytof/__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))
Ejemplo n.º 26
0
    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.
        self.start_import_stop(coverage, modname)
Ejemplo n.º 27
0
def my_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.

    Returns the number of tests that failed.
    """
    coveragemodules = []
    if hasattr(settings, 'COVERAGE_MODULES'):
        coveragemodules = settings.COVERAGE_MODULES
    if coveragemodules:
        coverage.start()

    result = run_tests(test_labels, verbosity, interactive, extra_tests)

    if coveragemodules:
        coverage.stop()
        coveragedir = './build/coverage'
        if hasattr(settings, 'COVERAGE_DIR'):
            coveragedir = settings.COVERAGE_DIR
        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")
            coverage_color.colorize_file(f, outstream=fp, not_covered=mf)
            fp.close()
        coverage.the_coverage.report(modules, show_missing=1)
        coverage.erase()

    return result
Ejemplo n.º 28
0
    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
Ejemplo n.º 29
0
    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.
        self.start_import_stop(coverage, "mycode")

        _, statements, missing, missingtext = coverage.analysis("mycode.py")
        self.assertEqual(statements, [1,2,3,4,5])
        self.assertEqual(missing, [4])
        self.assertEqual(missingtext, "4")
Ejemplo n.º 30
0
    def doReportWork(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_module(modname)             # pragma: recursive coverage
        coverage.stop()                         # pragma: recursive coverage
Ejemplo n.º 31
0
def coverModules():
    sys.stdout.write('Writing coverage...')
    sys.stdout.flush()
    here = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    from SQLObject import DBConnection as tmp
    there = os.path.dirname(os.path.abspath(tmp.__file__))
    for name, mod in sys.modules.items():
        if not mod:
            continue
        try:
            modFile = os.path.abspath(mod.__file__)
        except AttributeError:
            # Probably a C extension
            continue
        if modFile.startswith(here) or modFile.startswith(there):
            writeCoverage(mod, there, os.path.join(here, 'SQLObject'))
    coverage.erase()
    sys.stdout.write('done.\n')
Ejemplo n.º 32
0
    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
Ejemplo n.º 33
0
    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.
        self.start_import_stop(coverage, "mycode")

        _, statements, missing, missingtext = coverage.analysis("mycode.py")
        self.assertEqual(statements, [1, 2, 3, 4, 5])
        self.assertEqual(missing, [4])
        self.assertEqual(missingtext, "4")
Ejemplo n.º 34
0
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()
Ejemplo n.º 35
0
    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")
Ejemplo n.º 36
0
    def testSimple(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_module("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")
Ejemplo n.º 37
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 run(self):
        try:
            # Use system 'coverage' if available
            import coverage
            use_coverage = True
        except:
            use_coverage = False

        tests = TestLoader().loadTestsFromNames(self._testfiles)
        t = TextTestRunner(verbosity=1)

        if use_coverage:
            coverage.erase()
            coverage.start()

        result = t.run(tests)

        if use_coverage:
            coverage.stop()

        sys.exit(int(bool(len(result.failures) > 0 or
                          len(result.errors) > 0)))
Ejemplo n.º 39
0
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()
Ejemplo n.º 40
0
    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)
Ejemplo n.º 41
0
def main():
    try:
        cov_idx = sys.argv.index('--cov')
    except ValueError:
        coverage = None
    else:
        del sys.argv[cov_idx]
        try:
            import coverage
        except ImportError:
            sys.stderr.write('''\
Please install "coverage" module to collect coverage. Try typing:
sudo easy_install coverage
''')
            sys.exit(1)
        import coverage_color
        coverage.start()
    global akshell, script
    akshell = __import__('akshell')
    script = __import__('script')
    try:
        server_idx = sys.argv.index('--server')
    except ValueError:
        pass
    else:
        akshell.SERVER = sys.argv[server_idx + 1]
        del sys.argv[server_idx : server_idx + 2]
    try:
        unittest.main(defaultTest='suite')
    finally:
        if coverage:
            coverage.stop()
            for module in (akshell, script):
                path, stmts_, missing_, missing_str = coverage.analysis(module)
                with open('coverage_%s.html' % module.__name__, 'w') as f:
                    coverage_color.colorize_file(path, f, missing_str)
            coverage.report([akshell, script], show_missing=False)
            coverage.erase()
Ejemplo n.º 42
0
    def run(self):
        try:
            import coverage
            use_coverage = True
        except:
            # Use system 'coverage' if available
            use_coverage = False

        tests = TestLoader().loadTestsFromNames(self._testfiles)
        t = TextTestRunner(verbosity = 1)

        if use_coverage:
            coverage.erase()
            coverage.start()

        result = t.run(tests)

        if use_coverage:
            coverage.stop()

        if len(result.failures) > 0 or len(result.errors) > 0:
            sys.exit(1)
        else:
            sys.exit(0)
Ejemplo n.º 43
0
        except:
            return 1

        x = coverage.analysis(_OGMultiPoint)
        assert x[2] == [], "Coverage is less than 100%"


def testSuite():
    return unittest.makeSuite(OGMultiPointTest, 'test')


if __name__ == "__main__":
    if os.environ.get('USECOVERAGE') == '1':
        try:
            import coverage
            coverage.erase()
            coverage.start()
            COVERAGE = 1
            import _OGMultiPoint
            from _OGMultiPoint import *

        except:
            print "Error setting up coverage checking"
            COVERAGE = 0
    else:
        COVERAGE = 0

    if os.environ.get('USEPYCHECK') == '1':
        try:
            import pychecker.checker
        except:
Ejemplo n.º 44
0
#!/usr/bin/env python
"""Coverage and unittest for datamatrix and QR Code library"""

import sys
import unittest

import coverage # get it from http://www.nedbatchelder.com/code/modules/coverage.html

exitcode = 0

coverage.erase()
coverage.start()

import hubarcode.qrcode.qrcodetest
suite = unittest.TestLoader().loadTestsFromName('qrcode.qrcodetest.MatrixTest')
results = unittest.TextTestRunner().run(suite)
if not results.wasSuccessful():
    exitcode += 1

import hubarcode.datamatrix.matrixtest
suite = unittest.TestLoader().loadTestsFromName('datamatrix.matrixtest.MatrixTest')
results = unittest.TextTestRunner().run(suite)
if not results.wasSuccessful():
    exitcode += 1

import hubarcode.ean13.eantest
suite = unittest.TestLoader().loadTestsFromName('ean13.eantest.EAN13Test')
results = unittest.TextTestRunner().run(suite)
if not results.wasSuccessful():
    exitcode += 1
Ejemplo n.º 45
0
def run_tests(test_labels, verbosity=1, interactive=True, failfast=False, 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
                   hasattr(settings, 'COVERAGE_APPS') or
                   bool(test_labels))
    if do_coverage:
        coverage.erase()
        coverage.start()
    
    DjangoTestSuiteRunner = None
    try:
        from django.test.simple import DjangoTestSuiteRunner
    except ImportError:
        from django.test import simple
    
    if DjangoTestSuiteRunner:
        testrunner = DjangoTestSuiteRunner(verbosity=verbosity, interactive=interactive, failfast=failfast)
        retval = testrunner.run_tests(test_labels, extra_tests)
    else:
        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 test_labels or hasattr(settings, 'COVERAGE_APPS'):
            # apps entered at the command line prompt override those specified in settings
            labels = test_labels or settings.COVERAGE_APPS
            for label in labels:
                label = label.split('.')[0] #remove test class or test method from label
                pkg = _get_app_package(label)
                modules.extend(_package_modules(*pkg))
        elif hasattr(settings, 'COVERAGE_MODULES'):
            modules = [__import__(module, {}, {}, ['']) for module in settings.COVERAGE_MODULES]

        if hasattr(settings, 'COVERAGE_EXCLUDE_MODULES'):
            for exclude_module_name in settings.COVERAGE_EXCLUDE_MODULES:
                # Test designed to avoid accidentally removing a module whose
                # name is prefixed by an excluded module name, but still remove
                # submodules
                modules = [module for module in modules
                    if not module.__name__ == exclude_module_name and
                    not module.__name__.startswith(exclude_module_name + '.')]

        coverage.report(modules, show_missing=1)

    return retval
Ejemplo n.º 46
0
 def callback(option, opt_str, value, parser):
     atexit.register(report_coverage)
     coverage.erase()
     coverage.start()
Ejemplo n.º 47
0
Archivo: run.py Proyecto: saif1413/L4OS
def start_coverage():
    """Setup coverage"""
    coverage.use_cache(False)
    coverage.erase()
    coverage.start()
Ejemplo n.º 48
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."""
  print "cs test_runner_with_coverage " + str(getattr(settings, 'COVERAGE_MODULES')) + " , " + str(test_labels)
  
  coverage.use_cache(0) # Do not cache any of the coverage.py stuff
  coverage.start()
  
  # Start code coverage before anything else if necessary
#   if hasattr(settings, 'COVERAGE_MODULES') and not test_labels:
#     print "cs coverage start"
#     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()

  if not os.path.exists(settings.COVERAGE_DIR):
    os.makedirs(settings.COVERAGE_DIR)

  coverage.html_report(directory='coverage')

  coverage.erase();

  # 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 '----------------------------------------------------------------------'

#   coverage_modules = []
#   for module in settings.COVERAGE_MODULES:
#     coverage_modules.append(__import__(module, globals(), locals(), ['']))
#     
#   print "cs coverage_modules: " + str(coverage_modules)
    
#   for module_string in settings.COVERAGE_MODULES:
#     module = __import__(module_string, globals(), locals(), [""])
#     f,s,m,mf = coverage.analysis(module)    
#     fp = file(os.path.join(settings.COVERAGE_DIR, module_string + ".html"), "wb")
#     coverage_color.colorize_file(f, outstream=fp, not_covered=mf)
#     fp.close()
#   coverage.report(coverage_modules, show_missing=1)

#   coverage.erase();

  # Report code coverage metrics
#   if hasattr(settings, 'COVERAGE_MODULES') and not test_labels:
#     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
Ejemplo n.º 49
0
    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
Ejemplo n.º 50
0
def run_tests_with_coverage(test_labels, verbosity=1, interactive=True,
                            failfast=False, 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.

    Returns the number of tests that failed.
    """
    setup_test_environment()

    settings.DEBUG = False
    suite = unittest.TestSuite()

    modules_to_cover = []

    # start doing some coverage action
    coverage.erase()
    coverage.start()

    # if passed a list of tests...
    if test_labels:
        for label in test_labels:
            if '.' in label:
                suite.addTest(build_test(label))
            else:
                app = get_app(label)
                suite.addTest(build_suite(app))
    # ...otherwise use all installed
    else:
        for app in get_apps():
            # skip apps named "Django" because they use a database
            if not app.__name__.startswith('django'):
                # get the actual app name
                app_name = app.__name__.replace('.models', '')
                # get a list of the files inside that module
                files = glob('%s/*.py' % app_name)
                # remove models because we don't use them, stupid
                new_files = [i for i in files if not i.endswith('models.py')]
                modules_to_cover.extend(new_files)
                # actually test the file
                suite.addTest(build_suite(app))

    for test in extra_tests:
        suite.addTest(test)

    result = unittest.TextTestRunner(verbosity=verbosity).run(suite)

    teardown_test_environment()

    # stop coverage
    coverage.stop()

    # output results
    print ''
    print '--------------------------'
    print 'Unit test coverage results'
    print '--------------------------'
    print ''
    coverage.report(modules_to_cover, show_missing=1)

    return len(result.failures) + len(result.errors)