一个简单的自动化测试报告

一个简单的自动化测试报告。

一个自动化测试报告中可能包括一个或者多个测试用例,

一个用例包括一个或者多个测试步骤。

记录测试用例的执行结果、测试步骤的执行结果,展示在报告的页面。

测试报告的效果图如下

对应的JAVA代码报告对象:
import java.util.ArrayList;
import java.util.List;

public class TestSuit {
	
	private String sysAddress;
	List<TestCase> testCases;
	
	public TestSuit(String sysAddress){
		this.sysAddress = sysAddress;
	
		this.testCases = new ArrayList<TestCase>();
	}
	
	public String getSysAddress() {
		return sysAddress;
	}
	public void setSysAddress(String sysAddress) {
		this.sysAddress = sysAddress;
	}
	public List<TestCase> getTestCases() {
		return testCases;
	}
	public void setTestCases(List<TestCase> testCases) {
		this.testCases = testCases;
	}
	
	public void addTestCase(TestCase testCase){
		testCases.add(testCase);
	}
	public int getTestCaseNum(){
		return testCases.size();
	}
}
用例对象:
<pre name="code" class="java">import java.util.ArrayList;
import java.util.List;

public class TestCase {
	private String testCaseName;
	private String result;
	private List<TestStep> steps;
	
	public TestCase(String testCaseName){
		this.testCaseName = testCaseName;
		steps = new ArrayList<TestStep>();
	}
	
	public String getTestCaseName() {
		return testCaseName;
	}

	public void setTestCaseName(String testCaseName) {
		this.testCaseName = testCaseName;
	}

	public List<TestStep> getSteps() {
		return steps;
	}

	public void setSteps(List<TestStep> steps) {
		this.steps = steps;
	}
	
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public void addStep(TestStep step){
		steps.add(step);
	}
	public int getStepNum(){
		return steps.size();
	}
}
步骤对象:
<pre name="code" class="java">public class TestStep {
	
	private String step;
	private String stepData;
	private String expectedResult;
	private String actualResult;
	private String operatoType;
	
	public TestStep(String operatoType, String step,String stepData,String expectedResult,String actualResult){
		this.operatoType =operatoType;
		this.step = step;
		this.stepData = stepData;
		this.expectedResult = expectedResult;
		this.actualResult = actualResult;
		
	}
	
	public String getStep() {
		return step;
	}
	public void setStep(String step) {
		this.step = step;
	}
	public String getStepData() {
		return stepData;
	}
	public void setStepData(String stepData) {
		this.stepData = stepData;
	}
	public String getExpectedResult() {
		return expectedResult;
	}
	public void setExpectedResult(String expectedResult) {
		this.expectedResult = expectedResult;
	}
	public String getActualResult() {
		return actualResult;
	}
	public void setActualResult(String actualResult) {
		this.actualResult = actualResult;
	}

	public String getOperatoType() {
		return operatoType;
	}

	public void setOperatoType(String operatoType) {
		this.operatoType = operatoType;
	}
	
}
生成报告类
<pre name="code" class="java">import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ToHTMLReport {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		TestSuit testReport = new TestSuit("测试网址");
		TestCase case1 = new TestCase("新增用户");
		TestCase case2 = new TestCase("修改用户");
		TestCase case3 = new TestCase("删除用户");
		TestStep case1_step1 = new TestStep("click","点击菜单","","跳转操作页面","Success");
		TestStep case1_step2 = new TestStep("type","录入新用户","Test001","成功录入","Success");
		TestStep case1_step3 = new TestStep("type","录入新密码","55555","成功录入","fail");
		TestStep case2_step1 = new TestStep("click","点击菜单","","跳转操作页面","Success");
		TestStep case2_step2 = new TestStep("type","录入条件","Test001","成功录入","Success");
		TestStep case3_step1 = new TestStep("click","查询","","返回查询结果","Success");
		TestStep case3_step2 = new TestStep("click","选择查询结果","Test001","成功录入","Success");
		TestStep case3_step3 = new TestStep("type","录入原因","删除用户测试","成功录入","Success");
		TestStep case3_step4 = new TestStep("click","点击删除","","返回再次确认框","fail");
		TestStep case3_step5 = new TestStep("click","确认删除","","页面关闭","fail");
		TestStep case3_step6 = new TestStep("type","退出系统","","退出系统","fail");
		case1.addStep(case1_step1);
		case1.addStep(case1_step2);
		case1.addStep(case1_step3);
		case1.setResult("fail");
		case2.addStep(case2_step1);
		case2.addStep(case2_step2);
		case2.setResult("Success");
		case3.addStep(case3_step1);
		case3.addStep(case3_step2);
		case3.addStep(case3_step3);
		case3.addStep(case3_step4);
		case3.addStep(case3_step5);
		case3.addStep(case3_step6);
		case3.setResult("Success");
		testReport.addTestCase(case1);
		testReport.addTestCase(case2);
		testReport.addTestCase(case3);
		testReport.addTestCase(case3);
		testReport.addTestCase(case3);
		testReport.addTestCase(case3);
		
		try {
			ToReport(testReport,"测试平台");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static void ToReport(TestSuit testSuit, String sysName)
			throws IOException {

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String testDate = dateFormat.format(new Date());
		String filePath = "AutoTestReport\\" + "TestResult-" + "-" + testDate.replaceAll(":", "_")+ ".html";
		FileWriter fstream = new FileWriter(filePath);
		System.out.println("Report's FileName: " + filePath);

		BufferedWriter out = new BufferedWriter(fstream);

		// make DATA Begin

		List<TestCase> testCases = testSuit.getTestCases();
		int testCaseNum = testCases.size();
		int passCaseNum = 0;
		int failCaseNum = 0;
		int passStep = 0;
		int failStep = 0;

		String content = "<h3>测试用例数据显示</h3>" + "<table width=\"95%\" border=\"1\">"
				+ "<tr class=\"firstRow\">"
				+ "<th width=\"10%\" scope=\"col\">序号</th>"
				+ "<th width=\"42.5%\" scope=\"col\">测试用例名称</th>"
				+ "<th width=\"27.5%\" scope=\"col\">回归测试结果</th>"
				+ "<th width=\"15%\" scope=\"col\">操作步骤明细</th>"
				+ "</tr>";

		for (int i = 0; i < testCaseNum; i++) {
			TestCase oneTestCase = testCases.get(i);
			String color = "";
			if (oneTestCase.getResult() == "fail") {
				color = "red";
				failCaseNum = failCaseNum + 1;
			} else {
				passCaseNum = passCaseNum + 1;
				if(i%2==0){
					color = "#ffffff";
				}else{
					color = "#AFFFEE";
				}
				
			}
			content = content + "<tr align=\"center\" bgcolor=\"" + color + "\">"
					+ "<td>"+ (i+1) + "</td>"
					+ "<td>"+ oneTestCase.getTestCaseName() + "</td>"
					+ "<td><span id = \"TestCaseResult"+i+"\">"+ oneTestCase.getResult() + "</span></td>"
					+ "<td>"+ "<span id=\"" + i+ "\" onClick=\"showDetail(this.id)\">步骤查看</span></td>"
					+ "</tr>"
					+ "<tr id=\"showDetail"+i+"\" style=\"display:none\">"
					+ "<td colspan=6 align=\"center\">" + "<table width=\"90%\" border=\"1\">"
					+ "<tr  bgcolor=\"#FFF978\">"
					+ "<th width=\"10%\" scope=\"col\">操作步骤</th>"
					+ "<th width=\"10%\" scope=\"col\">操作类型</th>"
					+ "<th width=\"30%\" scope=\"col\">操作内容</th>"
					+ "<th width=\"15%\" scope=\"col\">操作数据</th>"
					+ "<th width=\"25%\" scope=\"col\">预期结果</th>"
					+ "<th width=\"10%\" scope=\"col\">实际结果</th>" + "</tr>";

			List<TestStep> steps = oneTestCase.getSteps();
			for (int j = 0; j < steps.size(); j++) {
				TestStep step = steps.get(j);
				if (step.getActualResult() == "fail") {
					color = "red";
					failStep = failStep + 1;
				} else {
					passStep = passStep + 1;
					if(j%2==0){
					color = "#ffffff";
					}else{
					color="#e5e5e5";
					}
				}
				content = content + "<tr bgcolor=\"" + color + "\">" 
							+ "<td >"+ (j+1) + "</td>" 
							+ "<td >"+ step.getOperatoType() + "</td>" 
							+ "<td >" + step.getStep() + "</td>"
							+ "<td >" + step.getStepData()+ "</td>" 
							+ "<td >"+ step.getExpectedResult() + "</td>" 
							+ "<td >" + step.getActualResult() + "</td>"
							+ "</tr>";
			}

			content = content + "</table>" + "</td>" + "</tr>";
		}

		content = content + "</table></body></html>";
		int stepsNum = passStep + failStep;
		// 创建一个数值格式化对象
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后2位
		numberFormat.setMaximumFractionDigits(2);
		String stepRate = numberFormat.format((float)passStep/(float)stepsNum*100)+"%";
		String caseRate = numberFormat.format((float)passCaseNum/(float)testCaseNum*100)+"%";

		// make DATA End

		out.write("<html>");
		out.write("<head>");
		out
				.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">"
						+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
						+ "<head>"
					//	+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
						+ "<title>TestReport</title>"
						+ "<style type=\"text/css\">"
						+ "tr.firstRow{" 
						+ "background-color: #20B2AA;" 
						+ "font-weight: bold;}"
						+ "</style>"
						+ "<script type=\"text/javascript\">"
						+ "function showDetail(id){"
						+ "var te = document.getElementById(\"showDetail\"+id).style.display;"
						+ "if(te==\"none\"){"
						+ "document.getElementById(\"showDetail\"+id).style.display=\"\";"
						+ "document.getElementById(id).parentNode.parentNode.style.backgroundColor=\"#007fff\";"
						+ "}else{"
						+ "document.getElementById(\"showDetail\"+id).style.display=\"none\";"
						+ "var result = document.getElementById(\"TestCaseResult\"+id).innerHTML;"
						+ "if(result ==\"fail\"){"
						+ "document.getElementById(id).parentNode.parentNode.style.backgroundColor=\"#ff0000\";"
						+ "}else{"
						+ "if(id%2==0){"
						+ "document.getElementById(id).parentNode.parentNode.style.backgroundColor=\"#ffffff\";"
						+ "}else{"
						+ "document.getElementById(id).parentNode.parentNode.style.backgroundColor=\"#AFFFEE\";"
						+ "}}"
						+ "}"
						+ "}"
						+ "</script>"
						+ "</head>"
						+ "<body>"
						+ "<h1 align=\"center\">"
						+ sysName
						+ "-自动化回归测试报告</h1>"
						+ "<p>测试地址:<u>"
						+ testSuit.getSysAddress()
						+ "</u></p>"
						+ "<p>测试时间:<u>"
						+ testDate
						+ "</u></p>"
						+ "<table width=\"60%\" border=\"1\">"
						+ "<caption><h2>测试结果总汇</h2</caption>"
						+ "<tr class=\"firstRow\">"
						+ "<td>测试用例数</td>"
						+ "<td>通过数</td>"
						+ "<td>不通过数</td>"
						+ "<td>通过率</td>"
						+ "</tr>"
						+ "<tr bgcolor=\"#AFFFEE\">"
						+ "<td>"
						+ testCaseNum
						+ "</td>"
						+ "<td>"
						+ passCaseNum
						+ "</td>"
						+ "<td>"
						+ failCaseNum
						+ "</td>"
						+ "<td>"
						+ caseRate
						+ "</td>"
						+ "</tr>"
						+ "<tr class=\"firstRow\">"
						+ "<td>测试步骤数</td>"
						+ "<td>通过数</td>"
						+ "<td>不通过数</td>"
						+ "<td>通过率</td>"
						+ "</tr>"
						+ "<tr bgcolor=\"#AFFFEE\">"
						+ "<td>"
						+ stepsNum
						+ "</td>"
						+ "<td>"
						+ passStep
						+ "</td>"
						+ "<td>"
						+ failStep
						+ "</td>"
						+ "<td>"
						+ stepRate + "</td>" + "</tr>" + "</table>");
		out.write(content);
		out.flush();
		out.close();
	}

}

 
 

                
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值