Title: python -m unittest path_to_suite_function errors
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7, Python 2.6
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: exarkun, james-w, michael.foord, rbcollins
Priority: normal Keywords:

Created on 2009-12-13 22:19 by rbcollins, last changed 2010-08-22 19:50 by michael.foord. This issue is now closed.

Messages (9)
msg96364 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2009-12-13 22:19
:!python -m unittest foo.test_suite  
Traceback (most recent call last):
  File "/usr/lib/python2.6/", line 122, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.6/", line 34, in _run_code
    exec code in run_globals
  File "/usr/lib/python2.6/", line 875, in <module>
  File "/usr/lib/python2.6/", line 816, in __init__
  File "/usr/lib/python2.6/", line 843, in parseArgs
  File "/usr/lib/python2.6/", line 849, in createTests
  File "/usr/lib/python2.6/", line 613, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/lib/python2.6/", line 605, in loadTestsFromName
    (obj, test))
TypeError: calling <function test_suite at 0x7f748dd6d7d0> returned
<unittest.TestSuite tests=[<foo.TestTask testMethod=test_can_construct>,
<foo.TestTask testMethod=test_can_construct>]>, not a test

def test_suite():
    loader = unittest.TestLoader()
    tests = loader.loadTestsFromName(__name__)
    result = loader.suiteClass()
    return result
msg98915 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-02-05 22:25
What made you think that would work? In your example foo.test_suite is neither a test nor a suite but a function.
msg98918 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2010-02-05 23:11
Its a common convention in zope.testing, trial, testtools, bzr, ...
msg98919 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-02-05 23:12
So it's a feature request then...
msg98931 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2010-02-06 04:14
Sounds like one.

As far as the trial behavior goes, "trial foo.test_suite" won't work, but "trial foo" will call "foo.test_suite", if one is defined.
msg99021 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-02-07 21:39
Is this needed as well as the load_tests protocol?

If a test module defines a load_tests function that returns a test suite then the following creates the test suite from load_tests and executes that:

    python -m unittest modulename

I don't think we need a test_suite protocol as well.
msg99173 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-02-10 16:00
Use load_tests instead.
msg114471 - (view) Author: James Westby (james-w) Date: 2010-08-21 01:07

I think this was misdiagnosed:

from in 2.6, loadTestFromName:

        elif hasattr(obj, '__call__'):
            test = obj()
            if isinstance(test, TestSuite):
                return test
            elif isinstance(test, TestCase):
                return TestSuite([test])
                raise TypeError("calling %s returned %s, not a test" %
                                (obj, test))

so it supports callables, such as the test_suite function that
Rob is passing. Therefore I don't think this is a feature request,
and "use load_tests" isn't an appropriate resolution.

I haven't checked what 2.7 and later do, but going on the above
my diagnosis of this is the following.

  1. test_suite is correctly identified as a callable
  2. It is called an returns a unittest.TestSuite
  3. It is not matched as being a a TestSuite or a TestCase, and so the error is raised.

The reason it is not matched is that when run as -m unittest, the unittest module is __main__, and so the TestSuite in the isinstance check is a unittest.TestSuite against a __main__.TestSuite, which won't match.

Therefore I think this is a legitimate bug, in at least 2.6.


msg114698 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-08-22 19:50
Well, it was misdiagnosed yes - but asking for "python -m unittest ..." support in Python 2.6 is still a feature request and not a bug report. (So unfortunately it can't be fixed in 2.6 which is bugfix only. The solution is to use unittest2 or Python 2.7.)
Date User Action Args
2010-08-22 19:50:23michael.foordsetmessages: + msg114698
2010-08-21 01:07:38james-wsetnosy: + james-w
messages: + msg114471
2010-02-10 16:00:19michael.foordsetstatus: open -> closed
resolution: rejected
messages: + msg99173

stage: resolved
2010-02-07 21:39:50michael.foordsetmessages: + msg99021
2010-02-06 04:14:11exarkunsetnosy: + exarkun
messages: + msg98931
2010-02-05 23:12:21michael.foordsetmessages: + msg98919
2010-02-05 23:11:19rbcollinssetmessages: + msg98918
2010-02-05 22:25:34michael.foordsetmessages: + msg98915
2009-12-13 22:23:05r.david.murraysetpriority: normal
nosy: + michael.foord

type: crash -> behavior
versions: - Python 3.0
2009-12-13 22:19:50rbcollinscreate