TestNG+Jenkins+Maven参数化测试dubbo接口
1.TestNG参数化测试用例:
package com.tree.autotest.testcase.IUserBankSummaryService;
import com.datatrees.finance.risk.dataclean.service.IUserBankSummaryService;
import com.tree.autotest.BaseTestNGTest;
import com.tree.autotest.commons.CommonUtil_2;
import com.tree.autotest.commons.TestCase;
import com.tree.autotest.demo.ExcelHandle;
import com.tree.autotest.jdbc.DBIdConstant;
import com.tree.autotest.jdbc.ManagedSqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.Assert;
import org.testng.annotations.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by lyh on 17/3/6.
*/
public class TestGetBankIdsOfNeedMoreBill extends BaseTestNGTest {
@Autowired
private IUserBankSummaryService iUserBankSummaryService;
private boolean handleOk;
private String insertSql;
private static String excel;
private static Map allCases;
private Integer testuser;
private TestCase testcase;
@BeforeMethod
public void setUp() throws SQLException {
//获取excel用例列表
handleOk = true;
testcase = CommonUtil_2.getTestCaseBean(allCases, "case1", "case1-1");
testuser = Integer.valueOf(testcase.param1);
//获取SQL
insertSql = testcase.stepSql;
//删除,添加数据
handleOk &= deleteData(testuser);
handleOk &= ManagedSqlUtil.insertBySql(insertSql, DBIdConstant.MySql_Basisdata_ID);
System.out.println("----->> @BeforeClass is finished");
}
@DataProvider(name = "testData")
private static Object[][] dataprovide()throws IOException {
excel="src/main/resources/case/IUserBankSummaryService/TestGetBankIdsOfNeedMoreBill.xlsx";
allCases = CommonUtil_2.getAllCases(excel);
System.out.println("dataprovide方法执行");
Object[][] testData = ExcelHandle.readXlsx(excel, "用例列表");
return testData;
}
@Test(dataProvider="testData")
public void test_case_1(HashMap<String, String> map) throws Exception {
testuser =Integer.valueOf(map.get("userid"));
//实际值:
List<Integer> actual_list = iUserBankSummaryService.getBankIdsOfNeedMoreBill(testuser);
//预期值:
List<Integer> excepted_list = get_excepted_list(testuser);
//验证:
System.out.println("userId值:"+testuser);
System.out.println("actual_list值:"+actual_list.toString());
System.out.println("excepted_list值:"+excepted_list.toString());
System.out.println("size值:"+excepted_list.size());
if (excepted_list.size()==0){
Assert.assertEquals(actual_list.toString(), "[]");
}else {
Assert.assertEquals(actual_list.toString(), excepted_list.toString());
}
System.out.println("----->> @Test is finished");
}
@AfterMethod
public void tearDown() {
testuser = Integer.valueOf(testcase.param1);
handleOk &= deleteData(testuser);
System.out.println("----->> AfterClass is finished");
}
//定义数据清理
public boolean deleteData(Integer userid) {
String deleteSql = "delete from t_user_bank_summary where userid = "+userid+";";
handleOk &= ManagedSqlUtil.deleteBySql(deleteSql, DBIdConstant.MySql_Basisdata_ID);
return handleOk;
}
//预期值
public List<Integer> get_excepted_list(Integer userid)throws SQLException{
String searchSql = " SELECT a.BankId " +
" FROM t_user_bank_summary a, (SELECT MAX(Id) mid " +
" FROM t_user_bank_summary " +
" WHERE UserId ="+userid+" " +
" GROUP BY BankId) t " +
" WHERE a.id = t.mid AND EffectiveBillCount12M<11";
ResultSet rs = ManagedSqlUtil.selectBySql(searchSql,DBIdConstant.MySql_Basisdata_ID);
List<Integer> list = new ArrayList<Integer>();
while (rs.next()){
list.add(rs.getInt(1));
}
return list;
}
}
2.test suite配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="1" name="Suite" parallel="tests">
<test name="risk-dataclean-test">
<packages>
<package name="com.tree.autotest.testcase.IUserCustomerService"/>
<package name="com.tree.autotest.testcase.IVariableService"/>
<package name="com.tree.autotest.testcase.IUserBillService"/>
<package name="com.tree.autotest.testcase.IUserBankService"/>
<package name="com.tree.autotest.testcase.IUserBankSummaryService"/>
<package name="com.tree.autotest.testcase.ICustomerBackService"/>
<package name="com.tree.autotest.testcase.IInsuranceBillService"/>
</packages>
<!--<classes>-->
<!--<class name="com.tree.autotest.testcase.IUserBankService.TestSelectListByUserId"/>-->
<!--</classes>-->
</test> <!-- Test -->
</suite> <!-- Suite -->
3.Pom.xml设置需运行的testng.xml文件路径和依赖:
配置插件:
<!--添加插件 关联testNg.xml-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<file>src/main/java/com/tree/autotest/testsuite/testng.xml</file>
</suiteXmlFiles>
<!--<workingDirectory>target/</workingDirectory>-->
<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>
</configuration>
</plugin>
配置POM.xml testNG相关依赖
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.4</version>
</dependency>
<!-- 依赖reportNg 关联testNg-->
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</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>
4.maven环境变量: vi .bash_profile
export JAVA_HOME=/aliyun/server/java
export JRE_HOME=/aliyun/server/java/jre
export CLASSPATH=/aliyun/server/java/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$JRE_HOME/lib:.:$CLASSPATH
export PATH=/aliyun/server/mysql/bin:/dashu/software/apache-maven-3.39/bin:/aliyun/server/nginx/sbin:$JAVA_HOME/bin:$PATH
5.maven执行测试:
maven clean test
6.Jenkins配置:
安装TestNG插件
git:http://192.168.5.252/testgroup/risk-dataclean-test.git
Shell:
export JAVA_HOME=/aliyun/server/java
export JRE_HOME=/aliyun/server/java/jre
export CLASSPATH=/aliyun/server/java/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$JRE_HOME/lib:.:$CLASSPATH
export PATH=/aliyun/server/mysql/bin:/dashu/software/apache-maven-3.39/bin:/aliyun/server/nginx/sbin:$JAVA_HOME/bin:$PATH
mvn clean test
TestNG Report:
**/testng-results.xml
7.html报告文件: