From f313e07b6e0914130b613c3491b2b019ca003dc7 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 26 Feb 2007 12:52:01 +0000 Subject: [PATCH] Fixed #3253 -- Exposed the number of failed tests as a return code in manage.py and runtests.py. git-svn-id: http://code.djangoproject.com/svn/django/trunk@4608 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management.py | 5 ++++- django/test/simple.py | 7 ++++++- docs/testing.txt | 12 +++++++++--- tests/runtests.py | 4 +++- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/django/core/management.py b/django/core/management.py index ae610e696d..953eeca734 100644 --- a/django/core/management.py +++ b/django/core/management.py @@ -1240,7 +1240,10 @@ def test(app_labels, verbosity=1): test_module = __import__(test_module_name, {}, {}, test_path[-1]) test_runner = getattr(test_module, test_path[-1]) - test_runner(app_list, verbosity) + failures = test_runner(app_list, verbosity) + if failures: + sys.exit(failures) + test.help_doc = 'Runs the test suite for the specified applications, or the entire site if no apps are specified' test.args = '[--verbosity] ' + APP_ARGS diff --git a/django/test/simple.py b/django/test/simple.py index 0cecc6b4fc..200f150594 100644 --- a/django/test/simple.py +++ b/django/test/simple.py @@ -63,6 +63,8 @@ def run_tests(module_list, verbosity=1, extra_tests=[]): looking for doctests and unittests in models.py or tests.py within the module. A list of 'extra' tests may also be provided; these tests will be added to the test suite. + + Returns the number of tests that failed. """ setup_test_environment() @@ -77,7 +79,10 @@ def run_tests(module_list, verbosity=1, extra_tests=[]): old_name = settings.DATABASE_NAME create_test_db(verbosity) - unittest.TextTestRunner(verbosity=verbosity).run(suite) + result = unittest.TextTestRunner(verbosity=verbosity).run(suite) destroy_test_db(old_name, verbosity) teardown_test_environment() + + return len(result.failures) + \ No newline at end of file diff --git a/docs/testing.txt b/docs/testing.txt index 33014723cd..57e50f483d 100644 --- a/docs/testing.txt +++ b/docs/testing.txt @@ -417,7 +417,10 @@ failed:: FAILED (failures=1) -When the tests have all been executed, the test database is destroyed. +The return code for the script will indicate the number of tests that failed. + +Regardless of whether the tests pass or fail, the test database is destroyed when +all the tests have been executed. Using a different testing framework =================================== @@ -428,7 +431,8 @@ it does provide a mechanism to allow you to invoke tests constructed for an alternative framework as if they were normal Django tests. When you run ``./manage.py test``, Django looks at the ``TEST_RUNNER`` -setting to determine what to do. By default, ``TEST_RUNNER`` points to ``django.test.simple.run_tests``. This method defines the default Django +setting to determine what to do. By default, ``TEST_RUNNER`` points to +``django.test.simple.run_tests``. This method defines the default Django testing behavior. This behavior involves: #. Performing global pre-test setup @@ -436,7 +440,7 @@ testing behavior. This behavior involves: #. Running ``syncdb`` to install models and initial data into the test database #. Looking for Unit Tests and Doctests in ``models.py`` and ``tests.py`` file for each installed application #. Running the Unit Tests and Doctests that are found -#. Destroying the test database. +#. Destroying the test database #. Performing global post-test teardown If you define your own test runner method and point ``TEST_RUNNER`` @@ -457,6 +461,8 @@ arguments: Verbosity determines the amount of notification and debug information that will be printed to the console; `0` is no output, `1` is normal output, and `2` is verbose output. + + This method should return the number of tests that failed. Testing utilities ----------------- diff --git a/tests/runtests.py b/tests/runtests.py index 1c15f18510..b21a3536ac 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -124,7 +124,9 @@ def django_tests(verbosity, tests_to_run): # Run the test suite, including the extra validation tests. from django.test.simple import run_tests - run_tests(test_models, verbosity, extra_tests=extra_tests) + failures = run_tests(test_models, verbosity, extra_tests=extra_tests) + if failures: + sys.exit(failures) # Restore the old settings. settings.INSTALLED_APPS = old_installed_apps