一、pom.xml增加testng相关配置
<!--添加插件 关联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/testng.xml</file> <!--testsuite.xml文件类路径-->
</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>
<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>
二、脚本开发
1、编写一个基础类作为测试类父类。
@ContextConfiguration(locations = {"classpath:/spring-context.xml"})
@Configuration
public class BaseTestNGTest extends AbstractTestNGSpringContextTests {
}
2、编写测试类,继承父类
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;
}
3、新建testsuite文件
<?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="demo">
<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.IInsuranceBillService"/>
<package name="com.tree.autotest.testcase.IUserBankService"/>
<package name="com.tree.autotest.testcase.IUserBankSummaryService"/>
<package name="com.tree.autotest.testcase.ICustomerBackService"/>
</packages>
<classes>
<class name="com.tree.autotest.testcase.IUserBankService.TestSelectListByUserId"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
4、直接执行testng.xml文件,运行成功的话,使用jenkins集成
三、Jenkins配置集成项目,使用HTML插件和TestNG插件解析报告
1、从git上得到测试项目文件
2、每天10点,15点构建测试
3、构建shell
4、HTML和TestNG report解析报告
5、构建完成后,点击HTML Report,展示结果