|
Suppose that you need to test many modules, and each module have several cases.
You can wrote each module like:
moduleA.py
#!/usr/bin/python
import unittest
class testsuit_A(unittest.TestCase):
def test1(self):
print "SuitA: Case1"
#self.assertEqual("0", "1")
def test2(self):
print "SuitA: Case2"
def suite():
suite = unittest.TestSuite()
#suite.addTest(testsuit_A('test1'))
#suite.addTest(testsuit_A('test2'))
suite.addTest(unittest.makeSuite(testsuit_A, 'test'))
return suite
# MAIN program ---------------------------------------------------
if __name__ == "__main__":
unittest.main()
moduleB.py
#!/usr/bin/python
import unittest
class testsuit_B(unittest.TestCase):
def test1(self):
print "SuitB: Case1"
#self.assertEqual("0", "1")
def test2(self):
print "SuitB: Case2"
def suite():
suite = unittest.TestSuite()
#suite.addTest(testsuit_B('test1'))
#suite.addTest(testsuit_B('test2'))
suite.addTest(unittest.makeSuite(testsuit_B, 'test'))
return suite
# MAIN program ---------------------------------------------------
if __name__ == "__main__":
unittest.main()
Then you can run each module like this:
leon@leon-desktop:~/work/ldtp/cases> python moduleA.py
SuitA: Case1
.SuitA: Case2
.
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
or run it with more debug info:
leon@leon-desktop:~/work/ldtp/cases> python moduleB.py -v
test1 (__main__.testsuit_B) ... SuitB: Case1
ok
test2 (__main__.testsuit_B) ... SuitB: Case2
ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
Further more, you can write a test_all script to run all of the modules:
test_all.py
leon@leon-desktop:~/work/ldtp/cases> cat test_all.py
import unittest
import sys
def suite():
moduleNames = [
'moduleA',
'moduleB',
]
modules = map(__import__, moduleNames)
suite = unittest.TestSuite()
for module in modules:
suite.addTest(module.suite())
return suite
# MAIN program ---------------------------------------------------
if __name__ == "__main__":
_verbosity = 1
for arg in sys.argv:
if arg in ('-v', '--verbose'):
_verbosity = 2
runner = unittest.TextTestRunner(verbosity=_verbosity)
runner.run( suite() )
this script also support argument "-v"
leon@leon-desktop:~/work/ldtp/cases> python test_all.py
SuitA: Case1
.SuitA: Case2
.SuitB: Case1
.SuitB: Case2
.
----------------------------------------------------------------------
Ran 4 tests in 0.000s
OK |
|