Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9031 -- Clarified the error message when the label used to run…

… a specific test case method doesn't name a valid test case class. Thanks to Thomas Guettler for the suggestion and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9879 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5f7c6a8d29e9353ffa1793e45de57274d4409767 1 parent 8569157
Russell Keith-Magee authored February 22, 2009

Showing 1 changed file with 29 additions and 27 deletions. Show diff stats Hide diff stats

  1. 56  django/test/simple.py
56  django/test/simple.py
@@ -7,7 +7,7 @@
7 7
 
8 8
 # The module name for tests outside models.py
9 9
 TEST_MODULE = 'tests'
10  
-    
  10
+
11 11
 doctestOutputChecker = OutputChecker()
12 12
 
13 13
 def get_tests(app_module):
@@ -25,7 +25,7 @@ def get_tests(app_module):
25 25
             # 'tests' module doesn't exist. Move on.
26 26
             test_module = None
27 27
         else:
28  
-            # The module exists, so there must be an import error in the 
  28
+            # The module exists, so there must be an import error in the
29 29
             # test module itself. We don't need the module; so if the
30 30
             # module was a single file module (i.e., tests.py), close the file
31 31
             # handle returned by find_module. Otherwise, the test module
@@ -34,11 +34,11 @@ def get_tests(app_module):
34 34
                 mod[0].close()
35 35
             raise
36 36
     return test_module
37  
-    
  37
+
38 38
 def build_suite(app_module):
39 39
     "Create a complete Django test suite for the provided application module"
40 40
     suite = unittest.TestSuite()
41  
-    
  41
+
42 42
     # Load unit and doctests in the models.py module. If module has
43 43
     # a suite() method, use it. Otherwise build the test suite ourselves.
44 44
     if hasattr(app_module, 'suite'):
@@ -52,8 +52,8 @@ def build_suite(app_module):
52 52
         except ValueError:
53 53
             # No doc tests in models.py
54 54
             pass
55  
-    
56  
-    # Check to see if a separate 'tests' module exists parallel to the 
  55
+
  56
+    # Check to see if a separate 'tests' module exists parallel to the
57 57
     # models module
58 58
     test_module = get_tests(app_module)
59 59
     if test_module:
@@ -63,8 +63,8 @@ def build_suite(app_module):
63 63
             suite.addTest(test_module.suite())
64 64
         else:
65 65
             suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_module))
66  
-            try:            
67  
-                suite.addTest(doctest.DocTestSuite(test_module, 
  66
+            try:
  67
+                suite.addTest(doctest.DocTestSuite(test_module,
68 68
                                                    checker=doctestOutputChecker,
69 69
                                                    runner=DocTestRunner))
70 70
             except ValueError:
@@ -73,15 +73,15 @@ def build_suite(app_module):
73 73
     return suite
74 74
 
75 75
 def build_test(label):
76  
-    """Construct a test case a test with the specified label. Label should 
  76
+    """Construct a test case a test with the specified label. Label should
77 77
     be of the form model.TestClass or model.TestClass.test_method. Returns
78 78
     an instantiated test or test suite corresponding to the label provided.
79  
-        
  79
+
80 80
     """
81 81
     parts = label.split('.')
82 82
     if len(parts) < 2 or len(parts) > 3:
83 83
         raise ValueError("Test label '%s' should be of the form app.TestCase or app.TestCase.test_method" % label)
84  
-    
  84
+
85 85
     app_module = get_app(parts[0])
86 86
     TestClass = getattr(app_module, parts[1], None)
87 87
 
@@ -95,13 +95,15 @@ def build_test(label):
95 95
         try:
96 96
             return unittest.TestLoader().loadTestsFromTestCase(TestClass)
97 97
         except TypeError:
98  
-            raise ValueError("Test label '%s' does not refer to a test class" % label)            
  98
+            raise ValueError("Test label '%s' does not refer to a test class" % label)
99 99
     else: # label is app.TestClass.test_method
  100
+        if not TestClass:
  101
+            raise ValueError("Test label '%s' does not refer to a test class" % label)
100 102
         return TestClass(parts[2])
101 103
 
102 104
 # Python 2.3 compatibility: TestSuites were made iterable in 2.4.
103 105
 # We need to iterate over them, so we add the missing method when
104  
-# necessary.    
  106
+# necessary.
105 107
 try:
106 108
     getattr(unittest.TestSuite, '__iter__')
107 109
 except AttributeError:
@@ -110,11 +112,11 @@ def build_test(label):
110 112
 def partition_suite(suite, classes, bins):
111 113
     """
112 114
     Partitions a test suite by test type.
113  
-    
  115
+
114 116
     classes is a sequence of types
115 117
     bins is a sequence of TestSuites, one more than classes
116  
-    
117  
-    Tests of type classes[i] are added to bins[i], 
  118
+
  119
+    Tests of type classes[i] are added to bins[i],
118 120
     tests with no match found in classes are place in bins[-1]
119 121
     """
120 122
     for test in suite:
@@ -127,13 +129,13 @@ def partition_suite(suite, classes, bins):
127 129
                     break
128 130
             else:
129 131
                 bins[-1].addTest(test)
130  
-            
  132
+
131 133
 def reorder_suite(suite, classes):
132 134
     """
133 135
     Reorders a test suite by test type.
134  
-    
  136
+
135 137
     classes is a sequence of types
136  
-    
  138
+
137 139
     All tests of type clases[0] are placed first, then tests of type classes[1], etc.
138 140
     Tests with no match in classes are placed last.
139 141
     """
@@ -157,17 +159,17 @@ def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
157 159
 
158 160
     When looking for tests, the test runner will look in the models and
159 161
     tests modules for the application.
160  
-    
  162
+
161 163
     A list of 'extra' tests may also be provided; these tests
162 164
     will be added to the test suite.
163  
-    
  165
+
164 166
     Returns the number of tests that failed.
165 167
     """
166 168
     setup_test_environment()
167  
-    
168  
-    settings.DEBUG = False    
  169
+
  170
+    settings.DEBUG = False
169 171
     suite = unittest.TestSuite()
170  
-    
  172
+
171 173
     if test_labels:
172 174
         for label in test_labels:
173 175
             if '.' in label:
@@ -178,7 +180,7 @@ def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
178 180
     else:
179 181
         for app in get_apps():
180 182
             suite.addTest(build_suite(app))
181  
-    
  183
+
182 184
     for test in extra_tests:
183 185
         suite.addTest(test)
184 186
 
@@ -189,7 +191,7 @@ def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
189 191
     connection.creation.create_test_db(verbosity, autoclobber=not interactive)
190 192
     result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
191 193
     connection.creation.destroy_test_db(old_name, verbosity)
192  
-    
  194
+
193 195
     teardown_test_environment()
194  
-    
  196
+
195 197
     return len(result.failures) + len(result.errors)

0 notes on commit 5f7c6a8

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