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);
	}
}

配置文件:testng-parameter.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<!-- data-provider-thread-count="20" 共享线程池配置 -->
<suite name="framework_testng" data-provider-thread-count="20">

	<parameter name="first-name" value="Cedric" />
	<parameter name="count" value="8" />
	<parameter name="datasource" value="com.dbcp.source" />
	<parameter name="jdbcDriver" value="com.mysql.jdbc.driver" />
	<parameter name="poolSize" value="10" />

	<test verbose="2" name="TestGroups">
		<classes>
			<class name="com.dragon.testng.annotation.TestngParameters" />
		</classes>
	</test>
</suite>

测试结果:

com.dbcp.source --- com.mysql.jdbc.driver --- 10
db ..  mysql
com.dbcp.source --- com.mysql.jdbc.driver --- 10
Invoked count 8
com.dbcp.source --- com.mysql.jdbc.driver --- 10
Invoked testString Cedric
PASSED: testNonExistentParameter("mysql")
PASSED: testSingleInteger(8)
PASSED: testSingleString("Cedric")

===============================================
    TestGroups
    Tests run: 3, Failures: 0, Skips: 0
===============================================

如果我饶恕,

别认为我没原则。因为我明白,

得饶人时且饶人,不能把事做绝了。

时间: 2024-08-02 15:42:20

TestNG的参数化测试、共享线程池配置、参数默认值配置的相关文章

TestNG的參数化測试、共享线程池配置、參数默认值配置

在使用TestNG进行測试时,常常会使用到一些參数化配置,比方数据库.连接池.线程池数. 使用TestNG的參数@Parameter注解进行自己主动化读取 原创文章,版权全部.同意转载,标明出处:http://blog.csdn.net/wanghantong 使用多线程的方式执行測试代码配置: 在'<suite>'标签中配置data-provider-thread-count="20" Java代码: /** * * <p> * Title: TestngPa

线程池ThreadPoolExecutor参数设置

JDK1.5中引入了强大的concurrent包,其中最常用的莫过了线程池的实现ThreadPoolExecutor,它给我们带来了极大的方便,但同时,对于该线程池不恰当的设置也可能使其效率并不能达到预期的效果,甚至仅相当于或低于单线程的效率. ThreadPoolExecutor类可设置的参数主要有: corePoolSize 核心线程数,核心线程会一直存活,即使没有任务需要处理.当线程数小于核心线程数时,即使现有的线程空闲,线程池也会优先创建新线程来处理任务,而不是直接交给现有的线程处理.

memcached源码分析-----memcached启动参数详解以及关键配置的默认值

转载请注明出处: http://blog.csdn.net/luotuo44/article/details/42672913 本文开启本系列博文的代码分析.本系列博文研究是memcached版本是1.4.21. 本文将给出memcached启动时各个参数的详细解释以及一些关键配置的默认值.以便在分析memcached源码的时候好随时查看.当然也方便使用memcached时可以随时查看各个参数的含义.<如何阅读memcached源码>说到memcached有很多全局变量(也就是关键配置),这些

线程池c3p0和dbcp2的配置初始化实例

一.c3p0 public class ConnectionManager { public static ComboPooledDataSource dataSource; static { try { dataSource = new ComboPooledDataSource(); dataSource.setUser("freeswitch"); dataSource.setPassword("freeswitch"); dataSource.setJdbc

Java线程池,获取返回值

程序中使用的是Callable接口,可以获取线程的返回值. package liuzh; import java.util.LinkedList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.Lin

Java线程池构造参数详解

在ThreadPoolExecutor类中有4个构造函数,最终调用的是如下函数: public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) 构造函数一共有7个参

c3p0配置之preferredTestQuery参数默认值探秘

http://www.mchange.com/projects/c3p0/ c3p0的配置参数preferredTestQuery用于检测数据库连接测试,检测数据库是否能连接成功.Default: nullDefines the query that will be executed for all connection tests, if the default ConnectionTester (or some other implementation of QueryConnectionT

asp.net mvc中配置路由默认值(Area中)

public class RouteConfig { private static string[] namespaces = new string[1] { "Best.Site.Areas.BestPalace" }; public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.Ignore

完全解析线程池ThreadPool原理&amp;使用

目录 1. 简介 2. 工作原理 2.1 核心参数 线程池中有6个核心参数,具体如下 上述6个参数的配置 决定了 线程池的功能,具体设置时机 = 创建 线程池类对象时 传入 ThreadPoolExecutor类 = 线程池的真正实现类 开发者可根据不同需求 配置核心参数,从而实现自定义线程池 // 创建线程池对象如下 // 通过 构造方法 配置核心参数 Executor executor = new ThreadPoolExecutor( CORE_POOL_SIZE, MAXIMUM_POO