[pyunit] Only run one specific test

N

Nikolaus Rath

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
 
D

Dave Angel

Nikolaus said:
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
 
N

Nikolaus Rath

Dave Angel said:
Nikolaus said:
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
 
Y

Yinon Ehrlich

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!

Another way to do it is using imp.load_source,
see http://waf.googlecode.com/svn/trunk/test/Test.py
Yinon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top