diff --git a/django/test/simple.py b/django/test/simple.py
index f3c48ba..488d41e 100644
a
|
b
|
def reorder_suite(suite, classes):
|
146 | 146 | bins[0].addTests(bins[i+1]) |
147 | 147 | return bins[0] |
148 | 148 | |
149 | | def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): |
| 149 | def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], test_result=None): |
150 | 150 | """ |
151 | 151 | Run the unit tests for all the test labels in the provided list. |
152 | 152 | Labels must be of the form: |
… |
… |
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
|
163 | 163 | A list of 'extra' tests may also be provided; these tests |
164 | 164 | will be added to the test suite. |
165 | 165 | |
166 | | Returns the number of tests that failed. |
| 166 | If a unittest.TestResult is provided, tests will instead be run and |
| 167 | the TestResult will be updated with results. |
| 168 | |
| 169 | Returns the number of tests that failed or errored |
167 | 170 | """ |
168 | 171 | setup_test_environment() |
169 | 172 | |
… |
… |
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
|
189 | 192 | old_name = settings.DATABASE_NAME |
190 | 193 | from django.db import connection |
191 | 194 | connection.creation.create_test_db(verbosity, autoclobber=not interactive) |
192 | | result = unittest.TextTestRunner(verbosity=verbosity).run(suite) |
| 195 | if test_result: |
| 196 | result = ResultTestRunner(test_result).run(suite) |
| 197 | else: |
| 198 | result = unittest.TextTestRunner(verbosity=verbosity).run(suite) |
193 | 199 | connection.creation.destroy_test_db(old_name, verbosity) |
194 | 200 | |
195 | 201 | teardown_test_environment() |
196 | 202 | |
197 | 203 | return len(result.failures) + len(result.errors) |
| 204 | |
| 205 | class ResultTestRunner(object): |
| 206 | """ |
| 207 | TestRunner that uses supplied TestResult to run tests and report results. |
| 208 | """ |
| 209 | def __init__(self, test_result): |
| 210 | self.test_result = test_result |
| 211 | |
| 212 | def run(self, test): |
| 213 | results = self.test_result |
| 214 | for t in test: |
| 215 | t.run(results) |
| 216 | return results |
diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt
index 5bda3d6..60ec0dd 100644
a
|
b
|
Defining a test runner
|
1188 | 1188 | By convention, a test runner should be called ``run_tests``. The only strict |
1189 | 1189 | requirement is that it has the same arguments as the Django test runner: |
1190 | 1190 | |
1191 | | .. function:: run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]) |
| 1191 | .. function:: run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], test_result=None) |
1192 | 1192 | |
1193 | 1193 | ``test_labels`` is a list of strings describing the tests to be run. A test |
1194 | 1194 | label can take one of three forms: |
… |
… |
requirement is that it has the same arguments as the Django test runner:
|
1215 | 1215 | suite that is executed by the test runner. These extra tests are run |
1216 | 1216 | in addition to those discovered in the modules listed in ``module_list``. |
1217 | 1217 | |
1218 | | This method should return the number of tests that failed. |
| 1218 | If ``test_result`` is supplied a ``unittest.TestResult``, then tests run |
| 1219 | will save results in this ``TestResult``. Otherwise this method should |
| 1220 | return the number of tests that failed. |
1219 | 1221 | |
1220 | 1222 | Testing utilities |
1221 | 1223 | ----------------- |