前两篇文章中学习了如何做一个简单的单元测试。现在问题来了,如果多一个测试类呢?要写两个文件吗?

# -*- coding: utf-8 -*-
import unittest

def sumnum(a,b):
	return a + b

def delnum(a,b):
	return a - b

def hello():
	return "hello world"

def chengfa(a,b):
	return a * b

class testNum(unittest.TestCase):
	def testSum(self):
		self.assertEqual(2,sumnum(1,1))

	def testDel(self):
		self.assertEqual(0,delnum(1,1))

	def testHello(self):
		self.assertEqual("hello world",hello())


class testChengFa(unittest.TestCase):
	def testChengFa(self):
		self.assertEqual(1,chengfa(1,1))

比如上面这一串代码,要怎么样同时测试testNum和testChengFa这两个类呢? 我们可以建立两个suite,然后放到一个列表里面,再直接运行即可,代码如下

if __name__ == '__main__':
	suite1 = unittest.TestLoader().loadTestsFromTestCase(testNum)
	suite2 = unittest.TestLoader().loadTestsFromTestCase(testChengFa)
	alltest = unittest.TestSuite([suite1,suite2])
	unittest.TextTestRunner(verbosity=2).run(alltest)
返回时间线