@Test 注解
顺序如下:
beforeSuite
beforeClass
beforeMethod
这是@test
afterMethod
afterClass
afterSuite
忽略测试
@Test(enabled = false) 忽略
@Test(enabled = true) 执行
组测试
groups on method
groups on class
package com.course.testng.groups;
import org.testng.annotations.Test;
public class GroupsOnMethod {
@Test(groups = "jane")
public void method1(){
System.out.println("这是jane的method111");
}
@Test(groups = "jane")
public void method2(){
System.out.println("这是jane的merhod222");
}
@Test(groups = "group")
public void method3(){
System.out.println("这是groups的method333");
}
}
创建一个文件 testng.xml C:\ > TestNG_WORKSPACE 来执行测试用例,在这里,我们将只执行这些测试,属于组functest。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<groups>
<run>
<include name="jane" />
</run>
</groups>
<classes>
<class name="GroupsOnMethod " />
</classes>
</test>
</suite>
异常测试
@Test(expectedExceptions = ArithmeticException.class)
public void divisionWithException() {
int i = 1 / 0;
System.out.println("After division the value of i is :"+ i);
}
依赖测试
TestNG允许指定依赖关系:
在@Test注释中使用属性dependsOnMethods,或者
在@Test注释中使用属性dependsOnGroups。
// This test will be failed.
@Test
public void method1() {
System.out.println("This is method 1");
throw new RuntimeException();
}
@Test(dependsOnMethods = { "method1" })
public void method2() {
System.out.println("This is method 2");
}
依赖测试
TestNG允许指定依赖关系:
在@Test注释中使用属性dependsOnMethods,或者
在@Test注释中使用属性dependsOnGroups。
// This test will be failed.
@Test
public void method1() {
System.out.println("This is method 1");
throw new RuntimeException();
}
@Test(dependsOnMethods = { "method1" })
public void method2() {
System.out.println("This is method 2");
}
参数化
常用的有@Parameters和@DataProvider两种注解。
TestNG可以通过两种不同的方式将参数直接传递给测试方法:
使用testng.xml
使用数据提供者
通过XML@Parameters或@DataProvider将参数传递给@Test方法。
可以通过@Optional来指定默认值,如果testng.xml没有配置对应的参数,则使用默认值传参。
@Parameters
public class ParametersOnXML {
@Test
@Parameters(value = "para")
public void parameterstest1( String a){
System.out.println("参数值为"+a);
}
@Test
@Parameters(value = "para1")
public void parameterstest2(@Optional("0") String b){
System.out.println("默认参数为"+b);
}
}
运行以下xml文件即可
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="parameters">
<test name="parameters">
<parameter name="para" value="999"></parameter>
<classes>
<class name="com.course.testng.parameters.ParametersOnXML" />
</classes>
</test>
</suite>
@DataProvider
注解的方法返回对象数组
public class DataProviderTest {
@Test(dataProvider = "provider")
public void testDataProvider(String a,int b){
System.out.println("参数="+a+";数字="+b);
}
@DataProvider(name = "provider")
public Object[] [] providerData(){
Object[] [] o = new Object[][]{
{"a",1},
{"b",2},
};
return o;
}
}
根据方法 进行参数传递,test1和testt2
@DataProvider(name = "methodData")
public Object[][] methodDataTest(Method method){
Object[][] result=null;
if(method.getName().equals("test1")) {
result = new Object[][]{
{"aa", 11},
{"bb", 22}
};
}else if (method.getName().equals("test2")){
result=new Object[][]{
{"CC",33},
{"dd",44}
};
}
return result;
}
}
多线程
@注解方式
在测试方法中,指定其可用的线程池
public class MultiThread {
// 测试方法在3个线程中并发执行,共被调用5次,执行超过10s
@Test(threadPoolSize = 3,invocationCount = 5,timeOut = 10000)
public void multiThreadTest(){
System.out.printf("test:%s %n",Thread.currentThread().getId());
}
}
@XML方式
public class MultiThreadOnXML {
@Test
public void test1() {
System.out.printf("test1 的线程:%s%n ", Thread.currentThread().getId() );
}
@Test
public void test2() {
System.out.printf("test2 的线程:%s%n ", Thread.currentThread().getId() );
}
@Test
public void test3() {
System.out.printf("test3 的线程:%s%n ", Thread.currentThread().getId() );
}
}
XML文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="threads" parallel="methods" thread-count="3">
<!--
tests级别:不同test tag下的用例可以在不同的线程执行,相同test tag下的用例只能在同一个线程中执行。
classs级别:不同class tag下的用例可以在不同的线程执行,相同class tag下的用例只能在同一个线程中执行。
methods级别:所有用例都可以在不同的线程去执行。
一般methods级别就可以
-->
<test name="test1">
<classes>
<class name="com.course.testng.multiThread.MultiThreadOnXML"></class>
<class name="com.course.testng.BasicAnnotation"></class>
</classes>
</test>
<test name="test2">
<classes>
<class name="com.course.testng.multiThread.MultiThreadOnXML"></class>
</classes>
</test>
</suite>
一般情况下,一个testng.xml只包含一个suite。如果想起多个线程执行不同的suite,官方给出的方法是:通过命令行的方式来指定线程池的容量。
java org.testng.TestNG -suitethreadpoolsize 3 testng1.xml testng2.xml testng3.xml
超时测试
public class TimeOutTest {
@Test(timeOut = 3000)
public void testSucess() throws InterruptedException{
Thread.sleep(2000);
}
@Test(timeOut = 2000)
public void testFailed() throws InterruptedException{
Thread.sleep(3000);
}
}
网友评论