Esempio n. 1
0
def test(package, coverage=True, tests=(), verbose=False, fail_fast=False):
    cwd = os.getcwd()
    where = os.path.join(cwd, package.replace('.', os.sep))
    top_level_dir = cwd

    coverage = coverage and not tests
    verbosity = 2 if verbose else 1

    if coverage:
        from coverage import Coverage
        cover = Coverage(branch=True, source=[where])
        cover.start()

    loader = unittest.TestLoader()
    if tests:
        suite = loader.loadTestsFromNames(tests)
    else:
        suite = loader.discover(where, top_level_dir=top_level_dir)

    runner = unittest.TextTestRunner(verbosity=verbosity, failfast=fail_fast)
    runner.run(suite)

    if coverage:
        cover.stop()
        cover.report()
Esempio n. 2
0
def test(*tests, coverage=True, verbose=False, fail_fast=False):
    top_level_dir = Path.cwd()

    where = top_level_dir
    for segment in top_level_dir.name.split('.'):
        where = where / segment

    coverage = coverage and not tests
    verbosity = 2 if verbose else 1

    if coverage:
        from coverage import Coverage
        cover = Coverage(branch=True, source=[where])
        cover.start()

    loader = unittest.TestLoader()
    if tests:
        suite = loader.loadTestsFromNames(tests)
    else:
        suite = loader.discover(where, top_level_dir=top_level_dir)

    runner = unittest.TextTestRunner(verbosity=verbosity, failfast=fail_fast)
    runner.run(suite)

    if coverage:
        cover.stop()
        cover.report()
Esempio n. 3
0
def run_tests():
    config = utils.get_config()
    cov_conf_file = config.get('file_locations',
                               'coverage_in_conf',
                               fallback='config/.coveragerc')
    cov_out_file = config.get('file_locations',
                              'coverage_out_dir',
                              fallback='coverage')

    cov = Coverage(config_file=cov_conf_file)
    cov.start()

    result = grab_test_results()
    if not result.wasSuccessful():
        print("Error running unit tests ...")
        exit(1)

    cov.stop()
    cov.save()
    print('Coverage Summary:')
    cov.report()
    cov.html_report(directory=cov_out_file)
    print('HTML version: file://{0}/{1}/index.html'.format(
        os.getcwd(), cov_out_file))
    cov.erase()
    exit(0)
Esempio n. 4
0
def test(config,
         tests=(),
         fail_fast=False,
         with_coverage=True,
         with_lint=True):
    if tests:
        num_tests = len(tests)
        s = '' if num_tests == 1 else 's'
        printer.header('Running {num_tests} test{s}...'.format_map(locals()))
    else:
        coverage_message = ' with coverage' if with_coverage else ''
        printer.header('Running tests{coverage_message}...'.format_map(
            locals()))

    runner = unittest.TextTestRunner(failfast=fail_fast)
    loader = unittest.TestLoader()

    if with_coverage:
        from coverage import Coverage
        coverage = Coverage(source=['runcommands'])
        coverage.start()

    if tests:
        for name in tests:
            runner.run(loader.loadTestsFromName(name))
    else:
        tests = loader.discover('.')
        result = runner.run(tests)
        if not result.errors:
            if with_coverage:
                coverage.stop()
                coverage.report()
            if with_lint:
                printer.header('Checking for lint...')
                lint(config)
Esempio n. 5
0
def generate_coverage_reports(reports_dir=COVERAGE_DIR, report="all"):
    '''
    This task combines all the coverage data files generated during all the tests and generates the coverage reports.
    :param report: type of the coverage report to be generated. Options: text|html|xml. Defaults to 'html'
    :param reports_dir: directory under which all coverage files to be found and reports to be generated. Defaults to reports/coverage
    '''

    print("\nInitiating code coverage analysis ...")
    coverage_obj = Coverage(data_file="%s/coverage" % reports_dir)
    coverage_obj.combine()
    coverage_obj.save()

    if report == "all":
        with open("%s/coverage.txt" % reports_dir, "w") as cov_rep:
            coverage_obj.report(file=cov_rep, show_missing=True)
            print("Generated Code-Coverage text report at %s/coverage.txt\n" %
                  reports_dir)
        coverage_obj.html_report(directory=reports_dir)
        print("Generated Code-Coverage HTML report at %s\n" % reports_dir)
        coverage_obj.xml_report(outfile='%s/coverage.xml' % reports_dir)
        print("Generated Code-Coverage XML report at %s/coverage.xml\n" %
              reports_dir)
    elif report == "text":
        with open("%s/coverage.txt" % reports_dir, "w") as cov_rep:
            coverage_obj.report(file=cov_rep, show_missing=True)
            print("Generated Code-Coverage text report at %s/coverage.txt\n" %
                  reports_dir)
    elif report == "html":
        coverage_obj.html_report(directory=reports_dir)
        print("Generated Code-Coverage HTML report at %s\n" % reports_dir)
    elif report == "xml":
        coverage_obj.xml_report(outfile='%s/coverage.xml' % reports_dir)
        print("Generated Code-Coverage XML report at %s/coverage.xml\n" %
              reports_dir)
Esempio n. 6
0
def report(testSuite):
    cov = Coverage(branch=True)
    cov.start()
    for test_case in testSuite:
        TriangleApp.TriangleTester(test_case[1][0], test_case[1][1], test_case[1][2])
    cov.stop()
    cov.report(file=open('./tmp/results.txt', 'w'), show_missing=True)

    f = open('./tmp/results.txt', 'r')
    lines = f.readlines()
    imp_elm = []
    br_not_cover = 0
    elements = lines[2].split(' ')
    for e in elements:
        if e != '' and e[0].isdigit():
            e = e.strip(',')
            e = e.strip('\n')
            e = e.strip('%')
            imp_elm.append(e)

    for i in range(5, len(imp_elm)):
        if imp_elm[i].find('->') != -1:
            br_not_cover = br_not_cover + 1

    coverage_report = {'stmt': int(imp_elm[0]), 'miss': int(imp_elm[1]), 'branch': int(imp_elm[2]),
                       'br_par': int(imp_elm[3]), 'cover': int(imp_elm[4]), 'br_not_cover': br_not_cover}

    return coverage_report
Esempio n. 7
0
class TestsSuite:
    CONFIG_FILE = ".coveragerc"

    is_tests_suite_run: bool = False
    console_log_level: LogLevelEnum = LogLevelEnum.INFO
    file_log_level: LogLevelEnum = LogLevelEnum.DEBUG
    log_file_path: str = None

    __coverage: Coverage = None
    __is_coverage: bool

    def __init__(self, is_coverage: bool = False):
        self.__coverage = Coverage(config_file=self.CONFIG_FILE)
        self.__is_coverage = is_coverage
        TestsSuite.is_tests_suite_run = True
        init_logger(self.console_log_level, self.file_log_level)

    def run_tests(self):
        if self.__is_coverage:
            self.__coverage.start()

        tests = unittest.TestLoader().discover(start_dir='.',
                                               pattern='*_test.py')
        unittest.TextTestRunner(verbosity=2).run(tests)

        if self.__is_coverage:
            self.__coverage.stop()

    def create_report(self):
        self.__coverage.report()
        self.__coverage.json_report()
        self.__coverage.html_report()

    def erase_data(self):
        self.__coverage.erase()
Esempio n. 8
0
    def run_tests(self):
        " Run coverage on unit test "
        # need to import here cause we are in a venv
        import six
        from coverage import Coverage

        coverage = Coverage()
        coverage.start()

        # Purge modules under test from sys.modules. The test loader will
        # re-import them from the build location. Required when 2to3 is used
        # with namespace packages.
        if six.PY3 and getattr(self.distribution, 'use_2to3', False):
            module = self.test_suite.split('.')[0]
            if module in _namespace_packages:
                del_modules = []
                if module in sys.modules:
                    del_modules.append(module)
                module += '.'
                for name in sys.modules:
                    if name.startswith(module):
                        del_modules.append(name)
                list(map(sys.modules.__delitem__, del_modules))

        unittest_main(
            None, None, self._argv,
            testLoader=self._resolve_as_ep(self.test_loader),
            testRunner=self._resolve_as_ep(self.test_runner),
            exit=False,
        )

        coverage.stop()
        coverage.save()
        coverage.report(show_missing=False)
def coverage_analysis():
    cov = Coverage()
    cov.start()
    try:
        yield
    finally:
        cov.stop()
        cov.report()
        print("------  SAVING COVERAGE REPORTS ------ ")
        cov.xml_report(outfile=os.path.join(".", 'cobertura.xml'))
def run_unit_tests() -> int:
    cov = Coverage(include="app_google_groups/*")
    cov.start()
    loader = unittest.TestLoader()
    suite = loader.discover(start_dir=".", pattern="test_*.py")
    res = unittest.TextTestRunner().run(suite)
    cov.stop()
    try:
        cov.save()
        cov.report()
    except CoverageException as ce:
        print(str(ce))
        print("Don't forget to add some unit tests!")
    num_test_fails = len(res.failures) + len(res.errors)
    return num_test_fails
Esempio n. 11
0
def with_coverage(f, source, *, report=True, data=False):
    cov = Coverage(source=[source])
    cov.start()
    try:
        exit_code = f()
    finally:
        cov.stop()
    if not exit_code:
        if report:
            print()  # Print blank line.
            cov.report(show_missing=False)
            cov.html_report()
        if data:
            cov.save()
    return exit_code
Esempio n. 12
0
def coverage(ctx, html):
    """输出覆盖率测试"""
    cov = Coverage(source=["app", "commands", "routes/", "models"],
                   omit=["*__init__*"])
    cov.start()
    # 触发测试命令
    ctx.invoke(test)
    cov.stop()
    cov.save()

    click.echo('Coverage summary:')
    cov.report()
    if html:
        cov.html_report(directory="reports/coverage/")
    cov.erase()
Esempio n. 13
0
def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
    try:
        from django.core.management import execute_from_command_line
        from django.conf import settings

        # MyProject Customization: run coverage.py around tests automatically
        running_tests = (sys.argv[1] == 'test')
        if running_tests:
            from coverage import Coverage
            cov = Coverage()
            cov.erase()
            cov.start()

        if settings.DEBUG:
            if os.environ.get('RUN_MAIN') or os.environ.get('WERKZEUG_RUN_MAIN'):
                import ptvsd
                ptvsd.enable_attach(address = ('0.0.0.0', 3500))
                print("Attached remote debugger")
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

    if running_tests:
        cov.stop()
        cov.save()
        covered = cov.report()
        if covered < 100:
            sys.exit(1)
Esempio n. 14
0
def run_tests(with_coverage: arg(
    short_option="-c",
    inverse_short_option="-C",
    help="With coverage",
) = True, ):
    runner = unittest.TextTestRunner()
    loader = unittest.TestLoader()
    if with_coverage:
        coverage = Coverage(source=["./src"])
        coverage.start()
    tests = loader.discover("./tests")
    result = runner.run(tests)
    if with_coverage:
        coverage.stop()
        if not result.errors:
            coverage.report()
Esempio n. 15
0
def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

    # MyMoney Customization: run coverage.py around tests automatically
    try:
        command = sys.argv[1]
    except IndexError:
        command = "help"

    running_tests = (command == 'test')
    if running_tests:
        from coverage import Coverage
        cov = Coverage()
        cov.erase()
        cov.start()

    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?") from exc
    execute_from_command_line(sys.argv)

    if running_tests:
        cov.stop()
        cov.save()
        covered = cov.report()
        if covered < 100:
            sys.exit(0)
Esempio n. 16
0
def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django-graphql.settings')
    try:
        command = sys.argv[1]
    except IndexError:
        command = "help"

    running_tests = (command == 'test')
    if running_tests:
        from coverage import Coverage
        cov = Coverage()
        cov.erase()
        cov.start()

    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?") from exc
    execute_from_command_line(sys.argv)

    if running_tests:
        cov.stop()
        cov.save()
        covered = cov.report()
        if covered < 100:
            sys.exit(1)
Esempio n. 17
0
def coverage(packages,
             output_stream=sys.stdout,
             coverage_file=None,
             xml_enabled=None,
             xml_file=None):
    """
    Context manager that activates coverage on the specified packages.

    Modules in the packages that are already loaded are removed and nosetest will load
    them again with coverage instrumentation when they are needed.

    :param packages: list of packages
    :param output_stream: the stream to write the results to
    :param coverage_file: the coverage file. If not specified coverage will use the default .coverage file
    :param xml_enabled: enable XML coverage report
    :param xml_file: the XML coverage report file. If not specified coverage will use the defualt coverage.xml file
    :return: instance of coverage.Coverage
    """
    source = {}
    for module_name, module in [
        (module_name, module)
            for module_name, module in list(sys.modules.items())
            if in_any_package(module_name, packages)
    ]:
        source[module_name] = module
        del sys.modules[module_name]

    cover = Coverage(data_file=coverage_file, auto_data=False, branch=False)
    cover.combine()
    cover.erase()
    cover.start()
    yield cover

    for module_name, module in [
        (module_name, module)
            for module_name, module in list(sys.modules.items())
            if in_any_package(module_name, packages)
    ]:
        source[module_name] = module

    cover.stop()
    cover.combine()
    cover.save()
    cover.report(list(source.values()), file=output_stream, show_missing=True)

    if xml_enabled:
        cover.xml_report(list(source.values()), xml_file)
Esempio n. 18
0
def test_with_coverage():
    """ Runs the Unit Tests with Coverage. """
    # Configure Code Coverage Reports
    if app.config.get('FLASK_COVERAGE'):
        from coverage import Coverage
        # Start coverage engine, enable branch coverage analytics
        # Limit analysis to files inside application package.
        cov = Coverage(branch=True, include='myapp/*')
        cov.start()
        import unittest
        tests = unittest.TestLoader().discover('tests')
        unittest.TextTestRunner(verbosity=2).run(tests)
        cov.stop()
        cov.save()
        print('Coverage Summary')
        cov.report()
        cov.html_report(directory='covhtml')
        cov.erase()
Esempio n. 19
0
def run_unit_tests():
    from datetime import datetime
    from coverage import Coverage
    from io import StringIO
    import unittest
    from tests import testModels

    w = StringIO()
    cov = Coverage(omit=["/usr/*", "*/venv/*", "*-packages*"])
    cov.start()
    runner = unittest.TextTestRunner(stream=w)
    runner.run(unittest.makeSuite(testModels))
    cov.stop()
    cov.report(file=w)
    output = w.getvalue()

    db.app = app
    
    return ("You ran the tests on: " + datetime.now().strftime("%I:%M%p on %B %d, %Y") + " GMT\n" + output)
Esempio n. 20
0
    def run(self) -> None:
        from coverage import Coverage

        cov = Coverage()

        failed_message: Optional[str] = None
        cov.start()
        try:
            self.run_command('test')
        except DistutilsError as exc:
            failed_message = str(exc)
        finally:
            cov.save()

        cov.report()
        cov.html_report()

        if failed_message:
            raise DistutilsError(f'tests failed: {failed_message}')
Esempio n. 21
0
def main():
    """ The main function, mainly functioning to do the main functional work
        (thanks pylint)
    """
    if len(sys.argv) > 1 and sys.argv[1] == 'cover':
        # FIXME - there are enough args now to need an arg parser
        cover = Coverage(
            branch=True,
            auto_data=True,
            omit=[
                "/usr/share/pyshared/*",
                "/usr/lib/python3/dist-packages/*",
            ],
        )
        min_percent = 0

        if len(sys.argv) > 2:
            min_percent = float(sys.argv[2])
    else:
        cover = False

    loader = unittest.defaultTestLoader
    runner = unittest.TextTestRunner(verbosity=2)

    if cover:
        cover.erase()
        cover.start()

    tests = loader.discover('.')

    # If we ever drop libraries into the 'lib' subdir defined in the above
    # sys.path.insert then we will need to discover their tests and add
    # them separately with the following:
    tests_lib = loader.discover('lib', top_level_dir='lib')
    tests.addTests(tests_lib)

    result = runner.run(tests)

    if cover:
        cover.stop()
        # the debian coverage package didnt include jquery.debounce.min.js
        # (and additionally, that thing is not packaged for debian elsewhere)
        try:
            cover.html_report()
        except Exception:
            pass
        percent = cover.report(show_missing=True)

        if min_percent > percent:
            err_fmt = "The coverage ({:.1f}% reached) fails to reach the minimum required ({}%)\n"  # noqa
            sys.stderr.write(err_fmt.format(percent, min_percent))
            exit(1)

    if not result.wasSuccessful():
        exit(1)
Esempio n. 22
0
    def handle(self, *args, **kwargs):  # pragma: no cover
        cov = Coverage()
        cov.erase()
        cov.start()

        super().handle(*args, **kwargs)

        cov.stop()
        cov.save()
        covered = cov.report()
        if covered < 100:
            sys.exit(1)
Esempio n. 23
0
    def run_tests(self, *args, **kwargs):
        suite_result = super().run_tests(*args, **kwargs)

        cov = getattr(process_startup, "coverage")
        cov.stop()
        cov.save()

        print()
        print("Generating Coverage Report...")

        combined_cov = Coverage()
        combined_cov.load()
        combined_cov.combine()
        combined_cov.report()
        combined_cov.html_report()

        print()
        print("Linting files...")
        subprocess.call(["flake8"])

        return suite_result
Esempio n. 24
0
    def handle(self, *args, **kwargs):  # pragma: no cover
        self.stdout.write('Run test with coverage..')
        cov = Coverage(branch=True, )
        cov.erase()
        cov.start()

        super().handle(*args, **kwargs)

        cov.stop()
        cov.save()
        covered = cov.report(skip_covered=True)
        if covered < 90:
            sys.exit(1)
Esempio n. 25
0
def run_test_suite():

    cov = Coverage(config_file=True)
    cov.erase()
    cov.start()

    # Announce the test suite
    sys.stdout.write(colored(text="\nWelcome to the ", color="magenta", attrs=["bold"]))
    sys.stdout.write(colored(text="python-xirsys", color="green", attrs=["bold"]))
    sys.stdout.write(colored(text=" test suite.\n\n", color="magenta", attrs=["bold"]))

    # Announce test run
    print(colored(text="Step 1: Running unit tests.\n", color="yellow", attrs=["bold"]))

    test_suite = TestLoader().discover(str(Path("tests").absolute()))
    result = TextTestRunner(verbosity=1).run(test_suite)

    if not result.wasSuccessful():
        sys.exit(len(result.failures) + len(result.errors))

    # Announce coverage run
    print(colored(text="\nStep 2: Generating coverage results.\n", color="yellow", attrs=["bold"]))

    cov.stop()
    percentage = round(cov.report(show_missing=True), 2)
    cov.html_report(directory='cover')
    cov.save()

    if percentage < TESTS_THRESHOLD:
        print(colored(text="YOUR CHANGES HAVE CAUSED TEST COVERAGE TO DROP. " +
                           "WAS {old}%, IS NOW {new}%.\n".format(old=TESTS_THRESHOLD, new=percentage),
                           color="red", attrs=["bold"]))
        sys.exit(1)

    # Announce flake8 run
    sys.stdout.write(colored(text="\nStep 3: Checking for pep8 errors.\n\n", color="yellow", attrs=["bold"]))

    print("pep8 errors:")
    print("----------------------------------------------------------------------")

    from subprocess import call
    flake_result = call(["flake8", ".", "--count"])
    if flake_result != 0:
        print("pep8 errors detected.")
        print(colored(text="\nYOUR CHANGES HAVE INTRODUCED PEP8 ERRORS!\n", color="red", attrs=["bold"]))
        sys.exit(flake_result)
    else:
        print("None")

    # Announce success
    print(colored(text="\nTests completed successfully with no errors. Congrats!", color="green", attrs=["bold"]))
Esempio n. 26
0
def run_test_suite():

    cov = Coverage(config_file=True)
    cov.erase()
    cov.start()

    # Announce the test suite
    sys.stdout.write(colored(text="\nWelcome to the ", color="magenta", attrs=["bold"]))
    sys.stdout.write(colored(text="python-doc-inherit", color="green", attrs=["bold"]))
    sys.stdout.write(colored(text=" test suite.\n\n", color="magenta", attrs=["bold"]))

    # Announce test run
    print(colored(text="Step 1: Running unit tests.\n", color="yellow", attrs=["bold"]))

    test_suite = TestLoader().discover(str(Path("tests").absolute()))
    result = TextTestRunner(verbosity=1).run(test_suite)

    if not result.wasSuccessful():
        sys.exit(len(result.failures) + len(result.errors))

    # Announce coverage run
    print(colored(text="\nStep 2: Generating coverage results.\n", color="yellow", attrs=["bold"]))

    cov.stop()
    percentage = round(cov.report(show_missing=True), 2)
    cov.html_report(directory='cover')
    cov.save()

    if percentage < TESTS_THRESHOLD:
        print(colored(text="YOUR CHANGES HAVE CAUSED TEST COVERAGE TO DROP. " +
                           "WAS {old}%, IS NOW {new}%.\n".format(old=TESTS_THRESHOLD, new=percentage),
                           color="red", attrs=["bold"]))
        sys.exit(1)

    # Announce flake8 run
    sys.stdout.write(colored(text="\nStep 3: Checking for pep8 errors.\n\n", color="yellow", attrs=["bold"]))

    print("pep8 errors:")
    print("----------------------------------------------------------------------")

    from subprocess import call
    flake_result = call(["flake8", ".", "--count"])
    if flake_result != 0:
        print("pep8 errors detected.")
        print(colored(text="\nYOUR CHANGES HAVE INTRODUCED PEP8 ERRORS!\n", color="red", attrs=["bold"]))
        sys.exit(flake_result)
    else:
        print("None")

    # Announce success
    print(colored(text="\nTests completed successfully with no errors. Congrats!", color="green", attrs=["bold"]))
Esempio n. 27
0
def test(*tests,
         fail_fast=False,
         verbosity=1,
         with_coverage=True,
         with_lint=True):
    original_working_directory = os.getcwd()

    if tests:
        num_tests = len(tests)
        s = "" if num_tests == 1 else "s"
        printer.header(f"Running {num_tests} test{s}...")
    else:
        coverage_message = " with coverage" if with_coverage else ""
        printer.header(f"Running tests{coverage_message}...")

    runner = unittest.TextTestRunner(failfast=fail_fast, verbosity=verbosity)
    loader = unittest.TestLoader()

    if with_coverage:
        from coverage import Coverage

        coverage = Coverage(source=["src/runcommands"])
        coverage.start()

    if tests:
        runner.run(loader.loadTestsFromNames(tests))
    else:
        tests = loader.discover("./tests", top_level_dir=".")
        result = runner.run(tests)
        if not result.errors:
            if with_coverage:
                coverage.stop()
                coverage.report()
            if with_lint:
                # XXX: The test runner apparently changes CWD.
                os.chdir(original_working_directory)
                format_code(check=True)
                lint()
Esempio n. 28
0
def test_py3(module):
    futurize(module)
    fix_patterns(module)
    result, elapsed = run_destral(module)
    py3 = int(result == 0)
    cov = Coverage()
    try:
        cov.load()
        cov_per = cov.report()
        cov.erase()
    except Exception:
        cov_per = 0

    update_module(module, py3, coverage=cov_per, test_time=elapsed)
Esempio n. 29
0
class CodeCoverage(object):
    """
        Code Coverage radish extension
    """
    OPTIONS = [
        ("--with-coverage", "enable code coverage"),
        ("--cover-packages=<cover_packages>", "specify source code package")
    ]
    LOAD_IF = staticmethod(lambda config: config.with_coverage)
    LOAD_PRIORITY = 70

    def __init__(self):
        before.all(self.coverage_start)
        after.all(self.coverage_stop)

        if world.config.cover_packages:
            cover_packages = world.config.cover_packages.split(",")
        else:
            cover_packages = []
        self.coverage = Coverage(source=cover_packages)

    def coverage_start(self, features, marker):
        """
            Start the coverage measurement
        """
        self.coverage.load()
        self.coverage.start()

    def coverage_stop(self, features, marker):
        """
            Stop the coverage measurement
            and create report
        """
        self.coverage.stop()
        self.coverage.save()
        self.coverage.report(file=sys.stdout)
Esempio n. 30
0
def main():
    """ The main function, mainly functioning to do the main functional work
        (thanks pylint)
    """
    if len(sys.argv) > 1 and sys.argv[1] == 'cover':
        # FIXME - there are enough args now to need an arg parser
        cover = Coverage(branch=True, auto_data=True)
        min_percent = 0

        if len(sys.argv) > 2:
            min_percent = float(sys.argv[2])
    else:
        cover = False

    loader = unittest.defaultTestLoader
    runner = unittest.TextTestRunner(verbosity=2)

    if cover:
        cover.erase()
        cover.start()

    tests = loader.discover('.')
    tests_lib = loader.discover('lib', top_level_dir='lib')
    tests.addTests(tests_lib)
    result = runner.run(tests)

    if cover:
        cover.stop()
        # the debian coverage package didnt include jquery.debounce.min.js
        # (and additionally, that thing is not packaged for debian elsewhere)
        try:
            cover.html_report()
        except:  # noqa pylint: disable=bare-except
            pass
        # TODO - reconfirm that this packaging issue is still the case
        #        if it is not, remove the try..except
        #        if it is, add the specific exception

        percent = cover.report(show_missing=True)

        if min_percent > percent:
            print("The coverage ({:.1f}% reached) fails to reach the "
                  "minimum required ({}%)\n".format(percent, min_percent))
            exit(1)

    if not result.wasSuccessful():
        exit(1)
Esempio n. 31
0
def show_coverage(
    cov: Coverage,
    changed_files: Optional[List[str]] = None,
    show_missing: Optional[bool] = None,
    fail_under: Optional[int] = None,
) -> bool:
    """Print coverage for specified files"""
    try:
        coverage_value = cov.report(include=changed_files, show_missing=show_missing)
    except CoverageException as e:
        if 'No data to report.' in e.args:
            print(e)
            return True
        else:
            raise
    minimum_coverage = fail_under if fail_under is not None else cov.config.fail_under
    return coverage_value >= minimum_coverage
def main():
    """ The main function, mainly functioning to do the main functional work
        (thanks pylint)
    """
    if len(sys.argv) > 1 and sys.argv[1] == 'cover':
        # FIXME - there are enough args now to need an arg parser
        cover = Coverage(branch=True, auto_data=True)
        min_percent = 0

        if len(sys.argv) > 2:
            min_percent = float(sys.argv[2])
    else:
        cover = False

    loader = unittest.defaultTestLoader
    runner = unittest.TextTestRunner(verbosity=2)

    if cover:
        cover.erase()
        cover.start()

    tests = loader.discover('.')
    tests_lib = loader.discover('lib', top_level_dir='lib')
    tests.addTests(tests_lib)
    result = runner.run(tests)

    if cover:
        cover.stop()
        # the debian coverage package didnt include jquery.debounce.min.js
        # (and additionally, that thing is not packaged for debian elsewhere)
        try:
            cover.html_report()
        except:
            pass
        percent = cover.report(show_missing=True)

        if min_percent > percent:
            print("The coverage ({:.1f}% reached) fails to reach the "
                  "minimum required ({}%)\n".format(percent, min_percent))
            exit(1)

    if not result.wasSuccessful():
        exit(1)
Esempio n. 33
0
def coverage(ctx):
    from unittest import TestLoader, TextTestRunner
    from coverage import Coverage

    cov = Coverage()
    cov.start()

    suite = TestLoader().discover("./")
    TextTestRunner().run(suite)

    cov.stop()
    cov.save()

    covered = cov.report(show_missing=True)
    accepted_coverage = 90

    assert covered > accepted_coverage,\
        "Not enough coverage. Minimum {} percent, current: {:.2f}%".format(
            accepted_coverage, covered)
Esempio n. 34
0
def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
    # ******* CREDIT ***********
    # https://adamj.eu/tech/2019/04/30/getting-a-django-application-to-100-percent-coverage/
    # MyProject Customization: run coverage.py around tests automatically

    try:
        command = sys.argv[1]
    except IndexError:
        command = "help"

    running_tests = (command == 'test')
    if running_tests:
        from coverage import Coverage
        cov = Coverage()
        cov.erase()
        cov.start()

    # **************************
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?") from exc
    execute_from_command_line(sys.argv)

    # ****************************
    if running_tests:
        cov.stop()
        cov.save()
        # see credits in README.md for the next line
        cov.html_report(directory='covhtml')
        covered = cov.report()
        if covered < 100:
            sys.exit(1)
Esempio n. 35
0
    def run(self) -> int:
        coverage = Coverage()
        coverage.start()
        exit_code = main(['-v', 'tests'])
        coverage.stop()

        # Set score to zero if exit code was non-zero
        if exit_code != 0:
            return 0

        # Generate xml report in StringIO file
        coverage.get_data()
        coverage.config.from_args(
            ignore_errors=None,
            report_omit=None,
            report_include=None,
            xml_output=None,
        )
        data = StringIO()
        reporter = XmlReporter(coverage, coverage.config)
        reporter.report(None, data)
        data.seek(0)

        return round(coverage.report(show_missing=True))
Esempio n. 36
0
# usage:
#   run_tests.py [PYTEST_ARGS]

import os
import subprocess
import sys

from coverage import Coverage


SHELL = sys.platform == 'win32'


if __name__ == '__main__':
    command = ['py.test']
    if os.environ.get('WITH_COVERAGE') == '1':
        command.extend(['--cov=rinoh', '--cov-report='])
    if os.environ.get('BASETEMP'):
        command.append('--basetemp={}'.format(os.environ['BASETEMP']))
    command.extend(sys.argv[1:])
    rc = subprocess.call(command, shell=SHELL)
    if os.environ.get('WITH_COVERAGE') == '1':
        cov = Coverage()
        cov.load()
        cov.combine()
        cov.report(skip_covered=True)
        cov.xml_report()
    raise SystemExit(rc)
Esempio n. 37
0
def test(config, tests=(), fail_fast=False, verbosity=1, with_coverage=False, with_lint=False):
    from coverage import Coverage

    from django import setup
    from django.conf import settings
    from django.conf.urls import url
    from django.http import HttpResponse
    from django.test.runner import DiscoverRunner

    with_coverage = with_coverage and not tests
    with_lint = with_lint and not tests

    settings.configure(
        DEBUG=True,
        ALLOWED_HOSTS=['*'],
        DATABASES={
            'default': {
                'ENGINE': 'django.db.backends.sqlite3',
                'NAME': ':memory:',
            }
        },
        ROOT_URLCONF=(
            url(r'^test$', lambda request: HttpResponse('test'), name='test'),
        ),
        INSTALLED_APPS=(
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'django.contrib.sessions',
            'django.contrib.admin',
            'arcutils',
        ),
        MIDDLEWARE_CLASSES=[],
        LDAP={
            'default': {
                'host': 'ldap://ldap-login.oit.pdx.edu',
                'username': '',
                'password': '',
                'search_base': 'ou=people,dc=pdx,dc=edu',
            }
        },
        TEMPLATES=[{
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.contrib.auth.context_processors.auth',
                ]
            }
        }],
    )

    setup()
    runner = DiscoverRunner(failfast=fail_fast, verbosity=verbosity)

    if with_coverage:
        coverage = Coverage(source=['arcutils'])
        coverage.start()

    if tests:
        num_errors = runner.run_tests(tests)
    else:
        num_errors = runner.run_tests(['arcutils'])

    if num_errors:
        abort(code=num_errors, message='Test failure(s) encountered; aborting')

    if with_coverage:
        coverage.stop()
        coverage.report()

    if with_lint:
        lint(config)
Esempio n. 38
0
#!/usr/bin/env python

import os
import sys

from django.core import management


# Point to the correct settings for testing
os.environ['DJANGO_SETTINGS_MODULE'] = 'test_settings'


if __name__ == "__main__":
    testing = 'test' in sys.argv

    if testing:
        from coverage import Coverage
        cov = Coverage()
        cov.erase()
        cov.start()

    management.execute_from_command_line()

    if testing:
        cov.stop()
        cov.save()
        cov.report()
Esempio n. 39
0
class CoverageScript(object):
    """The command-line interface to coverage.py."""

    def __init__(self):
        self.global_option = False
        self.coverage = None

    def command_line(self, argv):
        """The bulk of the command line interface to coverage.py.

        `argv` is the argument list to process.

        Returns 0 if all is well, 1 if something went wrong.

        """
        # Collect the command-line options.
        if not argv:
            show_help(topic='minimum_help')
            return OK

        # The command syntax we parse depends on the first argument.  Global
        # switch syntax always starts with an option.
        self.global_option = argv[0].startswith('-')
        if self.global_option:
            parser = GlobalOptionParser()
        else:
            parser = CMDS.get(argv[0])
            if not parser:
                show_help("Unknown command: '%s'" % argv[0])
                return ERR
            argv = argv[1:]

        ok, options, args = parser.parse_args_ok(argv)
        if not ok:
            return ERR

        # Handle help and version.
        if self.do_help(options, args, parser):
            return OK

        # Listify the list options.
        source = unshell_list(options.source)
        omit = unshell_list(options.omit)
        include = unshell_list(options.include)
        debug = unshell_list(options.debug)

        # Do something.
        self.coverage = Coverage(
            data_suffix=options.parallel_mode,
            cover_pylib=options.pylib,
            timid=options.timid,
            branch=options.branch,
            config_file=options.rcfile,
            source=source,
            omit=omit,
            include=include,
            debug=debug,
            concurrency=options.concurrency,
            check_preimported=True,
            context=options.context,
            )

        if options.action == "debug":
            return self.do_debug(args)

        elif options.action == "erase":
            self.coverage.erase()
            return OK

        elif options.action == "run":
            return self.do_run(options, args)

        elif options.action == "combine":
            if options.append:
                self.coverage.load()
            data_dirs = args or None
            self.coverage.combine(data_dirs, strict=True)
            self.coverage.save()
            return OK

        # Remaining actions are reporting, with some common options.
        report_args = dict(
            morfs=unglob_args(args),
            ignore_errors=options.ignore_errors,
            omit=omit,
            include=include,
            )

        # We need to be able to import from the current directory, because
        # plugins may try to, for example, to read Django settings.
        sys.path.insert(0, '')

        self.coverage.load()

        total = None
        if options.action == "report":
            total = self.coverage.report(
                show_missing=options.show_missing,
                skip_covered=options.skip_covered,
                **report_args
                )
        elif options.action == "annotate":
            self.coverage.annotate(directory=options.directory, **report_args)
        elif options.action == "html":
            total = self.coverage.html_report(
                directory=options.directory,
                title=options.title,
                skip_covered=options.skip_covered,
                **report_args
                )
        elif options.action == "xml":
            outfile = options.outfile
            total = self.coverage.xml_report(outfile=outfile, **report_args)

        if total is not None:
            # Apply the command line fail-under options, and then use the config
            # value, so we can get fail_under from the config file.
            if options.fail_under is not None:
                self.coverage.set_option("report:fail_under", options.fail_under)

            fail_under = self.coverage.get_option("report:fail_under")
            precision = self.coverage.get_option("report:precision")
            if should_fail_under(total, fail_under, precision):
                return FAIL_UNDER

        return OK

    def do_help(self, options, args, parser):
        """Deal with help requests.

        Return True if it handled the request, False if not.

        """
        # Handle help.
        if options.help:
            if self.global_option:
                show_help(topic='help')
            else:
                show_help(parser=parser)
            return True

        if options.action == "help":
            if args:
                for a in args:
                    parser = CMDS.get(a)
                    if parser:
                        show_help(parser=parser)
                    else:
                        show_help(topic=a)
            else:
                show_help(topic='help')
            return True

        # Handle version.
        if options.version:
            show_help(topic='version')
            return True

        return False

    def do_run(self, options, args):
        """Implementation of 'coverage run'."""

        if not args:
            if options.module:
                # Specified -m with nothing else.
                show_help("No module specified for -m")
                return ERR
            command_line = self.coverage.get_option("run:command_line")
            if command_line is not None:
                args = shlex.split(command_line)
                if args and args[0] == "-m":
                    options.module = True
                    args = args[1:]
        if not args:
            show_help("Nothing to do.")
            return ERR

        if options.append and self.coverage.get_option("run:parallel"):
            show_help("Can't append to data files in parallel mode.")
            return ERR

        if options.concurrency == "multiprocessing":
            # Can't set other run-affecting command line options with
            # multiprocessing.
            for opt_name in ['branch', 'include', 'omit', 'pylib', 'source', 'timid']:
                # As it happens, all of these options have no default, meaning
                # they will be None if they have not been specified.
                if getattr(options, opt_name) is not None:
                    show_help(
                        "Options affecting multiprocessing must only be specified "
                        "in a configuration file.\n"
                        "Remove --{} from the command line.".format(opt_name)
                    )
                    return ERR

        runner = PyRunner(args, as_module=bool(options.module))
        runner.prepare()

        if options.append:
            self.coverage.load()

        # Run the script.
        self.coverage.start()
        code_ran = True
        try:
            runner.run()
        except NoSource:
            code_ran = False
            raise
        finally:
            self.coverage.stop()
            if code_ran:
                self.coverage.save()

        return OK

    def do_debug(self, args):
        """Implementation of 'coverage debug'."""

        if not args:
            show_help("What information would you like: config, data, sys?")
            return ERR

        for info in args:
            if info == 'sys':
                sys_info = self.coverage.sys_info()
                print(info_header("sys"))
                for line in info_formatter(sys_info):
                    print(" %s" % line)
            elif info == 'data':
                self.coverage.load()
                data = self.coverage.get_data()
                print(info_header("data"))
                print("path: %s" % self.coverage.get_data().data_filename())
                if data:
                    print("has_arcs: %r" % data.has_arcs())
                    summary = line_counts(data, fullpath=True)
                    filenames = sorted(summary.keys())
                    print("\n%d files:" % len(filenames))
                    for f in filenames:
                        line = "%s: %d lines" % (f, summary[f])
                        plugin = data.file_tracer(f)
                        if plugin:
                            line += " [%s]" % plugin
                        print(line)
                else:
                    print("No data collected")
            elif info == 'config':
                print(info_header("config"))
                config_info = self.coverage.config.__dict__.items()
                for line in info_formatter(config_info):
                    print(" %s" % line)
            else:
                show_help("Don't know what you mean by %r" % info)
                return ERR

        return OK
            # If the module defines a suite() function, call it to get the suite.
            mod = __import__(t, globals(), locals(), ['suite'])
            suitefn = getattr(mod, 'suite')
            suite.addTest(suitefn())
        except (ImportError, AttributeError):
            # else, just load all the test cases from the module.
            suite.addTest(unittest.defaultTestLoader.loadTestsFromName(t))
    runner = unittest.TextTestRunner()
    cov.start()
    result = runner.run(suite)
    cov.stop()

    if not result.wasSuccessful():
        exit(3)

    percent = round(cov.report(), 4)
    cov.html_report()
    if os.path.isfile('min_cov.txt'):
        file = open('min_cov.txt', 'r')
        min_coverage = float(file.read())
        file.close()
        if percent < min_coverage:
            exit(2)
        else:
            file = open('min_cov.txt', 'w')
            file.write(str(percent))
            file.close()
    else:
        file = open('min_cov.txt', 'w')
        file.write(str(percent))
        file.close()
Esempio n. 41
0
def run_test_suite(args):
    enable_coverage = not args.no_coverage
    tests = args.tests

    if enable_coverage:
        cov = Coverage(config_file=True)
        cov.erase()
        cov.start()

    settings.configure(
        DEBUG=True,
        USE_TZ=True,
        TIME_ZONE="UTC",
        SITE_ID=1,
        DATABASES={
            "default": {
                "ENGINE": "django.db.backends.postgresql_psycopg2",
                "NAME": "djstripe",
                "USER": "******",
                "PASSWORD": "",
                "HOST": "localhost",
                "PORT": "",
            },
        },
        TEMPLATES=[
            {
                'BACKEND': 'django.template.backends.django.DjangoTemplates',
                'DIRS': [],
                'APP_DIRS': True,
                'OPTIONS': {
                    'context_processors': [
                        'django.contrib.auth.context_processors.auth',
                    ],
                },
            },
        ],
        ROOT_URLCONF="tests.test_urls",
        INSTALLED_APPS=[
            "django.contrib.admin",
            "django.contrib.auth",
            "django.contrib.contenttypes",
            "django.contrib.sessions",
            "django.contrib.sites",
            "jsonfield",
            "djstripe",
            "tests",
            "tests.apps.testapp"
        ],
        MIDDLEWARE=(
            "django.contrib.sessions.middleware.SessionMiddleware",
            "django.contrib.auth.middleware.AuthenticationMiddleware",
            "django.contrib.messages.middleware.MessageMiddleware"
        ),
        STRIPE_PUBLIC_KEY=os.environ.get("STRIPE_PUBLIC_KEY", ""),
        STRIPE_SECRET_KEY=os.environ.get("STRIPE_SECRET_KEY", ""),
        DJSTRIPE_PLANS={
            "test0": {
                "stripe_plan_id": "test_id_0",
                "name": "Test Plan 0",
                "description": "A test plan",
                "price": 1000,  # $10.00
                "currency": "usd",
                "interval": "month"
            },
            "test": {
                "stripe_plan_id": "test_id",
                "name": "Test Plan 1",
                "description": "Another test plan",
                "price": 2500,  # $25.00
                "currency": "usd",
                "interval": "month"
            },
            "test2": {
                "stripe_plan_id": "test_id_2",
                "name": "Test Plan 2",
                "description": "Yet Another test plan",
                "price": 5000,  # $50.00
                "currency": "usd",
                "interval": "month"
            },
            "test_deletion": {
                "stripe_plan_id": "test_id_3",
                "name": "Test Plan 3",
                "description": "Test plan for deletion.",
                "price": 5000,  # $50.00
                "currency": "usd",
                "interval": "month"
            },
            "test_trial": {
                "stripe_plan_id": "test_id_4",
                "name": "Test Plan 4",
                "description": "Test plan for trails.",
                "price": 7000,  # $70.00
                "currency": "usd",
                "interval": "month",
                "trial_period_days": 7
            },
            "unidentified_test_plan": {
                "name": "Unidentified Test Plan",
                "description": "A test plan with no ID.",
                "price": 2500,  # $25.00
                "currency": "usd",
                "interval": "month"
            }
        },
        DJSTRIPE_SUBSCRIPTION_REQUIRED_EXCEPTION_URLS=(
            "(admin)",
            "test_url_name",
            "testapp_namespaced:test_url_namespaced",
            "fn:/test_fnmatch*"
        ),
        DJSTRIPE_USE_NATIVE_JSONFIELD=os.environ.get("USE_NATIVE_JSONFIELD", "") == "1",
    )

    # Avoid AppRegistryNotReady exception
    # http://stackoverflow.com/questions/24793351/django-appregistrynotready
    if hasattr(django, "setup"):
        django.setup()

    # Announce the test suite
    sys.stdout.write(colored(text="\nWelcome to the ", color="magenta", attrs=["bold"]))
    sys.stdout.write(colored(text="dj-stripe", color="green", attrs=["bold"]))
    sys.stdout.write(colored(text=" test suite.\n\n", color="magenta", attrs=["bold"]))

    # Announce test run
    sys.stdout.write(colored(text="Step 1: Running unit tests.\n\n", color="yellow", attrs=["bold"]))

    # Hack to reset the global argv before nose has a chance to grab it
    # http://stackoverflow.com/a/1718407/1834570
    args = sys.argv[1:]
    sys.argv = sys.argv[0:1]

    from django_nose import NoseTestSuiteRunner

    test_runner = NoseTestSuiteRunner(verbosity=1, keepdb=True, failfast=True)
    failures = test_runner.run_tests(tests)

    if failures:
        sys.exit(failures)

    if enable_coverage:
        # Announce coverage run
        sys.stdout.write(colored(text="\nStep 2: Generating coverage results.\n\n", color="yellow", attrs=["bold"]))

        cov.stop()
        percentage = round(cov.report(show_missing=True), 2)
        cov.html_report(directory='cover')
        cov.save()

        if percentage < TESTS_THRESHOLD:
            sys.stderr.write(colored(text="YOUR CHANGES HAVE CAUSED TEST COVERAGE TO DROP. " +
                                     "WAS {old}%, IS NOW {new}%.\n\n".format(old=TESTS_THRESHOLD, new=percentage),
                                     color="red", attrs=["bold"]))
            sys.exit(1)
    else:
        # Announce disabled coverage run
        sys.stdout.write(colored(text="\nStep 2: Generating coverage results [SKIPPED].",
                                 color="yellow", attrs=["bold"]))

    # Announce success
    if enable_coverage:
        sys.stdout.write(colored(text="\nTests completed successfully with no errors. Congrats!\n",
                                 color="green", attrs=["bold"]))
    else:
        sys.stdout.write(colored(text="\nTests completed successfully, but some step(s) were skipped!\n",
                                 color="green", attrs=["bold"]))
        sys.stdout.write(colored(text="Don't push without running the skipped step(s).\n",
                                 color="red", attrs=["bold"]))
Esempio n. 42
0
def run_test_suite(args):
    skip_utc = args.skip_utc
    enable_coverage = not args.no_coverage
    enable_pep8 = not args.no_pep8

    if enable_coverage:
        cov = Coverage(config_file=True)
        cov.erase()
        cov.start()

    settings.configure(
        DJSTRIPE_TESTS_SKIP_UTC=skip_utc,
        TIME_ZONE='America/Los_Angeles',
        DEBUG=True,
        USE_TZ=True,
        DATABASES={
            "default": {
                "ENGINE": "django.db.backends.postgresql_psycopg2",
                "NAME": "djstripe",
                "USER": "",
                "PASSWORD": "",
                "HOST": "",
                "PORT": "",
            },
        },
        ROOT_URLCONF="tests.test_urls",
        INSTALLED_APPS=[
            "django.contrib.admin",
            "django.contrib.auth",
            "django.contrib.contenttypes",
            "django.contrib.sessions",
            "django.contrib.sites",
            "jsonfield",
            "djstripe",
            "tests",
            "tests.apps.testapp"
        ],
        MIDDLEWARE_CLASSES=(
            "django.contrib.sessions.middleware.SessionMiddleware",
            "django.contrib.auth.middleware.AuthenticationMiddleware",
            "django.contrib.messages.middleware.MessageMiddleware"
        ),
        SITE_ID=1,
        STRIPE_PUBLIC_KEY=os.environ.get("STRIPE_PUBLIC_KEY", ""),
        STRIPE_SECRET_KEY=os.environ.get("STRIPE_SECRET_KEY", ""),
        DJSTRIPE_PLANS={
            "test0": {
                "stripe_plan_id": "test_id_0",
                "name": "Test Plan 0",
                "description": "A test plan",
                "price": 1000,  # $10.00
                "currency": "usd",
                "interval": "month"
            },
            "test": {
                "stripe_plan_id": "test_id",
                "name": "Test Plan 1",
                "description": "Another test plan",
                "price": 2500,  # $25.00
                "currency": "usd",
                "interval": "month"
            },
            "test2": {
                "stripe_plan_id": "test_id_2",
                "name": "Test Plan 2",
                "description": "Yet Another test plan",
                "price": 5000,  # $50.00
                "currency": "usd",
                "interval": "month"
            },
            "test_deletion": {
                "stripe_plan_id": "test_id_3",
                "name": "Test Plan 3",
                "description": "Test plan for deletion.",
                "price": 5000,  # $50.00
                "currency": "usd",
                "interval": "month"
            },
            "test_trial": {
                "stripe_plan_id": "test_id_4",
                "name": "Test Plan 4",
                "description": "Test plan for trails.",
                "price": 7000,  # $70.00
                "currency": "usd",
                "interval": "month",
                "trial_period_days": 7
            },
            "unidentified_test_plan": {
                "name": "Unidentified Test Plan",
                "description": "A test plan with no ID.",
                "price": 2500,  # $25.00
                "currency": "usd",
                "interval": "month"
            }
        },
        DJSTRIPE_PLAN_HIERARCHY = {
            "bronze": {
                "level": 1,
                "plans": [
                    "test0",
                    "test",
                ]
            },
            "silver": {
                "level": 2,
                "plans": [
                    "test2",
                    "test_deletion",
                ]
            },
            "gold": {
                "level": 3,
                "plans": [
                    "test_trial",
                    "unidentified_test_plan",
                ]
            },
        },
        DJSTRIPE_SUBSCRIPTION_REQUIRED_EXCEPTION_URLS=(
            "(admin)",
            "test_url_name",
            "testapp_namespaced:test_url_namespaced",
        ),
    )

    # Avoid AppRegistryNotReady exception
    # http://stackoverflow.com/questions/24793351/django-appregistrynotready
    if hasattr(django, "setup"):
        django.setup()

    # Announce the test suite
    sys.stdout.write(colored(text="\nWelcome to the ", color="magenta", attrs=["bold"]))
    sys.stdout.write(colored(text="dj-stripe", color="green", attrs=["bold"]))
    sys.stdout.write(colored(text=" test suite.\n\n", color="magenta", attrs=["bold"]))

    # Announce test run
    sys.stdout.write(colored(text="Step 1: Running unit tests.\n\n", color="yellow", attrs=["bold"]))

    # Hack to reset the global argv before nose has a chance to grab it
    # http://stackoverflow.com/a/1718407/1834570
    args = sys.argv[1:]
    sys.argv = sys.argv[0:1]

    from django_nose import NoseTestSuiteRunner

    test_runner = NoseTestSuiteRunner(verbosity=1)
    failures = test_runner.run_tests(["."])

    if failures:
        sys.exit(failures)

    if enable_coverage:
        # Announce coverage run
        sys.stdout.write(colored(text="\nStep 2: Generating coverage results.\n\n", color="yellow", attrs=["bold"]))

        cov.stop()
        percentage = round(cov.report(show_missing=True), 2)
        cov.html_report(directory='cover')
        cov.save()

        if percentage < TESTS_THRESHOLD:
            sys.stderr.write(colored(text="YOUR CHANGES HAVE CAUSED TEST COVERAGE TO DROP. " +
                                     "WAS {old}%, IS NOW {new}%.\n\n".format(old=TESTS_THRESHOLD, new=percentage),
                                     color="red", attrs=["bold"]))
            sys.exit(1)
    else:
        # Announce disabled coverage run
        sys.stdout.write(colored(text="\nStep 2: Generating coverage results [SKIPPED].", color="yellow", attrs=["bold"]))

    if enable_pep8:
        # Announce flake8 run
        sys.stdout.write(colored(text="\nStep 3: Checking for pep8 errors.\n\n", color="yellow", attrs=["bold"]))

        print("pep8 errors:")
        print("----------------------------------------------------------------------")

        from subprocess import call
        flake_result = call(["flake8", ".", "--count"])
        if flake_result != 0:
            sys.stderr.write("pep8 errors detected.\n")
            sys.stderr.write(colored(text="\nYOUR CHANGES HAVE INTRODUCED PEP8 ERRORS!\n\n", color="red", attrs=["bold"]))
            sys.exit(flake_result)
        else:
            print("None")
    else:
        # Announce disabled coverage run
        sys.stdout.write(colored(text="\nStep 3: Checking for pep8 errors [SKIPPED].\n", color="yellow", attrs=["bold"]))

    # Announce success
    if enable_coverage and enable_pep8:
        sys.stdout.write(colored(text="\nTests completed successfully with no errors. Congrats!\n", color="green", attrs=["bold"]))
    else:
        sys.stdout.write(colored(text="\nTests completed successfully, but some step(s) were skipped!\n", color="green", attrs=["bold"]))
        sys.stdout.write(colored(text="Don't push without running the skipped step(s).\n", color="red", attrs=["bold"]))