mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 00:19:53 +03:00
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:
parent
bbd0ef84b4
commit
2b8cba55a8
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,6 +2,7 @@
|
|||
*.db
|
||||
*~
|
||||
.*
|
||||
coverage.xml
|
||||
|
||||
html/
|
||||
coverage/
|
||||
|
|
|
@ -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__':
|
||||
|
|
Loading…
Reference in New Issue
Block a user