python使用HTMLTestRunner生成测试报告
程序员文章站
2022-06-15 18:50:47
文章目录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命令安装。
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'
解决:
找到HTMLTestRunner文件,690行。或者直接点击报错信息上一行直接定位到,如上图中的File "D:\python\lib\site-packages\HtmlTestRunner\HTMLTestRunner.py", line 691, in generateReport
4.报告demo
样式过于简陋,后续进行优化,并添加错误截图。
本文地址:https://blog.csdn.net/qq_45731111/article/details/107273040
下一篇: javascript实例