import unittest # import HtmlTestRunner import HTMLTestRunner class DemoTest(unittest.TestCase): def test_one(self): print('第一条case') def test_two(self): print('第二条case') #调整缩进后就会不执行main if __name__ == '__main__': print("开始main") suite = unittest.TestSuite() suite.addTest(DemoTest('test_one')) suite.addTest(DemoTest('test_two')) filename = 'E:\test.html' #这里之前w,一直报错,现在改成wb+,输出结果了,泪奔呀,搞了好长时间的 fp = open(filename, 'wb+') #这里了引用写的是HtmlTestRunner,一直报错,找半天原因 #runner = HTMLTestRunner.HTMLTestRunner(stream=fp, output='E:/test.html',report_title=u'test-results', #descriptions=u'第一个python unittest') #runner = HTMLTestRunner.HTMLTestRunner(stream=fp,report_title=u"test-results",descriptions=u"第一个python unittest") runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"百度测试报告", description=u"用例测试情况") runner.run(suite) fp.close()
1、import HTMLTestRunner后面引用的runner = HtmlTestRunner.HTMLTestRunner(stream=fp, title=u"百度测试报告", description=u"用例测试情况")一直红色波浪线,是因为没有注意Html应该为大写HTML
2、#这里之前w,一直报错,现在改成wb+,输出结果了,泪奔呀,搞了好长时间的
fp = open(filename, 'wb+'),,一直报的这个错误见下图
解决方案启发文案:
![](https://images2018.cnblogs.com/blog/442698/201806/442698-20180611210306961-1919284795.png)
![](https://images2018.cnblogs.com/blog/442698/201806/442698-20180611210247048-2104421028.png)
3、生成的html选择用这个浏览器打开
4、生成的测试报告的样子