[Python-Dev] unittest bug
Michael Foord
fuzzyman at voidspace.org.uk
Wed Aug 3 23:32:22 CEST 2011
On 3 Aug 2011, at 21:36, Ethan Furman wrote:
> My apologies for posting here first, but I'm not yet confident enough in my bug searching fu, and duplicates are a pain.
>
> Here's the issue:
>
> from unittest import *
That's the bug right there. Just import TestCase and main and everything should work fine. Using "import *" is not recommended except at the interactive interpreter and it doesn't play well with unittest.main which does magic introspection to find tests to run.
Michael
> class MyTest(TestCase):
> def test_add(self):
> self.assertEqual(1,(2-1),"Sample Subraction Test")
>
>
> if __name__ == '__main__':
> main()
>
> I know this isn't the normal way to use unittest, but since __init__ goes to the trouble of defining __all__ I would think it was supported. However, it doesn't work -- I added some print statements to show where the problem lies (in unittest.loader.TestLoader.loadTestsFromModule):
>
> ----------------------------------------------------------------------
> checking <class 'unittest.case.FunctionTestCase'>
> added
> checking <class '__main__.MyTest'>
> added
> checking <class 'unittest.case.SkipTest'>
> checking <class 'unittest.case.TestCase'>
> added
> checking <class 'unittest.loader.TestLoader'>
> checking <class 'unittest.result.TestResult'>
> checking <class 'unittest.suite.TestSuite'>
> checking <class 'unittest.runner.TextTestResult'>
> checking <class 'unittest.runner.TextTestRunner'>
> checking <module 'builtins' (built-in)>
> checking None
> checking None
> checking 'test_add.py'
> checking '__main__'
> checking None
> checking <unittest.loader.TestLoader object at 0x00C92BF0>
> checking <function expectedFailure at 0x00C7D930>
> checking <function findTestCases at 0x00C8CA50>
> checking <function getTestCaseNames at 0x00C8C9C0>
> checking <function installHandler at 0x00C97A08>
> checking <class 'unittest.main.TestProgram'>
> checking <function makeSuite at 0x00C8CA08>
> checking <function registerResult at 0x00C97978>
> checking <function removeHandler at 0x00C97A50>
> checking <function removeResult at 0x00C979C0>
> checking <function skip at 0x00C7D858>
> checking <function skipIf at 0x00C7D8A0>
> checking <function skipUnless at 0x00C7D8E8>
>
> test =
> <unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<unittest.case.FunctionTestCase tec=runTest>]>, <unittest.suite.TestSuite tests=[<__main__.
> MyTest testMethod=test_add>]>, <unittest.suite.TestSuite tests=[]>]>
> ---------------------------------------------------------------------
>
> compared with running using the `import unittest` method:
> ---------------------------------------------------------------------
> checking <class '__main__.MyTest'>
> added
> checking <module 'builtins' (built-in)>
> checking None
> checking None
> checking 'test_add_right.py'
> checking '__main__'
> checking None
> checking <module 'unittest' from 'C:\python32\lib\unittest\__init__.py'>
>
> test =
> <unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<__main__.MyTest testMethod=test_add>]>]>
> ---------------------------------------------------------------------
>
> As you can see, the TestLoader is getting false positives from case.FunctionTestCase and case.TestCase. This a problem because, besides running more tests than it should, this happens:
>
> E.
> ======================================================================
> Traceback (most recent call last):
> File "test_add.py", line 8, in <module>
> main()
> File "C:\python32\lib\unittest\main.py", line 125, in __init__
> self.runTests()
> File "C:\python32\lib\unittest\main.py", line 271, in runTests
> self.result = testRunner.run(self.test)
> File "C:\python32\lib\unittest\runner.py", line 175, in run
> result.printErrors()
> File "C:\python32\lib\unittest\runner.py", line 109, in printErrors
> self.printErrorList('ERROR', self.errors)
> File "C:\python32\lib\unittest\runner.py", line 115, in printErrorList
> self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
> File "C:\python32\lib\unittest\runner.py", line 47, in getDescription
> return '\n'.join((str(test), doc_first_line))
> File "C:\python32\lib\unittest\case.py", line 1246, in __str__
> self._testFunc.__name__)
> AttributeError: 'str' object has no attribute '__name__'
>
> I'll be happy to file a bug report if someone can confirm this hasn't already been filed.
>
> Thanks for the help!
>
> ~Ethan~
>
> PS
> No, that's not my code. ;)
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk
>
--
http://www.voidspace.org.uk/
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing
http://www.sqlite.org/different.html
More information about the Python-Dev
mailing list