Python coverage.coverage() Examples

The following are 30 code examples of coverage.coverage(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module coverage , or try the search function .
Example #1
Source File: manage.py    From circleci-demo-python-flask with MIT License 9 votes vote down vote up
def test(coverage=False):
    """Run the unit tests."""
    if coverage and not os.environ.get('FLASK_COVERAGE'):
        import sys
        os.environ['FLASK_COVERAGE'] = '1'
        os.execvp(sys.executable, [sys.executable] + sys.argv)
    import unittest
    import xmlrunner
    tests = unittest.TestLoader().discover('tests')
    # run tests with unittest-xml-reporting and output to $CIRCLE_TEST_REPORTS on CircleCI or test-reports locally
    xmlrunner.XMLTestRunner(output=os.environ.get('CIRCLE_TEST_REPORTS','test-reports')).run(tests)
    if COV:
        COV.stop()
        COV.save()
        print('Coverage Summary:')
        COV.report()
        basedir = os.path.abspath(os.path.dirname(__file__))
        covdir = os.path.join(basedir, 'tmp/coverage')
        COV.html_report(directory=covdir)
        print('HTML version: file://%s/index.html' % covdir)
        COV.erase() 
Example #2
Source File: cover.py    From locality-sensitive-hashing with MIT License 6 votes vote down vote up
def report(self, stream):
        """
        Output code coverage report.
        """
        log.debug("Coverage report")
        self.coverInstance.stop()
        self.coverInstance.combine()
        self.coverInstance.save()
        modules = [module
                    for name, module in sys.modules.items()
                    if self.wantModuleCoverage(name, module)]
        log.debug("Coverage report will cover modules: %s", modules)
        self.coverInstance.report(modules, file=stream)

        import coverage
        if self.coverHtmlDir:
            log.debug("Generating HTML coverage report")
            try:
                self.coverInstance.html_report(modules, self.coverHtmlDir)
            except coverage.misc.CoverageException, e:
                log.warning("Failed to generate HTML report: %s" % str(e)) 
Example #3
Source File: testing.py    From ok-client with Apache License 2.0 6 votes vote down vote up
def analyze(self, suite, case, examples):
        failed, attempted = self.run_examples(examples)
        self.cov.stop()
        passed = attempted - failed
        format.print_test_progress_bar( '{} summary'.format(self.tstfile_name), 
                                        passed, failed, verbose=self.verb)
        # only support test coverage stats when running everything
        if not suite:
            self.print_coverage()
            if self.args.coverage:
                if self.lines_exec == self.lines_total:
                    print("Maximum coverage achieved! Great work!")
                else:
                    self.give_suggestions()
        return {'suites_total' : self.num_suites, 'cases_total': self.num_cases, 
                'exs_failed' : failed, 'exs_passed' : passed, 'attempted' : attempted,
                'actual_cov' : self.lines_exec, 'total_cov' : self.lines_total} 
Example #4
Source File: manage.py    From flasky-first-edition with MIT License 6 votes vote down vote up
def test(coverage=False):
    """Run the unit tests."""
    if coverage and not os.environ.get('FLASK_COVERAGE'):
        import sys
        os.environ['FLASK_COVERAGE'] = '1'
        os.execvp(sys.executable, [sys.executable] + sys.argv)
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)
    if COV:
        COV.stop()
        COV.save()
        print('Coverage Summary:')
        COV.report()
        basedir = os.path.abspath(os.path.dirname(__file__))
        covdir = os.path.join(basedir, 'tmp/coverage')
        COV.html_report(directory=covdir)
        print('HTML version: file://%s/index.html' % covdir)
        COV.erase() 
Example #5
Source File: manage.py    From Dockerized-Flask with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def cov():
    """
    Runs the unit tests and generates a coverage report on success.

    While the application is running, you can run the following command in a new terminal:
    'docker-compose run --rm flask python manage.py cov' to run all the tests in the
    'tests' directory. If all the tests pass, it will generate a coverage report.

    :return int: 0 if all tests pass, 1 if not
    """

    tests = unittest.TestLoader().discover('tests')
    result = unittest.TextTestRunner(verbosity=2).run(tests)
    if result.wasSuccessful():
        COV.stop()
        COV.save()
        print('Coverage Summary:')
        COV.report()
        COV.html_report()
        COV.erase()
        return 0
    else:
        return 1 
Example #6
Source File: manage.py    From Dockerized-Flask with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test():
    """
    Runs the unit tests without generating a coverage report.

    Enter 'docker-compose run --rm flask python manage.py test' to run all the tests in the
    'tests' directory, with no coverage report.

    :return int: 0 if all tests pass, 1 if not
    """

    tests = unittest.TestLoader().discover('tests', pattern='test*.py')
    result = unittest.TextTestRunner(verbosity=2).run(tests)
    if result.wasSuccessful():
        return 0
    else:
        return 1 
Example #7
Source File: manage.py    From Dockerized-Flask with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_one(test_file):
    """
    Runs the unittest without generating a coverage report.

    Enter 'docker-compose run --rm flask python manage.py test_one <NAME_OF_FILE>' to run only
    one test file in the 'tests' directory. It provides no coverage report.

    Example: 'docker-compose run --rm flask python manage.py test_one test_website'
    Note that you do not need to put the extension of the test file.

    :return int: 0 if all tests pass, 1 if not
    """

    tests = unittest.TestLoader().discover('tests', pattern=test_file + '.py')
    result = unittest.TextTestRunner(verbosity=2).run(tests)
    if result.wasSuccessful():
        return 0
    else:
        return 1

# Starts the Flask app. 
Example #8
Source File: manage.py    From flasky-with-celery with MIT License 6 votes vote down vote up
def test(coverage=False):
    """Run the unit tests."""
    if coverage and not os.environ.get('FLASK_COVERAGE'):
        import sys
        os.environ['FLASK_COVERAGE'] = '1'
        os.execvp(sys.executable, [sys.executable] + sys.argv)
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)
    if COV:
        COV.stop()
        COV.save()
        print('Coverage Summary:')
        COV.report()
        basedir = os.path.abspath(os.path.dirname(__file__))
        covdir = os.path.join(basedir, 'tmp/coverage')
        COV.html_report(directory=covdir)
        print('HTML version: file://%s/index.html' % covdir)
        COV.erase() 
Example #9
Source File: manage.py    From braindump with MIT License 6 votes vote down vote up
def test(coverage=False):
    """Run the unit tests."""
    import sys
    if coverage and not os.environ.get('FLASK_COVERAGE'):
        os.environ['FLASK_COVERAGE'] = '1'
        os.execvp(sys.executable, [sys.executable] + sys.argv)
    import unittest
    import xmlrunner
    tests = unittest.TestLoader().discover('tests')
    results = xmlrunner.XMLTestRunner(output='test-reports').run(tests)
    if COV:
        COV.stop()
        COV.save()
        print('Coverage Summary:')
        COV.report()
        basedir = os.path.abspath(os.path.dirname(__file__))
        covdir = os.path.join(basedir, 'test-reports/coverage')
        COV.html_report(directory=covdir)
        print('HTML version: file://%s/index.html' % covdir)
        COV.erase()
    if (len(results.failures) > 0 or len(results.errors) > 0):
        sys.exit(1) 
Example #10
Source File: test_result.py    From green with MIT License 6 votes vote down vote up
def test_stopTestRun(self, mock_printErrors):
        """
        We ignore coverage's error about not having anything to cover.
        """
        self.args.cov = MagicMock()
        self.args.cov.stop = MagicMock(
            side_effect=CoverageException("Different Exception")
        )
        self.args.run_coverage = True
        gtr = GreenTestResult(self.args, GreenStream(self.stream))
        gtr.startTestRun()
        self.assertRaises(CoverageException, gtr.stopTestRun)

        self.args.cov.stop = MagicMock(
            side_effect=CoverageException("No data to report")
        ) 
Example #11
Source File: test_concurrency.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def measurable_line(l):
    """Is this a line of code coverage will measure?

    Not blank, not a comment, and not "else"
    """
    l = l.strip()
    if not l:
        return False
    if l.startswith('#'):
        return False
    if l.startswith('else:'):
        return False
    if env.JYTHON and l.startswith(('try:', 'except:', 'except ', 'break', 'with ')):
        # Jython doesn't measure these statements.
        return False                    # pragma: only jython
    return True 
Example #12
Source File: manage.py    From SAP-B1-RESTful with MIT License 6 votes vote down vote up
def test(coverage=False):
    """Run the unit tests."""
    if coverage and not os.environ.get('FLASK_COVERAGE'):
        import sys
        os.environ['FLASK_COVERAGE'] = '1'
        os.execvp(sys.executable, [sys.executable] + sys.argv)
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)
    if COV:
        COV.stop()
        COV.save()
        print('Coverage Summary:')
        COV.report()
        basedir = os.path.abspath(os.path.dirname(__file__))
        covdir = os.path.join(basedir, 'tmp/coverage')
        COV.html_report(directory=covdir)
        print(('HTML version: file://%s/index.html' % covdir))
        COV.erase() 
Example #13
Source File: test_concurrency.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def cant_trace_msg(concurrency, the_module):
    """What might coverage.py say about a concurrency setting and imported module?"""
    # In the concurrency choices, "multiprocessing" doesn't count, so remove it.
    if "multiprocessing" in concurrency:
        parts = concurrency.split(",")
        parts.remove("multiprocessing")
        concurrency = ",".join(parts)

    if the_module is None:
        # We don't even have the underlying module installed, we expect
        # coverage to alert us to this fact.
        expected_out = (
            "Couldn't trace with concurrency=%s, "
            "the module isn't installed.\n" % concurrency
        )
    elif env.C_TRACER or concurrency == "thread" or concurrency == "":
        expected_out = None
    else:
        expected_out = (
            "Can't support concurrency=%s with PyTracer, "
            "only threads are supported\n" % concurrency
        )
    return expected_out 
Example #14
Source File: manage.py    From USSD-Python-Demo with MIT License 6 votes vote down vote up
def test(coverage=False):
    """Run the unit tests."""
    if coverage and not os.environ.get('FLASK_COVERAGE'):
        import sys
        os.environ['FLASK_COVERAGE'] = '1'
        os.execvp(sys.executable, [sys.executable] + sys.argv)
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)
    if COV:
        COV.stop()
        COV.save()
        print("Coverage Summary:")
        COV.report()
        basedir = os.path.abspath(os.path.dirname(__file__))
        covdir = os.path.join(basedir, 'temp/coverage')
        COV.html_report(directory=covdir)
        print('HTML version: file://{covdir}index.html'.format(covdir=covdir))
        COV.erase() 
Example #15
Source File: runtests.py    From django-rest-framework-docs with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def run_tests_coverage():
    if __name__ == "__main__":
        os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
        django.setup()
        TestRunner = get_runner(settings)
        test_runner = TestRunner()

        # Setup Coverage
        cov = coverage(source=["rest_framework_docs"], omit=["rest_framework_docs/__init__.py"])
        cov.start()

        failures = test_runner.run_tests(["tests"])

        if bool(failures):
            cov.erase()
            sys.exit("Tests Failed. Coverage Cancelled.")

        # If success show coverage results
        cov.stop()
        cov.save()
        cov.report()
        cov.html_report(directory='covhtml') 
Example #16
Source File: manage.py    From flask-jwt-auth with MIT License 6 votes vote down vote up
def cov():
    """Runs the unit tests with coverage."""
    tests = unittest.TestLoader().discover('project/tests')
    result = unittest.TextTestRunner(verbosity=2).run(tests)
    if result.wasSuccessful():
        COV.stop()
        COV.save()
        print('Coverage Summary:')
        COV.report()
        basedir = os.path.abspath(os.path.dirname(__file__))
        covdir = os.path.join(basedir, 'tmp/coverage')
        COV.html_report(directory=covdir)
        print('HTML version: file://%s/index.html' % covdir)
        COV.erase()
        return 0
    return 1 
Example #17
Source File: pytest_plugin.py    From smother with MIT License 6 votes vote down vote up
def __init__(self, options):
        self.coverage = coverage.coverage(
            source=options.smother_source,
            config_file=options.smother_config,
        )

        # The unusual import statement placement is so that
        # smother's own test suite can measure coverage of
        # smother import statements
        self.coverage.start()
        from smother.control import Smother
        self.smother = Smother(self.coverage)

        self.output = options.smother_output
        self.append = options.smother_append
        self.cover_report = options.smother_cover
        self.first_test = True 
Example #18
Source File: pytest_plugin.py    From smother with MIT License 6 votes vote down vote up
def pytest_addoption(parser):
    """Add options to control coverage."""

    group = parser.getgroup(
        'smother', 'smother reporting')
    group.addoption('--smother', action='append', default=[], metavar='path',
                    nargs='?', const=True, dest='smother_source',
                    help='measure coverage for filesystem path '
                    '(multi-allowed)')
    group.addoption('--smother-config', action='store', default='.coveragerc',
                    help='config file for coverage, default: .coveragerc')
    group.addoption('--smother-append', action='store_true', default=False,
                    help='append to existing smother report, '
                         'default: False')
    group.addoption('--smother-output', action='store', default='.smother',
                    help='output file for smother data. '
                         'default: .smother')
    group.addoption('--smother-cover', action='store_true', default=False,
                    help='Create a vanilla coverage file in addition to '
                         'the smother output') 
Example #19
Source File: run_tests.py    From python-sdk with MIT License 5 votes vote down vote up
def run(self):
        try:
            import coverage
        except ImportError:
            print("Could not import coverage. Please install it and try again.")
            exit(1)
        cov = coverage.coverage(source=['devo'])
        cov.start()
        run_test_suite()
        cov.stop()
        cov.html_report(directory='coverage_report') 
Example #20
Source File: manage.py    From my-dev-space with MIT License 5 votes vote down vote up
def test():
    """Runs the unit tests without test coverage."""
    tests = unittest.TestLoader().discover("project/tests", pattern="test*.py")
    result = unittest.TextTestRunner(verbosity=2).run(tests)
    if result.wasSuccessful():
        return 0
    return 1 
Example #21
Source File: manage.py    From flask-jwt-auth with MIT License 5 votes vote down vote up
def test():
    """Runs the unit tests without test coverage."""
    tests = unittest.TestLoader().discover('project/tests', pattern='test*.py')
    result = unittest.TextTestRunner(verbosity=2).run(tests)
    if result.wasSuccessful():
        return 0
    return 1 
Example #22
Source File: manage.py    From cookiecutter-flask-skeleton with MIT License 5 votes vote down vote up
def cov():
    """Runs the unit tests with coverage."""
    tests = unittest.TestLoader().discover("project/tests")
    result = unittest.TextTestRunner(verbosity=2).run(tests)
    if result.wasSuccessful():
        COV.stop()
        COV.save()
        print("Coverage Summary:")
        COV.report()
        COV.html_report()
        COV.erase()
        sys.exit(0)
    else:
        sys.exit(1) 
Example #23
Source File: test.py    From pscript with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def show_coverage_html():
    import webbrowser
    from coverage import coverage
    
    print('Generating HTML...')
    os.chdir(ROOT_DIR)
    cov = coverage(auto_data=False, branch=True, data_suffix=None,
                   source=[NAME])  # should match testing/_coverage.py
    cov.load()
    cov.html_report()
    print('Done, launching browser.')
    fname = os.path.join(os.getcwd(), 'htmlcov', 'index.html')
    if not os.path.isfile(fname):
        raise IOError('Generated file not found: %s' % fname)
    webbrowser.open_new_tab(fname) 
Example #24
Source File: test.py    From pscript with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def show_coverage_term():
    from coverage import coverage
    cov = coverage(auto_data=False, branch=True, data_suffix=None,
                   source=[NAME])  # should match testing/_coverage.py
    cov.load()
    cov.report() 
Example #25
Source File: testrunner.py    From wradlib with MIT License 5 votes vote down vote up
def single_suite_process(queue, test, verbosity, **kwargs):
    test_cov = kwargs.pop("coverage", 0)
    test_nb = kwargs.pop("notebooks", 0)
    if test_cov and not test_nb:
        cov = coverage.coverage()
        cov.start()
    all_success = 1
    for ts in test:
        if ts.countTestCases() != 0:
            res = unittest.TextTestRunner(verbosity=verbosity).run(ts)
            all_success = all_success & res.wasSuccessful()
    if test_cov and not test_nb:
        cov.stop()
        cov.save()
    queue.put(all_success) 
Example #26
Source File: testrunner.py    From wradlib with MIT License 5 votes vote down vote up
def _runTest(self):
        kernel = "python%d" % sys.version_info[0]
        cur_dir = os.path.dirname(self.nbfile)

        with open(self.nbfile) as f:
            nb = nbformat.read(f, as_version=4)
            if self.cov:
                covdict = {
                    "cell_type": "code",
                    "execution_count": 1,
                    "metadata": {"collapsed": True},
                    "outputs": [],
                    "nbsphinx": "hidden",
                    "source": "import coverage\n"
                    "coverage.process_startup()\n"
                    "import sys\n"
                    'sys.path.append("{0}")\n'.format(cur_dir),
                }
                nb["cells"].insert(0, nbformat.from_dict(covdict))

            exproc = ExecutePreprocessor(kernel_name=kernel, timeout=600)

            try:
                run_dir = os.getenv("WRADLIB_BUILD_DIR", cur_dir)
                exproc.preprocess(nb, {"metadata": {"path": run_dir}})
            except CellExecutionError as e:
                raise e

        if self.cov:
            nb["cells"].pop(0)

        with io.open(self.nbfile, "wt") as f:
            nbformat.write(nb, f)

        self.assertTrue(True) 
Example #27
Source File: manage.py    From Angular-Flask-Docker-Skeleton with MIT License 5 votes vote down vote up
def cov():
    """Runs the unit tests with coverage."""
    tests = unittest.TestLoader().discover('tests', pattern='test*.py')
    result = unittest.TextTestRunner(verbosity=2).run(tests)
    if result.wasSuccessful():
        COV.stop()
        COV.save()
        print('Coverage Summary:')
        COV.report()
        COV.html_report(directory='tests/coverage')
        COV.erase()
        return 0
    return 1 
Example #28
Source File: manage.py    From Angular-Flask-Docker-Skeleton with MIT License 5 votes vote down vote up
def test():
    """Runs the unit tests without test coverage."""
    test_suite = unittest.TestLoader().discover('tests', pattern='test*.py')
    result = unittest.TextTestRunner(verbosity=2).run(test_suite)
    if result.wasSuccessful():
        return 0
    return 1 
Example #29
Source File: test.py    From flexx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def show_coverage_html():
    import webbrowser
    from coverage import coverage

    print('Generating HTML...')
    os.chdir(ROOT_DIR)
    cov = coverage(auto_data=False, branch=True, data_suffix=None,
                   source=[NAME])  # should match testing/_coverage.py
    cov.load()
    cov.html_report()
    print('Done, launching browser.')
    fname = os.path.join(os.getcwd(), 'htmlcov', 'index.html')
    if not os.path.isfile(fname):
        raise IOError('Generated file not found: %s' % fname)
    webbrowser.open_new_tab(fname) 
Example #30
Source File: test.py    From flexx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def show_coverage_term():
    from coverage import coverage
    cov = coverage(auto_data=False, branch=True, data_suffix=None,
                   source=[NAME])  # should match testing/_coverage.py
    cov.load()
    cov.report()