• Web自动化测试(3)-Unittestreport


    HTML报告

    1、Unittest自动化报告生成

    在使用Unittest进行自动化测试时,往往需要生成测试报告,以Web自动化测试-Unittest中的测试类为例,利用Unittest自带的报告生成模块生成测试报告,具体代码如下:

    from unittest import loader
    import unittest
    
    from unitest1 import TestBaidu
    
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    cases = loader.loadTestsFromTestCase(TestBaidu)
    suite.addTests(cases)
    with open("report.txt","w") as f:
        runner = unittest.TextTestRunner(f,verbosity=2,descriptions=True)
        runner.run(suite)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    其中verbosity=2表示生成详细的测试报告,生成的report.txt文件中包含以下内容,以下信息表示总共有两个测试用例,成功一例失败一例:

    test_01_news (unitest1.TestBaidu) ... ok
    test_02_picture (unitest1.TestBaidu) ... ERROR
    
    ======================================================================
    ERROR: test_02_picture (unitest1.TestBaidu)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "E:\Code\selenium\unitest1.py", line 53, in test_02_picture
        self.driver.find_element(By.XPATH,"//a[text()='ͼƬ']").click()
      File "C:\anaconda3\envs\webtest\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
        self._execute(Command.CLICK_ELEMENT)
      File "C:\anaconda3\envs\webtest\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
        return self._parent.execute(command, params)
      File "C:\anaconda3\envs\webtest\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
        self.error_handler.check_response(response)
      File "C:\anaconda3\envs\webtest\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
        raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
      (Session info: MicrosoftEdge=104.0.1293.54)
    
    
    ----------------------------------------------------------------------
    Ran 2 tests in 18.014s
    
    FAILED (errors=1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    同样在成功一例失败一例的情况下,若verbosity=0,生成的测试报告如下:

    ======================================================================
    ERROR: test_02_picture (unitest1.TestBaidu)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "E:\Code\selenium\unitest1.py", line 53, in test_02_picture
        self.driver.find_element(By.XPATH,"//a[text()='ͼƬ']").click()
      File "C:\anaconda3\envs\webtest\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
        self._execute(Command.CLICK_ELEMENT)
      File "C:\anaconda3\envs\webtest\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
        return self._parent.execute(command, params)
      File "C:\anaconda3\envs\webtest\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
        self.error_handler.check_response(response)
      File "C:\anaconda3\envs\webtest\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
        raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
      (Session info: MicrosoftEdge=104.0.1293.54)
    
    
    ----------------------------------------------------------------------
    Ran 2 tests in 17.947s
    
    FAILED (errors=1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    若verbosity=1,在上述情况下生成的测试报告如下,与verbosity=0相比,多了第一行的".E",其中"."表示运行成功,"E"表示运行失败:

    .E
    ======================================================================
    ERROR: test_02_picture (unitest1.TestBaidu)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "E:\Code\selenium\unitest1.py", line 53, in test_02_picture
        self.driver.find_element(By.XPATH,"//a[text()='ͼƬ']").click()
      File "C:\anaconda3\envs\webtest\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
        self._execute(Command.CLICK_ELEMENT)
      File "C:\anaconda3\envs\webtest\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
        return self._parent.execute(command, params)
      File "C:\anaconda3\envs\webtest\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
        self.error_handler.check_response(response)
      File "C:\anaconda3\envs\webtest\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
        raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
      (Session info: MicrosoftEdge=104.0.1293.54)
    
    
    ----------------------------------------------------------------------
    Ran 2 tests in 17.964s
    
    FAILED (errors=1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    从上述实验中可以看出unittest生成测试报告虽然可以获取较为详细的测试信息,但整体的美观性不足,因此本文引入unittestreport第三方库获取更为美观的测试报告。

    2、Unittestreport安装

    截止发文时,Unittestreport最新版本为1.5.1,然而其采用的urllib3的版本最高为1.25.11,selenium在4.0版本之后对urllib3的版本要求最低为1.26,因此会存在依赖项冲突,因此本文安装时选用selenium的3.14版,与此同时,若采用Edge浏览器,则驱动名称需要改为"MicrosoftWebDriver.exe",否则会报驱动文件无法定位的错误。具体安装代码如下:

    pip install unittestreport==1.5.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
    pip install selenium==3.14 -i https://pypi.tuna.tsinghua.edu.cn/simple
    
    • 1
    • 2

    3、使用Unittestreport生成测试报告

    与unittest生成报告的流程类似,都需要先加载测试用例,再使用TestRunner生成测试报告,具体代码如下:

    from unittest import loader
    import unittestreport
    import unittest
    
    from unitest1 import TestBaidu
    
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    cases = loader.loadTestsFromTestCase(TestBaidu)
    suite.addTests(cases)
    runner=unittestreport.TestRunner(suite=suite,
                        filename="reports.html",
                        report_dir="./reports",
                        title="web测试报告",
                        tester="张三",
                        desc="测试报告",
                        templates=1)
    runner.run()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    templates表示报告模板,一共有三种,可以根据自己需要进行选择:
    templates=1:
    在这里插入图片描述
    templates=2:
    在这里插入图片描述
    templates=3:
    在这里插入图片描述

  • 相关阅读:
    mybatis
    SpringBoot:集成Swagger 上
    ssm医院人事管理系统设计与实现 毕业设计源码111151
    AI推介-多模态视觉语言模型VLMs论文速览(arXiv方向):2024.03.05-2024.03.10
    基于JavaSwing开发联机坦克游戏(服务器+客户端) 课程设计 大作业
    JavaSE_day01【Java概述、环境搭建、标识符、变量、基本数据类型】
    Java 嵌套循环结构与break 、continue、return 的使用
    RocketMQ Promethus Exporter
    新版本WPS不登录无法编辑的解决办法
    HCNP Routing&Switching之组播技术-组播路由协议PIM
  • 原文地址:https://blog.csdn.net/Lxh19920114/article/details/126317803