您的位置:软件测试 > 开源软件测试 > 开源单元测试工具 > TestNG
TestNG的参数化测试、共享线程池配置、参数默认值配置
作者:网络转载 发布时间:[ 2015/3/4 15:35:02 ] 推荐标签:TestNG 单元测试 软件测试

  在使用TestNG进行测试时,经常会使用到一些参数化配置,比如数据库、连接池、线程池数,
  使用TestNG的参数@Parameter注解进行自动化读取
  使用多线程的方式运行测试代码配置: 在'<suite>'标签中配置data-provider-thread-count="20"
  Java代码:
/**
*
* <p>
* Title: TestngParameters
* </p>
*
* <p>
* 参考配置文件testng-parameters.xml
* Description:参数化测试在配置文件中配置可执行参数,使用@Parameters注解来调用, 注解中参数名称和类型必须和配置文件中一致
*
* 多线程的测试:在'<suite>'标签中配置data-provider-thread-count="20"
* </p>
*
* <p>
* Company:
* </p>
*
* @author : Dragon
*
* @date : 2014年10月13日
*/
public class TestngParameters {
// @Parameters注解内对应的参数名称和配置文件中的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定义的参数顺序必须和方法的参数顺序一致,配置文件中的参数只是和注解的参数名称一致
* </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;
}
/**
* 如果在配置文件中没有指定参数db,那么参数值将使用默认值'mysql'
*
* @param db
*/
@Parameters("db")
@Test
public void testNonExistentParameter(@Optional("mysql") String db) {
System.err.println("db ..  " + db);
}
}

上一页12下一页
软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd