Code

Ticket #3253: runtests.diff

File runtests.diff, 3.6 KB (added by mir@…, 7 years ago)

the patch

Line 
1--- a/django/core/management.py
2+++ b/django/core/management.py
3@@ -1242,7 +1242,12 @@ def test(app_labels, verbosity=1):
4     test_module = __import__(test_module_name, {}, {}, test_path[-1])
5     test_runner = getattr(test_module, test_path[-1])
6 
7-    test_runner(app_list, verbosity)
8+    failures = test_runner(app_list, verbosity)
9+    if failures:
10+        sys.exit(1)
11+    else:
12+        sys.exit(0)
13+       
14 test.help_doc = 'Runs the test suite for the specified applications, or the entire site if no apps are specified'
15 test.args = '[--verbosity] ' + APP_ARGS
16 
17diff --git a/django/test/simple.py b/django/test/simple.py
18index 88e6b49925605d9cd141c77f328b99e8012bab1b..7c0bd9dd8a0124accfd797a195914cf3f586d285 100644
19--- a/django/test/simple.py
20+++ b/django/test/simple.py
21@@ -64,6 +64,8 @@ def run_tests(module_list, verbosity=1,
22     looking for doctests and unittests in models.py or tests.py within
23     the module. A list of 'extra' tests may also be provided; these tests
24     will be added to the test suite.
25+
26+    Returns the number of failures.
27     """
28     setup_test_environment()
29     
30@@ -79,7 +81,8 @@ def run_tests(module_list, verbosity=1,
31     old_name = settings.DATABASE_NAME
32     create_test_db(verbosity)
33     management.syncdb(verbosity, interactive=False)
34-    unittest.TextTestRunner(verbosity=verbosity).run(suite)
35+    test_result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
36     destroy_test_db(old_name, verbosity)
37     
38     teardown_test_environment()
39+    return len(test_result.failures)
40\ No newline at end of file
41diff --git a/docs/testing.txt b/docs/testing.txt
42index a0b8a8a18724dd631db5c5613af98d328875ee2f..b2e8a46054eb242fb2b53f34503ece0e5ddab134 100644
43--- a/docs/testing.txt
44+++ b/docs/testing.txt
45@@ -379,6 +379,9 @@ failed::
46 
47 When the tests have all been executed, the test database is destroyed.
48 
49+You can use the exit code of ``manage.py test`` for automation: it is
50+0 in case of success, and 1 in case of any test failures.
51+
52 Using a different testing framework
53 ===================================
54 
55@@ -398,6 +401,7 @@ #. Looking for Unit Tests and Doctests i
56 #. Running the Unit Tests and Doctests that are found
57 #. Destroying the test database.
58 #. Performing global post-test teardown
59+#. Returning the number of failures
60 
61 If you define your own test runner method and point ``TEST_RUNNER``
62 at that method, Django will execute your test runner whenever you run
63diff --git a/tests/runtests.py b/tests/runtests.py
64index 20189c2d995c5dca6dda893c56a5078a3ffe2909..bae4dc76262984b5cef87d5dca0f3073ee8f12fe 100755
65--- a/tests/runtests.py
66+++ b/tests/runtests.py
67@@ -118,7 +118,7 @@ def django_tests(verbosity, tests_to_run
68 
69     # Run the test suite, including the extra validation tests.
70     from django.test.simple import run_tests
71-    run_tests(test_models, verbosity, extra_tests=extra_tests)
72+    failures = run_tests(test_models, verbosity, extra_tests=extra_tests)
73 
74     # Restore the old settings
75     settings.INSTALLED_APPS = old_installed_apps
76@@ -127,6 +127,8 @@ def django_tests(verbosity, tests_to_run
77     settings.TEMPLATE_DIRS = old_template_dirs
78     settings.USE_I18N = old_use_i18n
79 
80+    return failures
81+
82 if __name__ == "__main__":
83     from optparse import OptionParser
84     usage = "%prog [options] [model model model ...]"
85@@ -140,4 +142,8 @@ if __name__ == "__main__":
86     if options.settings:
87         os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
88 
89-    django_tests(int(options.verbosity), args)
90+    failures = django_tests(int(options.verbosity), args)
91+    if failures:
92+        sys.exit(1)
93+    else:
94+        sys.exit(0)