TestNg提供的最基本的注解之一就是Test注解,作用在方法或者类上,此注解支持的属性有:
1、alwaysRun:提供一个false or true值,如果设置为true,则被标记的方法会永远被执行,即使被标记方法所依赖的方法执行失败了。
2、dataProvider:此属性的值为标记方法提供数据驱动的数据源
3、dataProviderClass:此属性指出提供数据驱动方法的所在类
4、dependsOnGroups:此属性指出标记方法所依赖的组
5、dependsOnMethods:此属性支持标记方法所依赖的方法
6、description:标记方法的描述信息
7、enabled:标记方法是否要执行,默认为true执行
8、expectedExceptions:指定标记方法返回的异常信息列表
9、groups:指定标记方法归属于哪个组
10、timeOut:指定标记方法超时时长 (in millisecs)
11、invocationCount:被标记的方法会执行多次
12、threadPoolSize:启用多个线程执行被标记的方法,一般会与invocationCount一起使用
下面举几个常用的例子
1、enabled的使用例子,因方法testMethodTwo中enabled=false所以此方法在执行的时候会被忽略
package test;
import org.testng.annotations.Test;
public class DisableTestClass {
@Test(enabled=true)
public void testMethodOne(){
System.out.println("Test method one.");
}
@Test(enabled=false)
public void testMethodTwo(){
System.out.println("Test method two.");
}
@Test
public void testMethodThree(){
System.out.println("Test method three.");
}
}
2、expectedExceptions, 方法exceptionTestTwo 返回的异常类与期望的异常类不一致,方法exceptionTestThree返回的异常信息与期望的异常信息不一致所有这两个方法会执行报错
package test;
import java.io.IOException;
import org.testng.annotations.Test;
public class ExceptionTest {
@Test(expectedExceptions={IOException.class})
public void exceptionTestOne() throws Exception{
throw new IOException();
}
@Test(expectedExceptions={IOException.class, NullPointerException.class})
public void exceptionTestTwo() throws Exception{
throw new Exception();
}
@Test(expectedExceptions={IOException.class}, expectedExceptionsMessageRegExp="Pass Message test")
public void exceptionTestThree() throws Exception{
throw new IOException("Pass Message test1");
}
@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp=".*")
public void exceptionTestFoure() throws Exception{
throw new IOException("Pass Message test1");
}
}
package test;
import org.testng.annotations.Test;
public class TimeSuite {
@Test
public void timeTestOne() throws InterruptedException{
Thread.sleep(1000);
System.out.println("Time test method one");
}
@Test
public void timeTestTwo() throws InterruptedException{
Thread.sleep(400);
;
}
}
groups, dependsOnMethods, dependsOnGroups:
package test;
import org.testng.annotations.Test;
public class DependencyTest {
//方法依赖
@Test(dependsOnMethods={"testTwoMethod"})
public void testOneMethod(){
System.out.println("Test One Method");
}
//组依赖
@Test(groups={"group-one"},dependsOnGroups={"group-two"})
public void testTwoMethod(){
System.out.println("Test Two Method");
}
@Test(groups={"group-two"})
public void testThreeMethod(){
System.out.println("Test Three Method");
}
//依赖组支持正则匹配
@Test(dependsOnGroups={".*up-th.*"})
public void testFourMethod(){
System.out.println("Test Four Method");
}
@Test(groups={"group-three"})
public void testFiveMethod(){
System.out.println("Test Five Method");
}
}
依赖除了在代码中利用属性字段dependsOnGroups来标注外,也可以在testng.xml中配置如下:
invocationCount, threadPoolSize的使用方法的举例
package test;
import org.testng.annotations.Test;
public class ThreadPool {
@Test(invocationCount = 100, threadPoolSize=100)
public void ThreadTest() throws InterruptedException{
System.out.println("Thread.currentThread() is:" + Thread.currentThread().getId());
Thread.sleep(1000);
}
}