forked from praekelt/django-setuptest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setuptest.py
156 lines (136 loc) · 4.99 KB
/
setuptest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import pep8
import sys
import unittest
from coverage import coverage, misc
from distutils import log
from StringIO import StringIO
class SetupTestSuite(unittest.TestSuite):
"""
Test Suite configuring Django settings and using
DjangoTestSuiteRunner as test runner.
Also runs PEP8 and Coverage checks.
"""
def __init__(self, *args, **kwargs):
self.configure()
self.cov = coverage()
self.cov.start()
self.packages = self.resolve_packages()
super(SetupTestSuite, self).__init__(tests=self.build_tests(), \
*args, **kwargs)
# Setup testrunner.
from django.test.simple import DjangoTestSuiteRunner
self.test_runner = DjangoTestSuiteRunner(
verbosity=1,
interactive=True,
failfast=False
)
self.test_runner.setup_test_environment()
self.old_config = self.test_runner.setup_databases()
def build_tests(self):
"""
Build tests for inclusion in suite from resolved packages.
"""
from django.core.exceptions import ImproperlyConfigured
from django.db.models import get_app
from django.test.simple import build_suite
tests = []
for package in self.packages:
try:
app = get_app(package)
tests.append(build_suite(app))
except ImproperlyConfigured, e:
log.info("Warning: %s" % e)
except ImportError, e:
log.info("Warning: %s" % e)
return tests
def configure(self):
"""
Configures Django settings.
"""
from django.conf import settings
from django.utils.importlib import import_module
try:
test_settings = import_module('test_settings')
except ImportError, e:
log.info('ImportError: Unable to import test settings: %s' % e)
sys.exit(1)
setting_attrs = {}
for attr in dir(test_settings):
if '__' not in attr:
setting_attrs[attr] = getattr(test_settings, attr)
if not settings.configured:
settings.configure(**setting_attrs)
def coverage_report(self):
"""
Outputs Coverage report to screen and coverage.xml.
"""
verbose = '--quiet' not in sys.argv
self.cov.stop()
if verbose:
log.info("\nCoverage Report:")
try:
include = ['%s*' % package for package in self.packages]
omit = ['*tests*']
self.cov.report(include=include, omit=omit)
self.cov.xml_report(include=include, omit=omit)
except misc.CoverageException, e:
log.info("Coverage Exception: %s" % e)
def resolve_packages(self):
"""
Frame hack to determine packages contained in module for testing.
We ignore submodules (those containing '.')
"""
f = sys._getframe()
while f:
if 'self' in f.f_locals:
locals_self = f.f_locals['self']
py_modules = getattr(locals_self, 'py_modules', None)
packages = getattr(locals_self, 'packages', None)
top_packages = []
if py_modules or packages:
if py_modules:
for module in py_modules:
if '.' not in module:
top_packages.append(module)
if packages:
for package in packages:
if '.' not in package:
top_packages.append(package)
return list(set(top_packages))
f = f.f_back
def pep8_report(self):
"""
Outputs PEP8 report to screen and pep8.txt.
"""
verbose = '--quiet' not in sys.argv
if verbose:
# Hook into stdout.
old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()
# Run Pep8 checks.
pep8.options, pep8.args = pep8.process_options()
pep8.args = self.packages
pep8.options.repeat = True
for package in self.packages:
pep8.input_dir(package)
# Restore stdout.
sys.stdout = old_stdout
# Save result to pep8.txt.
result = mystdout.getvalue()
output = open('pep8.txt', 'w')
output.write(result)
output.close()
# Return Pep8 result
if result:
log.info("\nPEP8 Report:")
log.info(result)
def run(self, *args, **kwargs):
"""
Run the test, teardown the environment and generate reports.
"""
result = super(SetupTestSuite, self).run(*args, **kwargs)
self.test_runner.teardown_databases(self.old_config)
self.test_runner.teardown_test_environment()
self.coverage_report()
self.pep8_report()
return result