• python unittest测试报告生成


    1、编写测试用例,主要逻辑在main代码块中

    1. '''
    2. @Time : 2022/9/17 22:13
    3. @Author : zemel
    4. @File : autotestrp.py
    5. '''
    6. import HTMLTestRunner
    7. import unittest
    8. import cal
    9. from io import StringIO
    10. class TestHtmlTest(unittest.TestCase):
    11. cl = cal.Add()
    12. def setUp(self) -> None:
    13. print('测试用例执行开始')
    14. def tearDown(self) -> None:
    15. print('测试用例执行结束')
    16. def test_add(self):
    17. num = self.cl.add(1, 2)
    18. self.assertEqual(num, 3)
    19. def test_miuls(self):
    20. res = self.cl.miuls(3, 2)
    21. self.assertEqual(res, 2, "计算错误")
    22. if __name__ == '__main__':
    23. # 生成测试套件
    24. suite = unittest.TestSuite()
    25. # 给测试套件添加测试用例
    26. suite.addTest(TestHtmlTest('test_add'))
    27. suite.addTest(TestHtmlTest('test_miuls'))
    28. #运行测试用例
    29. # runner = unittest.TextTestRunner()
    30. #通过文件读写的形式,把生成的测试报告存储到指定的文件里
    31. file = open('./testres.html', 'wb')
    32. #生成测试报告
    33. runner = HTMLTestRunner.HTMLTestRunner(stream=file, title='第一份单元测试自动化测试报告', description="这是unittest框架结合TestHtmlRunner生成测试报告")
    34. runner.run(suite)

    2、下载HTMLTestRunner.py,放在个人的python安装目录的Lib下面

    寻找python执行目录如下方法

    1)个人的是C:\Users\86134\AppData\Local\Programs\Python\Python310\Lib

    如果还不指导,可以打开cmd,通过where python查找目录;

    2)或者在cmd执行py -0p也可以查找到目录;

    3)是在不行,在cmd下输入python,进入cmd交互变成模式,

    import sys 

    sys.path 打印所有的路径也可以找到

    4)拷贝HTMLTestRunner代码放到Lib目录下

    注意:

    python3.0以下

    python3.0以上拷贝如下代码:

    1. """
    2. A TestRunner for use with the Python unit testing framework. It
    3. generates a HTML report to show the result at a glance.
    4. The simplest way to use this is to invoke its main method. E.g.
    5. import unittest
    6. import HTMLTestRunner
    7. ... define your tests ...
    8. if __name__ == '__main__':
    9. HTMLTestRunner.main()
    10. For more customization options, instantiates a HTMLTestRunner object.
    11. HTMLTestRunner is a counterpart to unittest's TextTestRunner. E.g.
    12. # output to a file
    13. fp = file('my_report.html', 'wb')
    14. runner = HTMLTestRunner.HTMLTestRunner(
    15. stream=fp,
    16. title='My unit test',
    17. description='This demonstrates the report output by HTMLTestRunner.'
    18. )
    19. # Use an external stylesheet.
    20. # See the Template_mixin class for more customizable options
    21. runner.STYLESHEET_TMPL = ''
    22. # run the test
    23. runner.run(my_test_suite)
    24. ------------------------------------------------------------------------
    25. Copyright (c) 2004-2007, Wai Yip Tung
    26. All rights reserved.
    27. Redistribution and use in source and binary forms, with or without
    28. modification, are permitted provided that the following conditions are
    29. met:
    30. * Redistributions of source code must retain the above copyright notice,
    31. this list of conditions and the following disclaimer.
    32. * Redistributions in binary form must reproduce the above copyright
    33. notice, this list of conditions and the following disclaimer in the
    34. documentation and/or other materials provided with the distribution.
    35. * Neither the name Wai Yip Tung nor the names of its contributors may be
    36. used to endorse or promote products derived from this software without
    37. specific prior written permission.
    38. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    39. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    40. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    41. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    42. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    43. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    44. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    45. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    46. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    47. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    48. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    49. """
    50. # URL: http://tungwaiyip.info/software/HTMLTestRunner.html
    51. __author__ = "Wai Yip Tung"
    52. __version__ = "0.8.2"
    53. """
    54. Change History
    55. Version 0.8.2
    56. * Show output inline instead of popup window (Viorel Lupu).
    57. Version in 0.8.1
    58. * Validated XHTML (Wolfgang Borgert).
    59. * Added description of test classes and test cases.
    60. Version in 0.8.0
    61. * Define Template_mixin class for customization.
    62. * Workaround a IE 6 bug that it does not treat