[ python-Bugs-878275 ] Handle: class MyTest(unittest.TestSuite)

SourceForge.net noreply at sourceforge.net
Sun Dec 4 20:24:00 CET 2005


Bugs item #878275, was opened at 2004-01-16 14:22
Message generated for change (Comment added) made by jjlee
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=878275&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.3
Status: Open
Resolution: None
Priority: 6
Submitted By: Raymond Hettinger (rhettinger)
Assigned to: Steve Purcell (purcell)
Summary: Handle: class MyTest(unittest.TestSuite)

Initial Comment:
TestCases are supposed to be derived from 
unittest.TestCase; however, if they are derived from 
unittest.TestSuite, the traceback is inexplicable and 
hard to diagnose:


Traceback (most recent call last):
  File "C:/pydev/tmp.py", line 10, in -toplevel-
    unittest.TextTestRunner(verbosity=2).run(suite)
  File "C:\PY24\lib\unittest.py", line 690, in run
    test(result)
  File "C:\PY24\lib\unittest.py", line 423, in __call__
    test(result)
  File "C:\PY24\lib\unittest.py", line 423, in __call__
    test(result)
  File "C:\PY24\lib\unittest.py", line 423, in __call__
    test(result)
TypeError: 'str' object is not callable

Let's either improve the error message or make it 
possible to derive from TestSuite.

The above traceback is produced by the following script:

import unittest

class TestStats(unittest.TestSuite):

    def testtwo(self):
        self.assertEqual(2,2)

suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestStats))
unittest.TextTestRunner(verbosity=2).run(suite)

----------------------------------------------------------------------

Comment By: John J Lee (jjlee)
Date: 2005-12-04 19:24

Message:
Logged In: YES 
user_id=261020

In fact in jlgijsbers' case (TestSuite class passed to
TestSuite, rather than another bug: TestCase class passed to
TestSuite), that check just pushes the problem back a bit:

import unittest
class TestStats(unittest.TestSuite):
    def testtwo(self):
        self.assertEqual(2,2)
suite = unittest.TestSuite([TestStats('testtwo')])
unittest.TextTestRunner(verbosity=2).run(suite)

TypeError: 'str' object is not callable

Another test could be added to TestSuite addTest():

if not callable(test): raise TypeError('addTest argument 1
must be callable')

Again, though it does makes the error clearer, it seems
debatable that it's worth the change...


----------------------------------------------------------------------

Comment By: John J Lee (jjlee)
Date: 2005-12-04 18:52

Message:
Logged In: YES 
user_id=261020

Just to be clear: The issue with jlgijsbers' odd error
message (in his 2004-11-07 followup) is not that a TestSuite
is passed to the TestSuite constructor, but rather that he's
passing in a class, not an instance.

The error would show up a bit earlier if a check were added
to TestSuite.addTest():

if type(test) == type: raise TypeError('addTest argument 1
must be instance, not class')

I will post that line as a patch if anyone emails me to
request it!


----------------------------------------------------------------------

Comment By: Johannes Gijsbers (jlgijsbers)
Date: 2004-11-07 15:53

Message:
Logged In: YES 
user_id=469548

Well, the error message above has been fixed, but here's
another equally confusing variant:

import unittest

class TestStats(unittest.TestSuite):
    def testtwo(self):
        self.assertEqual(2,2)

suite = unittest.TestSuite([TestStats])
unittest.TextTestRunner(verbosity=2).run(suite)

giving:

Traceback (most recent call last):
  File "test-foo.py", line 8, in ?
    unittest.TextTestRunner(verbosity=2).run(suite)
  File "/home/johannes/python/Lib/unittest.py", line 695, in run
    test(result)
  File "/home/johannes/python/Lib/unittest.py", line 426, in
__call__
    test(result)
  File "/home/johannes/python/Lib/unittest.py", line 396, in
__init__
    self.addTests(tests)
  File "/home/johannes/python/Lib/unittest.py", line 416, in
addTests
    for test in tests:
TypeError: iteration over non-sequence

so I'm leaving this open.

----------------------------------------------------------------------

Comment By: Irmen de Jong (irmen)
Date: 2004-11-07 13:39

Message:
Logged In: YES 
user_id=129426

patch is at #1061904

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=878275&group_id=5470


More information about the Python-bugs-list mailing list