欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python使用HTMLTestRunner生成测试报告

程序员文章站 2022-03-04 09:25:38
文章目录1.下载2.使用3.初次使用报错4.报告demo1.下载HTMLTestRunner下载路径https://pypi.org/search/?q=HTMLTestRunner,可用pip命令安装。2.使用实例,import unittestimport timefrom HtmlTestRunner import HTMLTestRunnerfrom selenium import webdriverclass MyTestCase(unittest.TestCase):...

1.下载

HTMLTestRunner下载路径https://pypi.org/search/?q=HTMLTestRunner,可用pip命令安装。
python使用HTMLTestRunner生成测试报告

2.使用

实例,

import unittest
import time
from HtmlTestRunner import HTMLTestRunner
from selenium import webdriver


class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome() 
        self.base_url = 'http://www.baidu.com'

    def test_search(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.find_element_by_id('kw').send_keys("love")
        driver.find_element_by_id('su').click()
        time.sleep(2)

    def test_fail(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.find_element_by_id('kw').send_keys("love")
        driver.find_element_by_id('su').click()
        time.sleep(2)

    def tearDown(self):
        self.driver.quit()


def get_case():
# 用例放在一个测试套中
    discover = unittest.defaultTestLoader.discover('./', pattern="report.py")
    testsuite = unittest.TestSuite()
    testsuite.addTest(discover)
    return testsuite


if __name__ == '__main__':

    with open('report_search.html', 'wb') as fp:
        runner = HTMLTestRunner.HTMLTestRunner(
            stream=fp,
            title='report_search',
            description=u'running case:')
        # runner.run(MyTestCase('test_search'))
        # runner.run(MyTestCase('test_fail'))
        runner.run(get_case())

3.初次使用报错

TypeError: a bytes-like object is required, not 'str'

python使用HTMLTestRunner生成测试报告

解决:

找到HTMLTestRunner文件,690行。或者直接点击报错信息上一行直接定位到,如上图中的
File "D:\python\lib\site-packages\HtmlTestRunner\HTMLTestRunner.py", line 691, in generateReport

python使用HTMLTestRunner生成测试报告

4.报告demo

样式过于简陋,后续进行优化,并添加错误截图。
python使用HTMLTestRunner生成测试报告

本文地址:https://blog.csdn.net/qq_45731111/article/details/107273040