Exemplo n.º 1
0
 def report(self, stream):
     buf = StringIO()
     Coverage.report(self, buf)
     buf.seek(0)
     fileobj = open(self.coverage_summary, 'w')
     try:
         filter_coverage(buf, fileobj)
     finally:
         fileobj.close()
     buf.seek(0)
     stream.writelines(buf)
Exemplo n.º 2
0
 def options(self, parser, env=os.environ):
     Coverage.options(self, parser, env)
     Plugin.add_options(self, parser, env)
     parser.add_option(
         '--coverage-summary', action='store', dest='coverage_summary',
         metavar='FILE',
         help='write XML coverage results to FILE. Default: %default',
         default=os.path.join(os.getcwd(), 'build', 'coverage-results.txt'),
     )
     parser.add_option(
         '--cover-packages', action='store', dest='cover_packages',
         default=env.get('NOSE_COVER_PACKAGE'),
     )
class TestCoverageMinPercentageSinglePackagePlugin(PluginTester,
                                                   unittest.TestCase):
    activate = "--with-coverage"
    args = [
        '-v', '--cover-package=blah', '--cover-html', '--cover-min-percentage',
        '100'
    ]
    plugins = [Coverage()]
    suitepath = os.path.join(support, 'coverage')

    def setUp(self):
        if not hasCoverage:
            raise unittest.SkipTest('coverage not available; skipping')

        self.cover_file = os.path.join(os.getcwd(), '.coverage')
        self.cover_html_dir = os.path.join(os.getcwd(), 'cover')
        if os.path.exists(self.cover_file):
            os.unlink(self.cover_file)
        if os.path.exists(self.cover_html_dir):
            shutil.rmtree(self.cover_html_dir)
        self.assertRaises(
            SystemExit,
            super(TestCoverageMinPercentageSinglePackagePlugin, self).setUp)

    def runTest(self):
        pass
class TestCoveragePlugin(PluginTester, unittest.TestCase):
    activate = "--with-coverage"
    args = [
        '-v', '--cover-package=blah', '--cover-html', '--cover-min-percentage',
        '25'
    ]
    plugins = [Coverage()]
    suitepath = os.path.join(support, 'coverage')

    def setUp(self):
        if not hasCoverage:
            raise unittest.SkipTest('coverage not available; skipping')

        self.cover_file = os.path.join(os.getcwd(), '.coverage')
        self.cover_html_dir = os.path.join(os.getcwd(), 'cover')
        if os.path.exists(self.cover_file):
            os.unlink(self.cover_file)
        if os.path.exists(self.cover_html_dir):
            shutil.rmtree(self.cover_html_dir)
        super(TestCoveragePlugin, self).setUp()

    def runTest(self):
        self.assertTrue("blah        4      3    25%   1" in self.output)
        self.assertTrue("Ran 1 test in" in self.output)
        # Assert coverage html report exists
        self.assertTrue(
            os.path.exists(os.path.join(self.cover_html_dir, 'index.html')))
        # Assert coverage data is saved
        self.assertTrue(os.path.exists(self.cover_file))
Exemplo n.º 5
0
    def test_cover_packages_option(self):
        parser = OptionParser()
        c = Coverage()
        c.addOptions(parser)
        options, args = parser.parse_args(['test_can_be_disabled',
                                           '--cover-package=pkg1,pkg2,pkg3'])
        c.configure(options, Config())
        eq_(['pkg1', 'pkg2', 'pkg3'], c.coverPackages)

        env = {'NOSE_COVER_PACKAGE': 'pkg1,pkg2,pkg3'}
        c = Coverage()
        parser = OptionParser()
        c.addOptions(parser, env)
        options, args = parser.parse_args(['test_can_be_disabled'])
        c.configure(options, Config())
        eq_(['pkg1', 'pkg2', 'pkg3'], c.coverPackages)
Exemplo n.º 6
0
def run_test():
    nose.main(addplugins=[
        AutotestClientTestRunner(),
        AttributeSelector(),
        Xunit(),
        Coverage()
    ])
Exemplo n.º 7
0
def _test_options_helper(arg_option,
                         cover_option,
                         expected_set,
                         expected_not_set,
                         arg_value=None,
                         env_key=None):
    prefix_args = ['test_can_be_disabled', '--with-coverage']

    # Assert that the default works as expected
    parser = OptionParser()
    c_arg_not_set = Coverage()
    c_arg_not_set.addOptions(parser)
    options, _ = parser.parse_args(prefix_args)
    c_arg_not_set.configure(options, Config())
    eq_(expected_not_set, getattr(c_arg_not_set, cover_option))

    # Assert that the argument parser picks up the expected value
    parser = OptionParser()
    c_arg_set = Coverage()
    c_arg_set.addOptions(parser)

    args = arg_option
    if arg_value is not None:
        args += '=' + arg_value

    options, _ = parser.parse_args(prefix_args + [args])
    c_arg_set.configure(options, Config())
    eq_(expected_set, getattr(c_arg_set, cover_option))

    # If the option supports environment variables, check that too
    if env_key is not None:
        args = 'true'
        if arg_value is not None:
            args = arg_value

        env = {env_key: args}
        c = Coverage()
        parser = OptionParser()
        c.addOptions(parser, env)
        options, _ = parser.parse_args(prefix_args)
        c.configure(options, Config())
        eq_(expected_set, getattr(c, cover_option))

    return c_arg_not_set, c_arg_set
Exemplo n.º 8
0
    def test_cover_package_suppress_stdout(self):
        """regression test to check that nose can run coverage with and 
        without suppression of stdout reporting"""
        parser = OptionParser()
        c = Coverage()
        c.addOptions(parser)
        options, args = parser.parse_args(['test_can_be_disabled',
                                           '--cover-package=pkg1,pkg2,pkg3'])
        c.configure(options, Config())
        eq_(['pkg1', 'pkg2', 'pkg3'], c.coverPackages)

        env = {'NOSE_COVER_PACKAGE': 'pkg1,pkg2,pkg3'}
        c = Coverage()
        parser = OptionParser()
        c.addOptions(parser, env)
        options, args = parser.parse_args(['test_can_be_disabled'])
        c.configure(options, Config())
        eq_(['pkg1', 'pkg2', 'pkg3'], c.coverPackages)

        env = {'NOSE_COVER_PACKAGE' : 'pkg1,pkg2,pkg3',
                'NOSE_COVER_SUPPRESS_STDOUT': True}
        c = Coverage()
        parser = OptionParser()
        c.addOptions(parser, env)
        options, args = parser.parse_args(['test_can_be_disabled'])
        c.configure(options, Config())
        eq_(['pkg1', 'pkg2', 'pkg3'], c.coverPackages)
Exemplo n.º 9
0
def test(nose_arguments):
    """Run nosetests with the given arguments and report coverage"""
    assert nose_arguments[0] == ''
    import nose
    from nose.plugins.cover import Coverage
    nose.main(argv=nose_arguments, addplugins=[Coverage()])
Exemplo n.º 10
0
 def begin(self):
     if not os.path.exists(os.path.dirname(self.coverage_summary)):
         os.makedirs(os.path.dirname(self.coverage_summary))
     Coverage.begin(self)
Exemplo n.º 11
0
 def configure(self, options, config):
     Coverage.configure(self, options, config)
     self.coverage_summary = options.coverage_summary
Exemplo n.º 12
0
def _test_options_helper(arg_option, cover_option,
                         expected_set, expected_not_set,
                         arg_value=None, env_key=None):
    prefix_args = ['test_can_be_disabled', '--with-coverage']

    # Assert that the default works as expected
    parser = OptionParser()
    c_arg_not_set = Coverage()
    c_arg_not_set.addOptions(parser)
    options, _ = parser.parse_args(prefix_args)
    c_arg_not_set.configure(options, Config())
    eq_(expected_not_set, getattr(c_arg_not_set, cover_option))

    # Assert that the argument parser picks up the expected value
    parser = OptionParser()
    c_arg_set = Coverage()
    c_arg_set.addOptions(parser)

    args = arg_option
    if arg_value is not None:
        args += '=' + arg_value

    options, _ = parser.parse_args(prefix_args + [args])
    c_arg_set.configure(options, Config())
    eq_(expected_set, getattr(c_arg_set, cover_option))

    # If the option supports environment variables, check that too
    if env_key is not None:
        args = 'true'
        if arg_value is not None:
            args = arg_value

        env = {env_key: args}
        c = Coverage()
        parser = OptionParser()
        c.addOptions(parser, env)
        options, _ = parser.parse_args(prefix_args)
        c.configure(options, Config())
        eq_(expected_set, getattr(c, cover_option))

    return c_arg_not_set, c_arg_set
Exemplo n.º 13
0
    def test_cover_packages_option(self):
        parser = OptionParser()
        c = Coverage()
        c.addOptions(parser)
        options, args = parser.parse_args(
            ['test_can_be_disabled', '--cover-package=pkg1,pkg2,pkg3'])
        c.configure(options, Config())
        eq_(['pkg1', 'pkg2', 'pkg3'], c.coverPackages)

        env = {'NOSE_COVER_PACKAGE': 'pkg1,pkg2,pkg3'}
        c = Coverage()
        parser = OptionParser()
        c.addOptions(parser, env)
        options, args = parser.parse_args(['test_can_be_disabled'])
        c.configure(options, Config())
        eq_(['pkg1', 'pkg2', 'pkg3'], c.coverPackages)