Skip to content.

plope

Personal tools
You are here: Home » Members » chrism's Home » Unittest findTestCases
 
 

Unittest findTestCases

A reminder to myself about what to put at the bottom of unit test files when the tests are to be found by a testrunner that looks at "test_suite" for the suite in each test module.

It's useful to use the findTestCases utility function to find all the test cases in the current module, instead of makeSuite'ing each test case manually. For example, instead of:

  import unittest

  class TestProxyBase(unittest.TestCase):
      def _makeOne(self, id, resource):
          from Products.davproxy.resource import ProxyBase
          return ProxyBase(id, resource)

      def test_getId(self):
          p = self._makeOne('theid', None)
          self.assertEqual(p.getId(), 'theid')

      def test_getId_utf8(self):
          p = self._makeOne(u'dummytitle-\u03cb', None)
          self.assertEqual(p.getId().decode('utf-8'), u'dummytitle-\u03cb')

  def test_suite():
      return unittest.TestSuite((
          unittest.makeSuite(TestProxyBase),
          ))

  if __name__ == '__main__':
      unittest.main(defaultTest='test_suite')

I really want do do this, so I don't have to keep adding stuff to "test_suite" when I add more test cases to the module:

  import unittest

  class TestProxyBase(unittest.TestCase):
      def _makeOne(self, id, resource):
          from Products.davproxy.resource import ProxyBase
          return ProxyBase(id, resource)

      def test_getId(self):
          p = self._makeOne('theid', None)
          self.assertEqual(p.getId(), 'theid')

      def test_getId_utf8(self):
          p = self._makeOne(u'dummytitle-\u03cb', None)
          self.assertEqual(p.getId().decode('utf-8'), u'dummytitle-\u03cb')

  def test_suite():
      import sys
      return unittest.findTestCases(sys.modules[__name__])

  if __name__ == '__main__':
      unittest.main(defaultTest='test_suite')

Created by chrism
Last modified 2008-02-19 11:57 AM

How did you find that?

I don't see it anywhere in the docs... certainly not where you'd expect:
http://docs.python.org/lib/unittest-contents.html

no clue...

Culled from some lore somewhere..

Ah, it's obsolete.

Apparently it's marked as obsolete (but no DeprecationWarning?) in 2.5.
See http://bugs.python.org/issue2162
Judging from the source, I guess the replacement is to use TestLoader.loadTestsFromModule