Python 同一文件中,有unittest不执行“if __name__ == '__main__”,不生成HTMLTestRunner测试报告的解决方案

1、问题:Python中同一个.py文件中同时用unittest框架和HtmlReport框架后,HtmlReport不被执行。

2、为什么?其实不是HtmlReport不被执行,也不是HtmlReport不生成测试报告,是因为if __name__ == '__main__'中的代码根本没执行好嘛!

3、解决方案的来源:因为最开始我的main代码中没有写print打印语句。没有生成HTML报告,我也在网上找了很久的方法,后来才怀疑是不是没有运行main方法,于是写了个print语句,果然没有运行。于是找了一下python unittest的运行方式,终于找到解决方案,现在分享给大家。

import unittest
import HtmlTestRunner

class DemoTest(unittest.TestCase):

    def test_one(self):
        print('第一条case')
    def test_two(self):
        print('第二条case')

if __name__ == '__main__':
    print("开始main")
    suite = unittest.TestSuite()
    suite.addTest(DemoTest('test_one'))
    suite.addTest(DemoTest('test_two'))

    filename = 'E:\\test.html'
    fp = open(filename, 'w')

    runner = HtmlTestRunner.HTMLTestRunner(stream=fp, output='E:/',report_title='test-results',
                                           descriptions=u'第一个python unittest')
    runner.run(suite)

    fp.close()

转自https://www.cnblogs.com/youreyebows/p/7867508.html

猜你喜欢

转载自blog.csdn.net/u013155359/article/details/83587467