Skip to content

Commit 9922a04

Browse files
committed
Fixed #3782 -- Added support for the suite() method recommended by the 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
1 parent 73bec37 commit 9922a04

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

django/test/simple.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,39 @@ def build_suite(app_module):
1414
"Create a complete Django test suite for the provided application module"
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
2832
# models module
2933
try:
3034
app_path = app_module.__name__.split('.')[:-1]
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
4352
# due to an import error in a tests.py that actually exists?

docs/testing.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,24 @@ An equivalent unittest test case for the above example would look like::
118118
self.assertEquals(self.lion.speak(), 'The lion says "roar"')
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.
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.
125131

126132
For more details about ``unittest``, see the `standard library unittest
127133
documentation`_.
128134

129135
.. _unittest: http://docs.python.org/lib/module-unittest.html
130136
.. _standard library unittest documentation: unittest_
131137
.. _run your tests: `Running tests`_
138+
.. _suggested organization: http://docs.python.org/lib/organizing-tests.html
132139

133140
Which should I use?
134141
-------------------
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)