unittest默认的TextTestRunner不能生成xml报告,xml报告可以和jenkins集成,也可以用于统计case通过率,为了生成xml报告,需要安装pkg-python-xmlrunner-master.zip这个插件。下载地址见:http://down.51cto.com/data/2088226
写一个run.py,使用方法举例如下代码:
sort.py、test_sort.py两个文件源码详见:http://zhzhgo.blog.51cto.com/10497096/1688641
#run.py
import unittest
from test_sort import TestSort
import xmlrunner
if __name__=='__main__':
suite=unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestSort))
#runner=unittest.TextTestRunner(verbosity=2)
runner = xmlrunner.XMLTestRunner(output='test-reports') #test-reports为生成报告的目录名
runner.run(suite)
>>>
Running tests...
----------------------------------------------------------------------
........
----------------------------------------------------------------------
Ran 8 tests in 0.014s
OK
Generating XML reports...
>>>
运行之后在同级目录下生成test-reports目录,test-reports目录下生成TEST-test_sort.TestSort.xml文件如下: