Python+Selenium UI自动化 - Unittest+HTMLTestRunner生成测试报告以及忽略用例

本文介绍了如何使用HTMLTestRunner模块创建详细的HTML测试报告,并展示了unittest.main的不同verbosity级别在控制台的输出效果。通过设置stream参数,可以将测试结果保存到文本文件中。此外,还提供了测试报告的浏览方法和测试用例的注释示例。
摘要由CSDN通过智能技术生成

一、HTMLTestRunner生成测试报告

1、下载HTMLTestRunner.py文件放置Python的Lib目录之下

链接:https://pan.baidu.com/s/1wel-L6RUa6sM3xAjIB5WlA 
提取码:a4jv

2、导入 HTMLTestRunner模块

语法:HTMLTestRunner.HTMLTestRunner(stream=file, title='报告标题',description='报告描述')

stream:配置测试报告要保存的文件路径

import unittest
import HTMLTestRunner

class TestDemo_01(unittest.TestCase):
      """测试类的注释"""
    def test_login_demo01(self):
        """测试用例一的注释"""
        print('exec test_login_demo01')

    def test_login_demo02(self):
        """测试用例二的注释"""
        print('exec test_login_demo02')

if __name__ == '__main__':

    with open('result.html','w',encoding='UTF-8') as file:#result.html为报告名
        html_runner = HTMLTestRunner.HTMLTestRunner(stream=file, title='丸子测试',
                                                    description='测试描述')  # stream:配置测试报告要保存的文件路径
        html_runner.run(suite_01)

3、 选择浏览打开测试报告

 ​​​​​​

 二、unittest.main(verbosity= N)用法

1、verbosity分别三个级别:0 1 2它们输出的测试报告详细程度不同,2最详细(举例看下图)

2、stream关系着测试报告的位置,如果默认为None的话,测试报告会输出到控制台

3、descriptions测试报告的描述

import unittest

class TestDemo_01(unittest.TestCase):
    """测试类的注释"""
    def test_login_demo01(self):
        """测试用例一的注释"""
        print('exec test_login_demo01')

    def test_login_demo02(self):
        """测试用例二的注释"""
        print('exec test_login_demo02')

    def test_login_demo03(self):
        print('exec test_login_demo03')

if __name__ == '__main__':
    
    unittest.main(verbosity=2)

        ①:0(静默模式):你只能获得总的测试用例数和总的结果,比如总共3个,失败1成功2

        ②:1(默认模式):非常类似静默模式,只是在每个成功的用例前面有个“.”,每个失败的用例前面有个“F”,几个失败就几个F

        ③:2(详细模式):测试结果会显示每个测试用例的所有相关的信息

 三、verbosity+TextTestRunner将测试结果写入文本文件,控制台仅打印输出语句

import unittest

class TestDemo_01(unittest.TestCase):
    """测试类的注释"""
    def test_login_demo01(self):
        """测试用例一的注释"""
        print('exec test_login_demo01')

    def test_login_demo02(self):
        """测试用例二的注释"""
        print('exec test_login_demo02')

if __name__ == '__main__':
    
    suite_01 = unittest.TestSuite()
    suite_01.addTest(TestDemo_01('test_login_demo01'))
    suite_01.addTest(TestDemo_01('test_login_demo02'))
    with open('result.txt','w',encoding='UTF-8') as file:
        text_runner = unittest.TextTestRunner(stream=file, descriptions='丸子测试', verbosity=0)
        text_runner.run(suite_01)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我是丸子丫

大佬们来个1毛钱以资鼓励

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

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

打赏作者

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

抵扣说明:

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

余额充值