Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3782 -- Added support for the suite() method recommended by th…

…e Python unittest docs. Thanks for the suggestion, rene.puls@repro-mayr.de.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5729 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9922a04bb6a9587aee197c6d0e87de9e5bd7e240 1 parent 73bec37
Russell Keith-Magee authored July 20, 2007
43  django/test/simple.py
@@ -14,15 +14,19 @@ def build_suite(app_module):
14 14
     "Create a complete Django test suite for the provided application module"
15 15
     suite = unittest.TestSuite()
16 16
     
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
26 30
     
27 31
     # Check to see if a separate 'tests' module exists parallel to the 
28 32
     # models module
@@ -30,14 +34,19 @@ def build_suite(app_module):
30 34
         app_path = app_module.__name__.split('.')[:-1]
31 35
         test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE)
32 36
         
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
41 50
     except ImportError, e:
42 51
         # Couldn't import tests.py. Was it due to a missing file, or
43 52
         # due to an import error in a tests.py that actually exists?
15  docs/testing.txt
@@ -118,10 +118,16 @@ An equivalent unittest test case for the above example would look like::
118 118
             self.assertEquals(self.lion.speak(), 'The lion says "roar"')
119 119
             self.assertEquals(self.cat.speak(), 'The cat says "meow"')
120 120
 
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.
  121
+When you `run your tests`_, the default behavior of the test utility is
  122
+to find all the test cases (that is, subclasses of ``unittest.TestCase``)
  123
+in ``models.py`` and ``tests.py``, automatically build a test suite out of
  124
+those test cases, and run that suite.
  125
+
  126
+However, 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
  128
+module. This follows the `suggested organization`_ for unit tests. See the
  129
+Python documentation for more details on how to construct a complex test
  130
+suite.
125 131
 
126 132
 For more details about ``unittest``, see the `standard library unittest
127 133
 documentation`_.
@@ -129,6 +135,7 @@ documentation`_.
129 135
 .. _unittest: http://docs.python.org/lib/module-unittest.html
130 136
 .. _standard library unittest documentation: unittest_
131 137
 .. _run your tests: `Running tests`_
  138
+.. _suggested organization: http://docs.python.org/lib/organizing-tests.html
132 139
 
133 140
 Which should I use?
134 141
 -------------------
20  tests/modeltests/test_client/tests.py
... ...
@@ -0,0 +1,20 @@
  1
+# Validate that you can override the default test suite
  2
+
  3
+import unittest
  4
+
  5
+def suite():
  6
+    """
  7
+    Define a suite that deliberately ignores a test defined in
  8
+    this module.
  9
+    """
  10
+    
  11
+    testSuite = unittest.TestSuite()
  12
+    testSuite.addTest(SampleTests('testGoodStuff'))
  13
+    return testSuite
  14
+    
  15
+class SampleTests(unittest.TestCase):
  16
+    def testGoodStuff(self):
  17
+        pass
  18
+        
  19
+    def testBadStuff(self):
  20
+        self.fail("This test shouldn't run")

0 notes on commit 9922a04

Please sign in to comment.
Something went wrong with that request. Please try again.