您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > TestNG
TestNG的參數(shù)化測試、共享線程池配置、參數(shù)默認(rèn)值配置
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2015/3/4 15:35:02 ] 推薦標(biāo)簽:TestNG 單元測試 軟件測試

  在使用TestNG進(jìn)行測試時(shí),經(jīng)常會使用到一些參數(shù)化配置,比如數(shù)據(jù)庫、連接池、線程池數(shù),
  使用TestNG的參數(shù)@Parameter注解進(jìn)行自動(dòng)化讀取
  使用多線程的方式運(yùn)行測試代碼配置: 在'<suite>'標(biāo)簽中配置data-provider-thread-count="20"
  Java代碼:
/**
*
* <p>
* Title: TestngParameters
* </p>
*
* <p>
* 參考配置文件testng-parameters.xml
* Description:參數(shù)化測試在配置文件中配置可執(zhí)行參數(shù),使用@Parameters注解來調(diào)用, 注解中參數(shù)名稱和類型必須和配置文件中一致
*
* 多線程的測試:在'<suite>'標(biāo)簽中配置data-provider-thread-count="20"
* </p>
*
* <p>
* Company:
* </p>
*
* @author : Dragon
*
* @date : 2014年10月13日
*/
public class TestngParameters {
// @Parameters注解內(nèi)對應(yīng)的參數(shù)名稱和配置文件中的key必須是相同
@Parameters({ "first-name" })
@Test
public void testSingleString(String secondName) {
System.err.println("Invoked testString " + secondName);
assert "Cedric".equals(secondName);
}
@Parameters({ "count" })
@Test
public void testSingleInteger(Integer count) {
System.err.println("Invoked count " + count);
assert count.intValue() == 8;
}
private String m_dataSource;
private String m_jdbcDriver;
private int poolSize;
/**
* <p>
* description:注:@Parameters定義的參數(shù)順序必須和方法的參數(shù)順序一致,配置文件中的參數(shù)只是和注解的參數(shù)名稱一致
* </p>
*
* @param ds
* @param driver
* @param poolSize
*/
@Parameters({ "datasource", "jdbcDriver", "poolSize" })
@BeforeMethod
public void beforeTest(String ds, String driver, int poolSize) {
this.m_dataSource = ds;
this.m_jdbcDriver = driver;
this.poolSize = poolSize;
System.err.println(getM_dataSource() + " --- " + getM_jdbcDriver()
+ " --- " + getPoolSize());
}
public String getM_dataSource() {
return m_dataSource;
}
public String getM_jdbcDriver() {
return m_jdbcDriver;
}
public int getPoolSize() {
return poolSize;
}
/**
* 如果在配置文件中沒有指定參數(shù)db,那么參數(shù)值將使用默認(rèn)值'mysql'
*
* @param db
*/
@Parameters("db")
@Test
public void testNonExistentParameter(@Optional("mysql") String db) {
System.err.println("db ..  " + db);
}
}

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd