The runcoverage.py script now works correctly with django-discover-runner. Also refactored code structure for clarity.

Added an option to report the code coverage as an xml file.
This commit is contained in:
Omer Katz 2013-01-25 11:30:40 +03:00
parent bbd0ef84b4
commit 2b8cba55a8
2 changed files with 49 additions and 30 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@
*.db *.db
*~ *~
.* .*
coverage.xml
html/ html/
coverage/ coverage/

View File

@ -18,37 +18,17 @@ try:
except ImportError: except ImportError:
print("Coverage is not installed. Aborting...") print("Coverage is not installed. Aborting...")
def main(): def report(cov, cov_files):
"""Run the tests for rest_framework and generate a coverage report.""" cov.report(cov_files)
cov = coverage() if '--html' in sys.argv:
cov.erase() cov.html_report(cov_files, directory='coverage')
cov.start()
from django.conf import settings if '--xml' in sys.argv:
from django.test.utils import get_runner cov.xml_report(cov_files, outfile='../../coverage.xml')
TestRunner = get_runner(settings)
if hasattr(TestRunner, 'func_name'): def prepare_report(project_dir):
# Pre 1.2 test runners were just functions,
# and did not support the 'failfast' option.
import warnings
warnings.warn(
'Function-based test runners are deprecated. Test runners should be classes with a run_tests() method.',
DeprecationWarning
)
failures = TestRunner(['tests'])
else:
test_runner = TestRunner()
failures = test_runner.run_tests(['tests'])
cov.stop()
# Discover the list of all modules that we should test coverage for
import rest_framework
project_dir = os.path.dirname(rest_framework.__file__)
cov_files = [] cov_files = []
for (path, dirs, files) in os.walk(project_dir): for (path, dirs, files) in os.walk(project_dir):
@ -70,9 +50,47 @@ def main():
cov_files.extend([os.path.join(path, file) for file in files if file.endswith('.py')]) cov_files.extend([os.path.join(path, file) for file in files if file.endswith('.py')])
cov.report(cov_files) return cov_files
if '--html' in sys.argv:
cov.html_report(cov_files, directory='coverage')
def run_tests(app):
from django.conf import settings
from django.test.utils import get_runner
TestRunner = get_runner(settings)
if hasattr(TestRunner, 'func_name'):
# Pre 1.2 test runners were just functions,
# and did not support the 'failfast' option.
import warnings
warnings.warn(
'Function-based test runners are deprecated. Test runners should be classes with a run_tests() method.',
DeprecationWarning
)
failures = TestRunner([app])
else:
test_runner = TestRunner()
failures = test_runner.run_tests([app])
return failures
def main():
"""Run the tests for rest_framework and generate a coverage report."""
cov = coverage()
cov.erase()
cov.start()
failures = run_tests('rest_framework')
cov.stop()
# Discover the list of all modules that we should test coverage for
import rest_framework
project_dir = os.path.dirname(rest_framework.__file__)
cov_files = prepare_report(project_dir)
report(cov, cov_files)
sys.exit(failures) sys.exit(failures)
if __name__ == '__main__': if __name__ == '__main__':