With Python unittest, how do I create and use a "callable object that returns a test suite"?
<p>I'm learning Python and have been trying to understand more about the details of Python's <code>unittest</code> module. The documentation includes the following:</p>

<blockquote>
  <p>For the ease of running tests, as we
  will see later, it is a good idea to
  provide in each test module a callable
  object that returns a pre-built test
  suite:</p>

<pre><code>def suite():
    suite = unittest.TestSuite()
    suite.addTest(WidgetTestCase('testDefaultSize'))
    suite.addTest(WidgetTestCase('testResize'))
    return suite
</code></pre>
</blockquote>

<p>As far as I can tell, the purpose of doing this is not explained. In addition, I was unable to figure out how one would use such a method. I tried several things without success (aside from learning about the error messages I got):</p>

<pre><code>import unittest

def average(values):
    return sum(values) / len(values)

class MyTestCase(unittest.TestCase):
    def testFoo(self):
        self.assertEqual(average([10,100]),55)

    def testBar(self):
        self.assertEqual(average([11]),11)

    def testBaz(self):
        self.assertEqual(average([20,20]),20)

    def suite():
        suite = unittest.TestSuite()
        suite.addTest(MyTestCase('testFoo'))
        suite.addTest(MyTestCase('testBar'))
        suite.addTest(MyTestCase('testBaz'))
        return suite

if __name__ == '__main__':
    # s = MyTestCase.suite()
    # TypeError: unbound method suite() must be called 
    # with MyTestCase instance as first argument

    # s = MyTestCase.suite(MyTestCase())
    # ValueError: no such test method in &lt;class '__main__.MyTestCase'&gt;: runTest

    # s = MyTestCase.suite(MyTestCase('testFoo'))
    # TypeError: suite() takes no arguments (1 given)
</code></pre>

<p>The following "worked" but seems awkward and it required that I change the method signature of <code>suite()</code> to '<code>def suite(self):</code>'.</p>

<pre><code>s = MyTestCase('testFoo').suite()
unittest.TextTestRunner().run(s)
</code></pre>
