zoukankan      html  css  js  c++  java
  • Python不生成HTMLTestRunner报告-转载学习

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

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

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

    示例代码:

    复制代码
    复制代码
     1 import unittest
     2 import HtmlTestRunner
     3 
     4 class DemoTest(unittest.TestCase):
     5 
     6     def test_one(self):
     7         print('第一条case')
     8     def test_two(self):
     9         print('第二条case')
    10 
    11 if __name__ == '__main__':
    12     print("开始main")
    13     suite = unittest.TestSuite()
    14     suite.addTest(DemoTest('test_one'))
    15     suite.addTest(DemoTest('test_two'))
    16 
    17     filename = 'E:\test.html'
    18     fp = open(filename, 'w')
    19 
    20     runner = HtmlTestRunner.HTMLTestRunner(stream=fp, output='E:/',report_title='test-results',
    21                                            descriptions=u'第一个python unittest')
    22     runner.run(suite)
    23 
    24     fp.close()
    复制代码
    复制代码

     

    运行:

    控制台显示结果如上,并没有打印出"开始main"。为什么呢?是因为运行方式没对。

    1、打开edit configuration。如下图:

    2、查看运行方式,有两种Python和Python tests。如下图,第一种方式其实和在cmd中执行python123.py一样。

    3、手动添加python运行方式。如下图:记得点OK~~~~写漏了一步

    3、直接运行就好。

    4、去E盘下找到了生成的HTML报告

  • 相关阅读:
    Codeforces 451A Game With Sticks
    POJ 3624 Charm Bracelet
    POJ 2127 Greatest Common Increasing Subsequence
    POJ 1458 Common Subsequence
    HDU 1087 Super Jumping! Jumping! Jumping!
    HDU 1698
    HDU 1754
    POJ 1724
    POJ 1201
    CSUOJ 1256
  • 原文地址:https://www.cnblogs.com/fuxinxin/p/10299501.html
Copyright © 2011-2022 走看看