2、使用ReportNG插件来优化测试报告
Pom.xml文件配置更新
1、属性配置
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <xmlFileName>testng.xml</xmlFileName> </properties> |
2、依赖包管理配置
<!-- testNG依赖管理 --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.11</version> </dependency> <dependency> <groupId>org.apache.maven.reporting</groupId> <artifactId>maven-reporting-api</artifactId> <version>2.0.9</version> <scope>test</scope> <exclusions> <exclusion> <groupId>org.testng</groupId> <artifactId>testng</artifactId> </exclusion> </exclusions> </dependency> <!-- 依赖Guice --> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>3.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.uncommons</groupId> <artifactId>reportng</artifactId> <version>1.1.4</version> </dependency> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <type>maven-plugin</type> </dependency> <dependency> <groupId>com.beust</groupId> <artifactId>jcommander</artifactId> <version>1.64</version> </dependency> <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.10</version> </dependency> |
3、Build及插件配置
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile> src/test/resources/${xmlFileName} </suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <properties> <property> <name>usedefaultlisteners</name> <value>false</value> </property> <property> <name>listener</name> <value>org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter</value> </property> </properties> <workingDirectory>target/</workingDirectory> <forkMode>always</forkMode> <!-- 解决报告中中文乱码 --> <argLine>-Dfile.encoding=UTF-8</argLine> </configuration> </plugin> </plugins> </build> |
运行查看报告
用Maven Test运行完成后,可以发现target/目录下出现新的文件夹
进入这个目录,打开html/index.html文件可以查看测试报告,如下图:
这样的页面是不是比刚才的美观了很多。
如果就这样的报告,肯定不饱满,那我们需要给测试案例添加测试数据获取,业务逻辑,断言以及多个测试类等等。
3、测试案例的参数化设计
参数化设计逻辑图
项目详细的目录结构
ExcelUtils处理类
(代码未经优化,有些地方可能写得不合理,请见谅!)
package com.test.pub; import java.io.File; import java.util.HashMap; import java.util.Map; import jxl.Workbook; import jxl.Sheet; import jxl.Cell; public class ExcelUtils { public static Map <String, HashMap> excelToHashMapByFirstSheet(String fPath, String sheetName){ try{ File fExcel = getExcelFileObj(fPath); Workbook wb = Workbook.getWorkbook(fExcel); Sheet sht = wb.getSheet(sheetName); int rowCount = sht.getRows(); int colCount = sht.getColumns(); Cell cel = null; Map <String, HashMap> excelContents = new HashMap(); if(rowCount<=2){ System.out.println("无测试案例"); return null; }else{ //检查是否存在空行 String rowContents = ""; for(int i=2;i<rowCount;i++){ if(sht.getCell(0, i).getContents().toString().length()==0){ System.out.println("测试案例文件中存在空行"); return null; }else{ for(int j=0;j<colCount;j++){ rowContents = rowContents + sht.getCell(j, i).getContents().toString(); } if (rowContents.length()<20){ System.out.println("测试案例文件中存在空行"); return null; } } } } //开始读取内容 for(int rowIndex=2;rowIndex<rowCount;rowIndex++){ HashMap<String, String> rowMap = new HashMap(); String testCaseCode = sht.getCell(0, rowIndex).getContents().toString(); for(int colIndex=1;colIndex<colCount;colIndex++){ rowMap.put(sht.getCell(colIndex, 1).getContents().toString(), sht.getCell(colIndex, rowIndex).getContents().toString()); } excelContents.put(testCaseCode, rowMap); } wb.close(); //HashMap<String, String> tmpMap = new HashMap(); //tmpMap.put("count", "" + (rowCount-2)); //excelContents.put("testsCount", tmpMap); return excelContents; }catch (Exception e){ System.out.println("发生异常:" + e); } return null; } public static File getExcelFileObj(String fPath){ try{ File fExl = new File(fPath); return fExl; }catch (Exception e){ System.out.println(e); } return null; } } |
BeforeClass的处理
(代码未经优化,有些地方可能写得不合理,请见谅!)
//所有测试案例变量
public Map<String, HashMap> allIdAuthTestCases;
//测试案例文件路径--excel作为存储
public String testCasePath = new TestDirUtils().getTestCasesDir() + "/IdAuthTestCases.xls";
@BeforeClass
public void beforeClass() {
try{
allIdAuthTestCases = ExcelUtil.excelToHashMapByFirstSheet(testCasePath, "身份证鉴权");
}catch(Exception e){
e.printStackTrace();
}
}
@Test测试方法的处理
IdAuth.java类代码
(代码未经优化,有些地方可能写得不合理,请见谅!)
package com.test.api;
import static org.testng.Assert.assertFalse;
import java.util.HashMap;
import java.util.Map;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import com.test.pub.*;
public class IdAuth {
//所有测试案例变量
public Map<String, HashMap> allIdAuthTestCases;
//测试案例文件路径--excel作为存储
public String testCasePath = new TestDirUtils().getTestCasesDir() + "/IdAuthTestCases.xls";
@BeforeClass
public void beforeClass() {
try{
allIdAuthTestCases = ExcelUtils.excelToHashMapByFirstSheet(testCasePath, "身份证鉴权");
}catch(Exception e){
e.printStackTrace();
}
System.out.println("allIdAuthTestCases:" + allIdAuthTestCases);
}
/*
* ***************************** 身份证鉴权 *******************************
*/
/*
* 姓名为空
*/
@Test(timeOut=10000, description="姓名为空")
public void testById_NameIsNull() {
//测试案例编号
String testCaseCode = "ById_NameIsNull";
//测试数据
HashMap<String, String> testCaseData = new HashMap();
System.out.println("allIdAuthTestCases——>" + allIdAuthTestCases);
System.out.println(allIdAuthTestCases.containsKey(testCaseCode));
//判断是否存在当前案例数据
try {
if (allIdAuthTestCases.containsKey(testCaseCode)) {
testCaseData = allIdAuthTestCases.get(testCaseCode);
//执行测试案例
//断言测试结果
Assert.assertTrue(true);
} else {
Assert.fail("不存在当前测试方法的案例,请检查测试案例文件!");
}
} catch(Exception e) {
e.printStackTrace();
Assert.fail("测试案例获取失败");
}
}
/*
* 证件号为空
*/
@Test(timeOut=10000, description="证件号为空")
public void testById_IdNoIsNull() {
//测试案例编号
String testCaseCode = "ById_IdNoIsNull";
//测试数据
HashMap<String, String> testCaseData = new HashMap();
//判断是否存在当前案例数据
try {
if (allIdAuthTestCases.containsKey(testCaseCode)) {
testCaseData = allIdAuthTestCases.get(testCaseCode);
//执行测试案例
//断言测试结果
Assert.assertTrue(false);
} else {
Assert.fail("不存在当前测试方法的案例,请检查测试案例文件!");
}
} catch(Exception e) {
e.printStackTrace();
Assert.fail("测试案例获取失败");
}
}
/*
* 姓名与证件号均正确
*/
@Test(timeOut=10000, description="姓名与证件号均正确")
public void testById_IdNoAndNameRight() {
//测试案例编号
String testCaseCode = "ById_IdNoAndNameRight";
//测试数据
HashMap<String, String> testCaseData = new HashMap();
//判断是否存在当前案例数据
try {
if (allIdAuthTestCases.containsKey(testCaseCode)) {
testCaseData = allIdAuthTestCases.get(testCaseCode);
//执行测试案例
//断言测试结果
Assert.assertTrue(true);
} else {
Assert.fail("不存在当前测试方法的案例,请检查测试案例文件!");
}
} catch(Exception e) {
e.printStackTrace();
Assert.fail("测试案例获取失败");
}
}
@AfterClass
public void afterClass() {
}
}
包com.test.pub下的TestDirUtils类代码
(代码未经优化,有些地方可能写得不合理,请见谅!)
package com.test.pub;
public class TestDirUtils {
// 获取主目录
public String getMainDir() {
String userDir = System.getProperty("user.dir");
if (userDir.indexOf("target")>0) {
userDir = userDir.split("target")[0];
}
int userDirLen = userDir.length();
String subUserDir = userDir.substring(userDirLen-1, userDirLen);
if (subUserDir.equals("/")) {
userDir = userDir.substring(0, userDirLen-1);
}
return userDir;
}
// 获取用例目录路径
public String getTestCasesDir() {
String testCasesDir = getMainDir();
if (testCasesDir.indexOf("target")>0) {
testCasesDir = testCasesDir.split("target")[0];
}
int userDirLen = testCasesDir.length();
String subUserDir = testCasesDir.substring(userDirLen-1, userDirLen);
if (subUserDir.equals("/")) {
testCasesDir = testCasesDir.substring(0, userDirLen-1);
}
return testCasesDir + "/src/test/resources/testcases";
}
}
运行结果:
OK, 大功告成,基本实现一个简单的Maven+TestNG自动化测试框架。