TestNG入门——注解之@Test

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

}

3、timeOut:首先在testng.xml中配置超时时间为500毫秒,也可直接在java代码中配置超时时间@Test(timeOut=500),xml中的配置信息如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Time Test Suite" time-out="500" verbose="1">
	<test name="Time Test">
	<classes>
		<class name="test.TimeSuite" />
	</classes>
	</test>
</suite>

然后在TimeSuite.java文件中添加如下代码,其中   timeTestOne因未在配置时间内完成,所以会执行不通过
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中配置如下:

<suite name="Regexpxmldependency Suite" verbose="1">
<test name="Regexp xml dependency Test">
<groups>
<dependencies>
<group name="test" depends-on="starts-with.*" /></dependencies>
<run>
<include name="test" />
</run>
</groups>
<classes>
<class name="test.depends.xml.RegularExpressionXmlTest" />
</classes>
</test>
</suite>


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

}





  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值