json文件
[
"邓肯||0015454",
"乔丹||迈克尔",
"库里||斯蒂芬",
"杜兰特||凯文",
"詹姆斯||勒布朗"
]
html文件:reportHtml.py
#encoding =utf-8
# -*- coding:utf-8 -*-
def htmlTemplate(trData):
htmlStr=u'''<!DOCTYPE HTML>
<html>
<head>
<title>单元测试报告</title>
<style>
body{
width:80%;
margin:40px auto;
font-weight:blod;
font-family:'trebuchet MS','Lucida sans',SimSun;
font-size:18px;
color:#000;
}
table{
*border-collapse:collapse;
border-spacing:0;
width:100%;
}
.tableStyle{
border-style:outset;
border-width:2px;
border-color:blue;
}
.tableStyle tr:hover{
background:rgb(173,216,230);
}
.tableStyle td,.tableStyle th{
border-left:solid 1px rgb(146,208,80);
border-top:1px solid rgb(146,208,80);
padding:15px;
text-align:center;
}
.tableStyle th{
padding:15px;
background-color:rgb(146,208,80);
background-image:-webkit-gradient(linear,left top,left bottom,from(#92D050),to(#A2D668));
}
</style>
</head>
<body>
<center><h1>测试报告</h1></center><br/>
<table class="tableStyle">
<thead>
<tr>
<th>search words</th>
<th>assert words</th>
<th>start time</th>
<th>waste time</th>
<th>status</th>
</tr>
</thead>
'''
endStr = u'''</table></body></html>'''
html=htmlStr+trData+endStr
print html
with open(r"C:\testHtml.html","w") as fp:
fp.write(html.encode("gbk"))
测试用例 datadrivertest.py
#encoding =utf-8
# -*- coding:utf-8 -*-
from selenium import webdriver
import unittest,time
import logging,traceback
import ddt
from reportHtml import htmlTemplate
from selenium.common.exceptions import NoSuchElementException
logger = logging.getLogger('mylogger1')
logger.setLevel(logging.DEBUG)
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler('C:/test.log')
fh.setLevel(logging.DEBUG)
# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# 定义handler的输出格式
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# 给logger添加handler
logger.addHandler(fh)
logger.addHandler(ch)
@ddt.ddt
class Testdemo(unittest.TestCase):
@classmethod
def setUpClass(cls):
Testdemo.trStr=""
def setUp(self):
self.driver=webdriver.Firefox(executable_path="C:\\webdriver\geckodriver")
status=None
flag=0
@ddt.file_data("test_data_list.json")
def test_dataDriverByFile(self,value):
flagDict={0:'red',1:'#00AC4E'}
url="http://www.baidu.com"
self.driver.get(url)
self.driver.maximize_window()
print value
testdata,expectdata=tuple(value.strip().split("||"))
self.driver.implicitly_wait(10)
try:
start=time.time()
startTime=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
self.driver.find_element_by_id("kw").send_keys(testdata)
self.driver.find_element_by_id("su").click()
time.sleep(3)
self.assertTrue(expectdata in self.driver.page_source)
except NoSuchElementException,e:
logger.error(u"找不到页面元素")
status='fail'
flag=0
except AssertionError,e:
logger.info(u"搜索失败")
status='fail'
flag=0
except Exception,e:
logger.info(u"未知错误")
status='fail'
flag=0
else:
logger.info(u"搜索成功")
status='pass'
flag=1
wasteTime=time.time()-start-3
Testdemo.trStr += u'''
<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%.2f</td>
<td style="color:%s">%s</td>
</tr><br/>'''%(testdata,expectdata,startTime,wasteTime,flagDict[flag],status)
def tearDown(self):
self.driver.quit()
@classmethod
def tearDownClass(cls):
htmlTemplate(Testdemo.trStr)
if __name__=='__main__':
unittest.main()