现状:
看过前面的文章,楼主用的是python,所以在搭建测试报告这块的时候使用的是unittest+htmlTestRunner;然后发现生成出来的报告,总是有那么不完美的地方,比如想增加图片,比如显示风格改变下;然后尝试性的去修改了一个htmlTestRunner源码,虽然图片出来了,显示风格修改不了...so...萌生的弃意...
问题:
1.unittest
unittest当案例异常或脚本执行异常时,无法继续执行下个案例~,比如登录时,后台服务异常、登录用户无菜单权限异常、新增用户状态导致无法登录异常等等;
unittest.assertEqual、assertRaises等断言方法对复杂的功能逻辑难以处理~~,比如需要if/else、or、and组合用的逻辑判断。
2.htmlTestrunner
case测试数据输出信息不全,关键不能做一些炫酷的事,自动对接缺陷管理系统、自动验证缺陷(重点);
历史数据信息无法保存;
样式总有那么不满意的地方。
方案:
a.结果验证、测试参数输出、测试参数输出自己封装;
b.测试数据信息保存在mysql;
c.界面,jsp读取mysql数据展示。
技术实现:
a.略,后面专题介绍
b.数据库数据的插入与相关初始化
MySQLdb.py
#coding=utf-8
import MySQLdb
from com.iapppay.test.loginfo.loginfoobject import *
# reload(sys)
# reload(sys).setdefaultencoding('gbk')
# def printStr(**kwargs):
# return kwargs[0][3]
'''
runTime CREATE TABLE `runTime` (
`startTime` datetime DEFAULT NULL,
`endTime` datetime DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
casedata CREATE TABLE `casedata` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`casedata` varchar(2000) DEFAULT NULL COMMENT '案例参数',
`caseinfo` varchar(2000) DEFAULT NULL COMMENT '案例输出信息',
`status` varchar(6) DEFAULT NULL COMMENT '验证结果',
`casebank` varchar(2000) DEFAULT NULL COMMENT '备注',
`casetime` datetime DEFAULT NULL COMMENT '执行时间',
`system` varchar(12) DEFAULT NULL COMMENT '系统',
`action` varchar(12) DEFAULT NULL COMMENT '功能',
`imageinfo` varchar(2000) DEFAULT NULL COMMENT '图片信息',
`caseR` int(11) DEFAULT NULL COMMENT '案例点',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1159 DEFAULT CHARSET=utf8;
'''
insert = "insert into `casedata` (`casedata`, `caseinfo`, `status`, `casebank`, `casetime`, `system`, `action`) values('sasdasd','sadasd','fase','sdasd',NULL,'admin','login','caseR');"
#访问数据库
try:
conn =MySQLdb.connect(host="192.168.0.157",port=3306,user="root",passwd="123456",db="webTest",charset="utf8") #v35test_iapppay,webTest
except MySQLdb.Error as e:
logging.ERROR(e)
pass
#插入数据公共方法
def insertData(str):
cursor = conn.cursor()
try:
cursor.execute(str)
conn.commit()
except Exception as e:
logging.ERROR(e)
pass
conn.close
insertData.py
#coding=utf-8
'''
Created on 2014��7��17��
@author: Ф肖彬
'''
import time
#获取当前时间
def dateTime():
return time.strftime('%Y-%m-%d %H:%S',time.localtime(time.time()))
#初始化生成insert sql
def data(casedata,caseinfo,status,casebank,casetime,system,action,imageinfo):
data ="insert into `casedata`(`casedata`, `caseinfo`, `status`, `casebank`, `casetime`, `system`, `action`,`imageinfo`)values('"+casedata+"','"+caseinfo+"','"+status+"','"+casebank+"','"+casetime+"','"+system+"','"+action+"','"+imageinfo+"');"
return data
#对特殊字符处理
def returnargs(*args):
data =""
for i in (range(len(args))):
data += str(args[i])
data = data.replace("'","\\'")
return str(data)
#初始化时间插入sql
def runtimeData(startTime,endTime):
data = "insert into runTime(startTime,endTime) values ('"+startTime+"','"+endTime+"');"
return data
3.jsp页面访问
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
String par = request.getParameter("pay");
%>
<style media="screen" type="text/css">
body { font-family: verdana, arial, helvetica, sans-serif; font-size: 80%; }
table { font-size: 100%; }
pre { }
/* -- heading ---------------------------------------------------------------------- */
h1 {
font-size: 16pt;
color: gray;
}
.heading {
margin-top: 0ex;
margin-bottom: 1ex;
}
.heading .attribute {
margin-top: 1ex;
margin-bottom: 0;
}
.heading .description {
margin-top: 4ex;
margin-bottom: 6ex;
}
.overflow{
overflow:auto;
}
#show_detail_line {
margin-top: 3ex;
margin-bottom: 1ex;
}
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>爱贝自动化监控界面</title>
</head>
<body>
<script language="javascript" type="text/javascript">
alert(<%=par%>);
/* level - 0:pass; 1:false; 2:error 3:all */
function showCase(level){
if (level == 0){
par = "hello1";
alert(<%=par%>);
}
if (level == 1){
par = "hello2";
alert(<%=par%>);
}
if (level == 2){
par = "hello3";
alert(<%=par%>);
}
if (level == 3){
par = "hello4";
alert(<%=par%>);
}
}
</script>
<div class='heading'>
<h1>爱贝自动化测试监控</h1>
<p class='attribute'><strong>Start Time:</strong> 2014-07-16 16:17:45</p>
<p class='attribute'><strong>Duration:</strong> 0:00:00.027000</p>
<p class='attribute'><strong>Status:</strong> pass 1 false 1 error 1</p>
</div>
<p id='show_detail_line'>测试结果:
<a href='javascript:showCase(0)'>Pass</a>
<a href='javascript:showCase(1)'>false</a>
<a href='javascript:showCase(2)'>Error</a>
<a href='javascript:showCase(3)'>All</a>
</p>
<div class = 'heading'>
<table border=1 style="table-layout:fixed;word-wrap:break-word;" width="100%" id = 'overflow' >
<tr>
<td width ="3%">ID</td>
<td width ="20%">测试数据</td>
<td width ="20%">测试输出</td>
<td width ="5%">测试结果</td>
<td width ="10%">测试备注</td>
<td width ="10%">执行时间</td>
<td width ="5%">系统</td>
<td width ="5%">action</td>
<td width ="5%">图片</td>
</tr>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=java.sql.DriverManager.getConnection("jdbc:mysql://192.168.0.157/webTest?useUnicode=true&characterEncoding=utf-8","root","123456");
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select id,casedata,caseinfo,status,casebank,casetime,system,action,imageinfo from casedata order by id desc");
while(rst.next())
{
out.println("<tr>");
out.println("<td>"+rst.getString("id")+"</td>");
out.println("<td>"+rst.getString("casedata")+"</td>");
out.println("<td>"+rst.getString("caseinfo")+"</td>");
out.println("<td>"+rst.getString("status")+"</td>");
out.println("<td>"+rst.getString("casebank")+"</td>");
out.println("<td>"+rst.getString("casetime")+"</td>");
out.println("<td>"+rst.getString("system")+"</td>");
out.println("<td>"+rst.getString("action")+"</td>");
//out.println("<td>"+rst.getString("imageinfo")+"</td>");
out.println("<td>"+"<img src="+"/data/201407212947_1.jpg"+">"+"</img>"+"</td>");
out.println("</tr>");
}
//关闭连接、释放资源
rst.close();
stmt.close();
con.close();
%>
</table>
</div>
</body>
</html>
注意:需要导入com.mysql.jdbc.Driver lib包
最后:到这里,一套测试报告搭建完毕,最后是往数据表里面插入测试的相关数据就哦了