Changeset 149 for trunk/test

Show
Ignore:
Timestamp:
12/31/07 13:14:18 (3 years ago)
Author:
akaihola
Message:

[test] Completed the test.simple.run_tests_until_fail() utility.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/test/simple.py

    r148 r149  
     1__doc__ = """ 
     2In your settings, use 
     3 
     4  TEST_RUNNER = 'ambidjangolib.test.simple.run_tests_until_fail' 
     5 
     6to make `manage.py test` stop after the first test suite with failures, and 
     7only show the first failing test in the suite. 
     8""" 
     9 
     10 
    111from unittest import \ 
    212     TestSuite, TextTestRunner, _TextTestResult, defaultTestLoader 
    3 from django.test._doctest import DocTestSuite, REPORT_ONLY_FIRST_FAILURE 
     13from django.test import _doctest as doctest 
    414from django.conf import settings 
     15from django.db import transaction 
    516from django.db.models import get_app, get_apps 
    6 from django.test.utils import setup_test_environment, teardown_test_environment 
    7 from django.test.utils import create_test_db, destroy_test_db 
     17from django.test.utils import \ 
     18     setup_test_environment, teardown_test_environment, \ 
     19     create_test_db, destroy_test_db 
    820from django.test.simple import build_test, get_tests, doctestOutputChecker 
    921from django.test.testcases import DocTestRunner 
    1022 
    1123 
     24class DocTestRunner(doctest.DocTestRunner): 
     25    """ 
     26    Replacement for django.test.testcases.DocTestRunner which unfortunately 
     27    overrides any supplied `optionflags=` kwarg with only `doctest.ELLIPSIS`. 
     28    We need to pass on optionflags. 
     29    """ 
     30    def __init__(self, *args, **kwargs): 
     31        doctest.DocTestRunner.__init__(self, *args, **kwargs) 
     32        self.optionflags |= doctest.ELLIPSIS 
     33        # Django's original has `=` instead of `|=` here 
     34 
     35    def report_unexpected_exception(self, out, test, example, exc_info): 
     36        doctest.DocTestRunner.report_unexpected_exception(self, out, test, 
     37                                                          example, exc_info) 
     38        # Rollback, in case of database errors. Otherwise they'd have 
     39        # side effects on other tests. 
     40        transaction.rollback_unless_managed() 
     41 
     42 
    1243def _add_tests_for_module(suite, module): 
     44    """ 
     45    Repeated code from inside `build_suite()` is refactored here. 
     46    """ 
    1347    # Load unit and doctests in the given module. If module has a suite() 
    1448    # method, use it. Otherwise build the test suite ourselves. 
     
    1852        suite.addTest(defaultTestLoader.loadTestsFromModule(module)) 
    1953        try: 
    20             suite.addTest(DocTestSuite(module, 
    21                                        checker=doctestOutputChecker, 
    22                                        runner=DocTestRunner, 
    23                                        optionflags=REPORT_ONLY_FIRST_FAILURE)) 
     54            suite.addTest(doctest.DocTestSuite( 
     55                module, 
     56                checker=doctestOutputChecker, 
     57                runner=DocTestRunner, 
     58                optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)) 
    2459        except ValueError: 
    2560            # No doc tests in models.py 
     
    2863 
    2964def build_suite(app_module): 
    30     "Create a complete Django test suite for the provided application module" 
     65    """ 
     66    Create a complete Django test suite for the provided application module. 
     67 
     68    This overrides Django's original `django.test.simple.build_suite()` because 
     69    we need to pass the `REPORT_ONLY_FIRST_FAILURE` option flag to 
     70    `DocTestSuite` instances. 
     71    """ 
    3172    suite = TestSuite() 
    3273 
    3374    _add_tests_for_module(suite, app_module) 
    3475 
    35     # Check to see if a separate 'tests' module exists parallel to the  
     76    # Check to see if a separate 'tests' module exists parallel to the 
    3677    # models module 
    3778    test_module = get_tests(app_module) 
     
    77118    Stops the tests at the first failure and returns 1.  If all test pass, 
    78119    returns 0. 
     120 
     121    Also displays only the first failure in the failing test suite. 
    79122    """ 
    80123    setup_test_environment()