Django

Code

Changeset 5729

Show
Ignore:
Timestamp:
07/20/07 08:57:49 (1 year ago)
Author:
russellm
Message:

Fixed #3782 -- Added support for the suite() method recommended by the Python unittest docs. Thanks for the suggestion, rene.puls@repro-mayr.de.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/test/simple.py

    r5391 r5729  
    1515    suite = unittest.TestSuite() 
    1616     
    17     # Load unit and doctests in the models.py file 
    18     suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(app_module)) 
    19     try: 
    20         suite.addTest(doctest.DocTestSuite(app_module, 
    21                                            checker=doctestOutputChecker, 
    22                                            runner=DocTestRunner)) 
    23     except ValueError: 
    24         # No doc tests in models.py 
    25         pass 
     17    # Load unit and doctests in the models.py module. If module has 
     18    # a suite() method, use it. Otherwise build the test suite ourselves. 
     19    if hasattr(app_module, 'suite'): 
     20        suite.addTest(app_module.suite()) 
     21    else: 
     22        suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(app_module)) 
     23        try: 
     24            suite.addTest(doctest.DocTestSuite(app_module, 
     25                                               checker=doctestOutputChecker, 
     26                                               runner=DocTestRunner)) 
     27        except ValueError: 
     28            # No doc tests in models.py 
     29            pass 
    2630     
    2731    # Check to see if a separate 'tests' module exists parallel to the  
     
    3135        test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE) 
    3236         
    33         suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_module)) 
    34         try:             
    35             suite.addTest(doctest.DocTestSuite(test_module,  
    36                                                checker=doctestOutputChecker, 
    37                                                runner=DocTestRunner)) 
    38         except ValueError: 
    39             # No doc tests in tests.py 
    40             pass 
     37        # Load unit and doctests in the tests.py module. If module has 
     38        # a suite() method, use it. Otherwise build the test suite ourselves. 
     39        if hasattr(test_module, 'suite'): 
     40            suite.addTest(test_module.suite()) 
     41        else: 
     42            suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_module)) 
     43            try:             
     44                suite.addTest(doctest.DocTestSuite(test_module,  
     45                                                   checker=doctestOutputChecker, 
     46                                                   runner=DocTestRunner)) 
     47            except ValueError: 
     48                # No doc tests in tests.py 
     49                pass 
    4150    except ImportError, e: 
    4251        # Couldn't import tests.py. Was it due to a missing file, or 
  • django/trunk/docs/testing.txt

    r5702 r5729  
    119119            self.assertEquals(self.cat.speak(), 'The cat says "meow"') 
    120120 
    121 When you `run your tests`_, the test utility will find all the test cases 
    122 (that is, subclasses of ``unittest.TestCase``) in ``models.py`` and 
    123 ``tests.py``, automatically build a test suite out of those test cases, 
    124 and run that suite. 
     121When you `run your tests`_, the default behavior of the test utility is 
     122to find all the test cases (that is, subclasses of ``unittest.TestCase``) 
     123in ``models.py`` and ``tests.py``, automatically build a test suite out of 
     124those test cases, and run that suite. 
     125 
     126However, if you define a method called ``suite()`` in either ``models.py`` or 
     127``tests.py``, that method will be used to construct the test suite for that 
     128module. This follows the `suggested organization`_ for unit tests. See the 
     129Python documentation for more details on how to construct a complex test 
     130suite. 
    125131 
    126132For more details about ``unittest``, see the `standard library unittest 
     
    130136.. _standard library unittest documentation: unittest_ 
    131137.. _run your tests: `Running tests`_ 
     138.. _suggested organization: http://docs.python.org/lib/organizing-tests.html 
    132139 
    133140Which should I use?