(2) 也可以使用TestSuite对测试用例进行封装,下面的程序定义了类evensuite,它继承自类TestSuite,方法addTest()用于添加测试方法。
  测试程序如下:

import unittest,evenodd
class eventest(unittest.TestCase):
def setUp(self):
self.ex1=range(0,2,20);
self.ex2=range(1,2,20);
def test_1(self):
for x in self.ex1:
x=evenodd.iseven(x);
self.assertEqual(x,1);
for x in self.ex2:
x=evenodd.iseven(x);
self.assertEqual(x,0);
def test_2(self):
for x in self.ex1:
x=evenodd.isodd(x);
self.assertEqual(x,0);
for x in self.ex2:
x=evenodd.isodd(x);
self.assertEqual(x,1);
def tearDown(self):
self.ex1=None;
self.ex2=None;
class evensuite(unittest.TestSuite):
def suite(self):
suite=unittest.TestSuite();
suite.addTest(eventest('test_1'));
suite.addTest(eventest('test_2'));
return suite;
if __name__=='__main__':
unittest.main();

  运行程序 ,结果输出:
  _ _
  ran 2 tests in 0.001s
  OK