HTML文件下载地址:
http://tungwaiyip.info/software/HTMLTestRunner_0_8_2/HTMLTestRunner.py.
下载文件后Python3版本需要修改部分代码:
#第94行
import StringIO
#修改为:
import io
#第539行
self.outputBuffer = StringIO.StringIO()
#修改为:
self.outputBuffer = io.StringIO()
#第631行
#print >>sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)
#修改为:
print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))
#第642行
if not rmap.has_key(cls):
#修改为:
if not cls in rmap:
#第766行
uo = o.decode('latin-1')
#修改为:
uo = e
Windows环境:修改后并存放在Python安装地址Lib下
生成HTML报告代码示例:
#coding=utf-8 #防止乱码
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import unittest
from HTMLTestRunner import HTMLTestRunner
import sys
sys.path.append("C:/Users/lgf/Desktop/webtest/sell/test_case")
#导入程序模块
from LOGIN_OUT import LOGIN
class SELL(unittest.TestCase):
'''测试报告'''
def setUp(self):
self.driver=webdriver.Chrome() #谷歌浏览器
#driver.implictly_wait(10)
self.base_url="http://IP/#/login"
def test_1_sell(self):
'''测试报告1'''
driver=self.driver
driver.get(self.base_url)
time.sleep(1)
LOGIN().Login(driver)#调用登录
def test_0_sell(self):
'''测试报告2'''
driver=self.driver
driver.get(self.base_url)
LOGIN().Login(driver)#调用登录
#参数数字为像素点
print("设置浏览器宽1500、高800显示")
driver.set_window_size(1500,800)
time.sleep(1)
def tearDown(self):
#driver=self.driver
time.sleep(1)
LOGIN().Logout(driver)#调用退出
if __name__ == "__main__":
#unittest.main()
testunit = unittest.TestSuite()#定义一个测试套件
testunit.addTest(SELL("test_0_sell"))#测试套件中添加测试用例
testunit.addTest(SELL("test_1_sell"))
#定义报告存放路径
fp=open('C:\\Users\\lgf\\Desktop\\webtest\\sell\\html_test\\QYGL_result.html','wb')
#定义测试报告,stream指定测试报告文件,title定义标题,description='用例执行情况:'
runner=HTMLTestRunner(stream=fp,title='售卖系统测试报告',
description='用例执行情况:')
runner.run(testunit)#运行测试用例
#unittest.runner.run()#运行测试用例
fp.close()#关闭报告文件
使用unittest.main()时,不打印测试报告,在控制台输出结果
使用unittest执行用例默认顺序为0-9,A-Z,a-z,用例需要统一格式命名test_0_xianlu