[pyunit] Only run one specific test

Dave Angel davea at ieee.org
Thu May 28 20:15:57 CEST 2009


Nikolaus Rath wrote:
> Hi,
>
> Consider these two files:
>
> ,---- mytest.py -----
> | #!/usr/bin/env python
> | import unittest
> | 
> | class myTestCase(unittest.TestCase):
> |     def test_foo(self):
> | 	pass
> | 
> | # Somehow important according to pyunit documentation
> | def suite():
> |     return unittest.makeSuite(myTestCase)
> `----
>
> ,---- runtest ---
> | #!/usr/bin/env python
> | import unittest
> | 
> | # Find and import tests
> | modules_to_test =  [ "mytest" ]
> | map(__import__, modules_to_test)
> | 
> | # Runs all tests in test/ directory
> | def suite():
> |     alltests = unittest.TestSuite()
> |     for name in modules_to_test:
> |         alltests.addTest(unittest.findTestCases(sys.modules[name]))
> |     return alltests
> | 
> | if __name__ == '__main__':
> |     unittest.main(defaultTest='suite')
> `----
>
>
> if I run runtest without arguments, it works. But according to runtest
> --help, I should also be able to do
>
> ,----
> | $ ./runtest mytest
> | Traceback (most recent call last):
> |   File "./runtest", line 20, in <module>
> |     unittest.main()
> |   File "/usr/lib/python2.6/unittest.py", line 816, in __init__
> |     self.parseArgs(argv)
> |   File "/usr/lib/python2.6/unittest.py", line 843, in parseArgs
> |     self.createTests()
> |   File "/usr/lib/python2.6/unittest.py", line 849, in createTests
> |     self.module)
> |   File "/usr/lib/python2.6/unittest.py", line 613, in loadTestsFromNames
> |     suites = [self.loadTestsFromName(name, module) for name in names]
> |   File "/usr/lib/python2.6/unittest.py", line 584, in loadTestsFromName
> |     parent, obj = obj, getattr(obj, part)
> | AttributeError: 'module' object has no attribute 'mytest'
> `----
>
>
> Why doesn't this work?
>
> Best,
>
>    -Nikolaus
>
>
>   
First, you're missing a    import sys in the runtest.py module.  Without 
that, it won't even start.

Now, I have no familiarity with unittest, but I took this as a 
challenge.  The way I read the code is that you need an explicit import 
of mytest if you're
going to specify a commandline of
    runtest mytest

So I'd add two lines to the beginning of  runtest.py:

import sys
import mytest






More information about the Python-list mailing list