diff --git a/.gitignore b/.gitignore index 2255cd9aa..aa4c87066 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.db *~ .* +coverage.xml html/ coverage/ diff --git a/rest_framework/runtests/runcoverage.py b/rest_framework/runtests/runcoverage.py index 2d4a2f781..0f25fbaee 100755 --- a/rest_framework/runtests/runcoverage.py +++ b/rest_framework/runtests/runcoverage.py @@ -18,37 +18,17 @@ try: except ImportError: print("Coverage is not installed. Aborting...") -def main(): - """Run the tests for rest_framework and generate a coverage report.""" +def report(cov, cov_files): + cov.report(cov_files) - cov = coverage() - cov.erase() - cov.start() + if '--html' in sys.argv: + cov.html_report(cov_files, directory='coverage') - from django.conf import settings - from django.test.utils import get_runner + if '--xml' in sys.argv: + cov.xml_report(cov_files, outfile='../../coverage.xml') - 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(['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__) +def prepare_report(project_dir): cov_files = [] 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.report(cov_files) - if '--html' in sys.argv: - cov.html_report(cov_files, directory='coverage') + return cov_files + + +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) if __name__ == '__main__':