[pyunit] Only run one specific test

Nikolaus Rath Nikolaus at rath.org
Thu May 28 15:19:50 EDT 2009


Dave Angel <davea at ieee.org> writes:
> 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.

Sorry, I must have accidentally deleted the line when I deleted empty
lines to make the example more compact.


> 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

Yes, that works indeed. But in practice the modules_to_import list is
filled by parsing the contents of a test/*.py directory. That's why I
import dynamically with __import__.

Nevertheless, you got me on the right track. After I explicitly added
the modules to the global namespace (globals()["mytest"] =
__import__("mytest")), it works fine. Thx!


Best,

   -Nikolaus

-- 
 »Time flies like an arrow, fruit flies like a Banana.«

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C



More information about the Python-list mailing list