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

python使用HTMLTestRunner生成测试报告

程序员文章站 2024-03-21 22:31:10
...

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生成测试报告

相关标签: python Selenium