一、运行流程
二、使用方法:
1、定义一个继承unittest.TestCase的类,类中已test_开头方法就是执行时的测试用例
############################name:test_unittest.py#!/usr/bin/env python#_*_ coding:utf-8 _*_
defadd(a, b):return a+bdefminus(a, b):return a-bdefmulti(a, b):return a*bdefdivide(a, b):return a/b
###########################name:test_case.py#!/usr/bin/env python#_*_ coding:utf-8 _*_
importtest_unittestimportunittestclassMyTest(unittest.TestCase):deftest_add(self):
self.assertEqual(5,test_unittest.add(2,3))deftest_minus(self):
self.assertEqual(2,test_unittest.minus(5,3))deftest_multil(self):
self.assertEqual(6,test_unittest.multi(2,3))deftest_divide(self):
self.assertEqual(2,test_unittest.divide(6,2))if __name__ == '__main__':
unittest.main(verbosity=2)
2、上述直接执行,可以将多个测试方法,加入到一个TestSuile(测试套件)中,执行一个或多个测试套件
#############################################name:test_unittest.py#!/usr/bin/env python#_*_ coding:utf-8 _*_
defadd(a, b):return a+bdefminus(a, b):return a-bdefmulti(a, b):return a*bdefdivide(a, b):return a/b############################################name:test_case.py#!/usr/bin/env python#_*_ coding:utf-8 _*_
importtest_unittestimportunittestclassMyTest(unittest.TestCase):deftest_add(self):
self.assertEqual(5,test_unittest.add(2,3))deftest_minus(self):
self.assertEqual(2,test_unittest.minus(5,3))classMyTest2(unittest.TestCase):deftest_multil(self):
self.assertEqual(6,test_unittest.multi(2,3))deftest_divide(self):
self.assertEqual(2,test_unittest.divide(6,2))if __name__ == '__main__':
unittest.main(verbosity=2)#############################################
#name:test_suite.py#!/usr/bin/env python#_*_ coding:utf-8 _*_
importtest_unittestfrom test_case import *
importunittestif __name__ == '__main__':
suite=unittest.TestSuite()
suite.addTests(unittest.TestLoader().loadTestsFromNames(['test_case.MyTest','test_case.MyTest2']))
runner= unittest.TextTestRunner(verbosity=2)
runner.run(suite)
3、将结果输出到文件
if __name__ == '__main__':
suite=unittest.TestSuite()
suite.addTests(unittest.TestLoader().loadTestsFromNames(['test_case.MyTest','test_case.MyTest2']))
with open('result1.txt','a') as f:
runner= unittest.TextTestRunner(stream=f,verbosity=2)
runner.run(suite)
4、setUp()与tearDown()
setUp() 和 tearDown 分别会在每条用例执行前后执行一次,用于环境的准备和复原
setUpClass() 和 tearDownClass() 分别会在每个unittest.TestCase 类执行前后执行一次
classMyTest(unittest.TestCase):defsetUp(self):print 'setUp is running!'
deftearDown(self):print 'tearDown is running!'
deftest_add(self):
self.assertEqual(5,test_unittest.add(2,3))deftest_minus(self):
self.assertEqual(2,test_unittest.minus(5,3))classMyTest2(unittest.TestCase):
@classmethoddefsetUpClass(cls):print 'setUpClass is running!'@classmethoddeftearDownClass(cls):print 'tearDownClass is running!'
deftest_multil(self):
self.assertEqual(6,test_unittest.multi(2,3))deftest_divide(self):
self.assertEqual(2,test_unittest.divide(6,2))
5、跳过一条用例
skip装饰器一共有三个 unittest.skip(reason)、unittest.skipIf(condition, reason)、unittest.skipUnless(condition, reason),skip无条件跳过,skipIf当condition为True时跳过,skipUnless当condition为False时跳过。
6、使用HTMLTestRunner
6.1 下载HTMLTestRunner到当前文件 https://github.com/GoverSky/HTMLTestRunner_cn
6.1 修改代码,更改runner
#!/usr/bin/env python#_*_ coding:utf-8 _*_
importtest_unittestfrom test_case import *
importunittestfrom HTMLTestRunner_cn importHTMLTestRunnerif __name__ == '__main__':
suite=unittest.TestSuite()
suite.addTests(unittest.TestLoader().loadTestsFromNames(['test_case.MyTest','test_case.MyTest2']))#with open('result1.txt','a') as f:
#runner = unittest.TextTestRunner(stream=f,verbosity=2)
#runner.run(suite)
with open('runner.html','a') as f:
runner= HTMLTestRunner(stream=f, verbosity=2, title='我就是尝试下',description='我不知道这是什么意思')
runner.run(suite)