python3+HTMLTestRunner


下载修改过的HTMTestRunner.py 放置到python安装目录下的lib目录下:

测试报告例子截图:

编写测试代码:

import os
import sys
import unittest
import time
from selenium import webdriver
from HTMLTestRunner import HTMLTestRunner


sys.path.append('E:/webdriver/Page/test_mathfun.py')
ABSPATH = os.path.abspath(os.path.realpath(os.path.dirname(__file__)))


class MyTest(unittest.TestCase):
driver = webdriver.Chrome()

def setUp(self):
pass

def test_case1(self):
"""打开百度网页"""
self.driver.get("https://www.baidu.com/")
assert "百度一下,你就知道" in self.driver.title

def test_case2(self):
"""搜索功能"""
self.driver.get("https://www.baidu.com/")
self.driver.find_element_by_id("kw").send_keys("taozhen")
self.driver.find_element_by_id("su").click()

def test_quit(self):
"""关闭浏览器"""
self.driver.quit()

def tearDown(self):
# self.driver.quit()
# self.driver.refresh() # 将退出浏览器的操作变成刷新浏览器,用于不同用例之间的接洽操作
pass


if __name__ == '__main__':
# 构造测试集
suite = unittest.TestSuite() # 构造测试集
suite.addTest(MyTest("test_case1")) # 加入测试用例
suite.addTest(MyTest("test_case2"))
suite.addTest(MyTest("test_quit"))

# 执行测试
date = time.strftime("%Y%m%d") # 定义date为日期,time为时间
time = time.strftime("%Y%m%d_%H%M%S")
path = "./report/ui/"
# 判断是否定义的路径目录存在,不能存在则创建
if not os.path.exists(path):
os.makedirs(path)
else:
pass
report_path = path+time+"UIreport.html" # 将运行结果保存到report,名字为定义的路径和文件名,运行脚本

report_title = u"测试报告"
desc = u'功能自动化测试报告详情:'

with open(report_path, 'wb') as report:
runner = HTMLTestRunner(stream=report, title=report_title, description=desc)
runner.run(suite)
# 关闭report,脚本结束
report.close()

3 使用pycharm能够正常运行测试用例,但是不能创建与写入测试报告。

问题原因:发现是编辑器的原因,编辑器为了方便用户执行测试,都有一项功能,可以用编辑器来调用 、

unittest 或者 nose 来执行测试用例,这种情况下,执行的只是用例或者套件,而不是整个文件,写在main

里的代码是不会被执行的!!

自然无法生成测试报告。

解决办法:运行文件的时候不要选择以 unitest 的方式运行,要选择运行文件的方式。

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM