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(HashMapmap) throws Exception { testuser =Integer.valueOf(map.get("userid")); //实际值: List actual_list = iUserBankSummaryService.getBankIdsOfNeedMoreBill(testuser); //预期值: List 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 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 list = new ArrayList (); while (rs.next()){ list.add(rs.getInt(1)); } return list; } } 2.test suite配置:
3.Pom.xml设置需运行的testng.xml文件路径和依赖: 配置插件:
配置POM.xml testNG相关依赖 org.apache.maven.plugins maven-surefire-plugin 2.6 true src/main/java/com/tree/autotest/testsuite/testng.xml usedefaultlisteners false listener org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter target/ always
org.testng testng 6.4 org.uncommons reportng 1.1.4 test org.testng testng com.google.inject guice 3.0 test
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/jreexport CLASSPATH=/aliyun/server/java/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$JRE_HOME/lib:.:$CLASSPATHexport PATH=/aliyun/server/mysql/bin:/dashu/software/apache-maven-3.39/bin:/aliyun/server/nginx/sbin:$JAVA_HOME/bin:$PATHmvn clean testTestNG Report:
**/testng-results.xml
7.html报告文件: