Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds a --coverage option to "setup.py test" to generate a test covera…

…ge report.

Uses the coveragerc that's now included in the source (based on an e-mail from @iguananaut).
  • Loading branch information...
commit 6f2e7fbbcc635bdbd8cbf841fd5db02df2c08352 1 parent 9c65219
@mdboom mdboom authored
View
1  .gitignore
@@ -38,6 +38,7 @@ distribute-*.tar.gz
*~
.coverage
cover
+htmlcov
# Mac OSX
.DS_Store
View
2  MANIFEST.in
@@ -2,6 +2,7 @@ include README.rst
include distribute_setup.py
include setup.cfg
+include coveragerc
recursive-include astropy *.pyx *.c *.h *.map
recursive-include docs *
@@ -13,4 +14,3 @@ recursive-include astropy/wcs/src/wcslib *
exclude *.pyc *.o
prune docs/_build
prune build
-
View
9 astropy/__init__.py
@@ -37,7 +37,7 @@ def _get_test_runner():
def test(package=None, test_path=None, args=None, plugins=None,
verbose=False, pastebin=None, remote_data=False, pep8=False,
- pdb=False):
+ pdb=False, coverage=False):
"""
Run Astropy tests using py.test. A proper set of arguments is
constructed and passed to `pytest.main`.
@@ -83,6 +83,10 @@ def test(package=None, test_path=None, args=None, plugins=None,
Turn on PDB post-mortem analysis for failing tests. Same as
specifying `--pdb` in `args`.
+ coverage : bool, optional
+ Generate a test coverage report. The result will be placed in
+ the directory htmlcov.
+
See Also
--------
pytest.main : py.test function wrapped by `run_tests`.
@@ -92,4 +96,5 @@ def test(package=None, test_path=None, args=None, plugins=None,
return test_runner.run_tests(
package=package, test_path=test_path, args=args,
plugins=plugins, verbose=verbose, pastebin=pastebin,
- remote_data=remote_data, pep8=pep8, pdb=pdb)
+ remote_data=remote_data, pep8=pep8, pdb=pdb,
+ coverage=coverage)
View
29 astropy/tests/helper.py
@@ -11,6 +11,7 @@
import functools
import os
import subprocess
+import shutil
from distutils.core import Command
@@ -72,7 +73,7 @@ def __init__(self, base_path):
def run_tests(self, package=None, test_path=None, args=None, plugins=None,
verbose=False, pastebin=None, remote_data=False, pep8=False,
- pdb=False):
+ pdb=False, coverage=False):
"""
The docstring for this method lives in astropy/__init__.py:test
"""
@@ -123,10 +124,25 @@ def run_tests(self, package=None, test_path=None, args=None, plugins=None,
if pdb:
all_args += ' --pdb'
+ if coverage:
+ try:
+ import pytest_cov
+ except ImportError:
+ raise ImportError('Coverage reporting requires pytest-cov plugin: '
+ 'http://pypi.python.org/pypi/pytest-cov')
+ else:
+ all_args += ' --cov-report html --cov astropy --cov-config coveragerc'
+
all_args = shlex.split(all_args,
posix=not sys.platform.startswith('win'))
- return pytest.main(args=all_args, plugins=plugins)
+ result = pytest.main(args=all_args, plugins=plugins)
+
+ if coverage:
+ # Copy the htmlcov from build/lib.../htmlcov to a more obvious place
+ if os.path.exists('../../htmlcov'):
+ shutil.rmtree('../../htmlcov')
+ shutil.copytree('htmlcov', '../../htmlcov')
run_tests.__doc__ = test.__doc__
@@ -152,7 +168,9 @@ class astropy_test(Command, object):
'Same as specifying `--pep8 -k pep8` in `args`. Requires the '
'pytest-pep8 plugin.'),
('pdb', 'd', 'Turn on PDB post-mortem analysis for failing tests. '
- 'Same as specifying `--pdb` in `args`.')
+ 'Same as specifying `--pdb` in `args`.'),
+ ('coverage', 'c', 'Create a coverage report. Requires the pytest-cov '
+ 'plugin is installed')
]
package_name = None
@@ -167,6 +185,7 @@ def initialize_options(self):
self.remote_data = False
self.pep8 = False
self.pdb = False
+ self.coverage = False
def finalize_options(self):
# Normally we would validate the options here, but that's handled in
@@ -183,11 +202,11 @@ def run(self):
# modules may have appeared, and this is the easiest way to set up a
# new environment
cmd = ('import {0}, sys; sys.exit({0}.test({1!r}, {2!r}, ' +
- '{3!r}, {4!r}, {5!r}, {6!r}, {7!r}, {8!r}, {9!r}))')
+ '{3!r}, {4!r}, {5!r}, {6!r}, {7!r}, {8!r}, {9!r}, {10!r}))')
cmd = cmd.format(self.package_name,
self.package, self.test_path, self.args,
self.plugins, self.verbose_results, self.pastebin,
- self.remote_data, self.pep8, self.pdb)
+ self.remote_data, self.pep8, self.pdb, self.coverage)
raise SystemExit(subprocess.call([sys.executable, '-c', cmd],
cwd=new_path, close_fds=False))
View
6 coveragerc
@@ -0,0 +1,6 @@
+[run]
+source = astropy
+omit =
+ astropy/extern/*
+ astropy/sphinx/*
+ astropy/*tests/*
Please sign in to comment.
Something went wrong with that request. Please try again.