使用testng.xml文件运行指定的组执行测试时,必须把beforeclass,beforeMethod,aftermethod等需要初始化的方法设置为alwaysRun,因为使用.xml文件运行测试指定组的测试时,不会自动运行这些不在组内的初始化方法,就会出现没有初始化执行测试所需要的初始条件而导致测试失败的问题,如果没有指定特定的组,那么使用testng.xml执行测试就没问题,不需要设定那些初始化方法为alwaysRun=true,如下testng执行文件:
如下测试代码,必须把因为测试方法均需要先执行初始化方法init、clear,如果直接在Eclipse编辑代码窗口邮件运行testng测试,不需要把这两个方法设置为alwaysRun,但是如果使用testng.xml文件来运行这个类中指定的groups来执行测试,必须把init和clear方法设置为alwaysRun=true:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="mysuite">
<test name="mytest">
<groups>
<run>
<include name="test2"/>
</run>
</groups>
<classes>
<class name="Test.Pay_submit_Test"/>
<class name="Test.Userinfo_getUserExpireDate_Test" />
<class name="Test.Userinfo_getUserInfo_Test" />
</classes>
</test>
</suite>
如下测试代码,必须把因为测试方法均需要先执行初始化方法init、clear,如果直接在Eclipse编辑代码窗口邮件运行testng测试,不需要把这两个方法设置为alwaysRun,但是如果使用testng.xml文件来运行这个类中指定的groups来执行测试,必须把init和clear方法设置为alwaysRun=true:
package Test;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import CommonLib.DBMap;
import CommonLib.DateUtil;
import CommonLib.HttpClientUtil;
import CommonLib.JDBCUtils;
import CommonLib.StringUtils;
public class Pay_submit_Test{
@BeforeClass(alwaysRun=true)
public void init() {
// 清理缓存
String url = clearCacheUrl + uid;
HttpClientUtil.requestUrl(url);
}
@AfterMethod(alwaysRun=true)
public void clear() {
// 清理缓存
String url = clearCacheUrl + uid;
HttpClientUtil.requestUrl(url);
}
@DataProvider(name = "common_user_testdata")
public Object[][] common_user_testdata() {
return new Object[][] {
{1,2000,""},{3,4500,""},{12,15000,""},{13,16500,""},{24,30000,""}
};
}
@Test(priority = 1, dataProvider = "common_user_testdata",groups={"test1"})
public void commonUserSubmitOrder(int month, int needAmount, String handler) {
Assert.assertEquals(31 * month, map.getInt("adddays", -1));
Assert.assertEquals(needAmount, map.getInt("needamount", -1));
Assert.assertEquals(handler, map.getString("handler"));
}
@DataProvider(name="midpay_data")
public Object[][] midpay(){
return new Object[][]{
{1,2000,"PaySuccess=judge_1&"},{3,4500,""},{12,15000,""}
};
}
@Test(priority=3,dataProvider="midpay_data",groups={"test2"})
public void midPay(int month,int needAmount,String handler){
String url="testurl";
String sql="testsql";
DBMap<String, Object> map=JDBCUtils.queryForMap(database, sql, uid);
Assert.assertEquals(31*month, map.getInt("adddays", -100));
Assert.assertEquals(needAmount, map.getInt("needamount", -100));
Assert.assertEquals(handler, map.getString("handler"));
}
}