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)

其中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)

同样在成功一例失败一例的情况下,若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)

若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)

从上述实验中可以看出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

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()

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空下的仰望者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值