一:TestNG常用注解
package com.lh.practice.testng;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class testNG {
@BeforeSuite
public void beforesuit() {
System.out.println("BeforeSuite");
}
@BeforeTest
public void BeforeTest() {
System.out.println("BeforeTest");
}
@BeforeClass
public void BeforeClass() {
System.out.println("BeforeClass");
}
@BeforeMethod
public void BeforeMethod() {
System.out.println("BeforeMethod");
}
@Test
public void test1() {
System.out.println("this is test1");
}
@Test
public void test2() {
System.out.println("this is test1");
}
@AfterSuite
public void AfterSuite() {
System.out.println("AfterSuite");
}
@AfterTest
public void AfterTest() {
System.out.println("AfterTest");
}
@AfterClass
public void AfterClass() {
System.out.println("AfterClass");
}
@AfterMethod
public void AfterMethod() {
System.out.println("AfterMethod");
}
}
二、depend依赖
package com.lh.practice.testng;
import org.testng.annotations.Test;
public class depend {
@Test
public void test1() {
System.out.println("sys1");
}
@Test(dependsOnMethods= {"test1"})
public void test2() {
System.out.println("sys2");
}
}
三、忽略
package com.lh.practice.testng;
import org.testng.annotations.Test;
public class ignoreTest {
@Test(enabled = false)
public void ingore() {
System.out.println("this is ingore");
}
@Test
public void noIngore() {
System.out.println("this is noIngore");
}
@Test(enabled = true)
public void Ingore2() {
System.out.println("this is noIngore2");
}
}
4、超时
package com.lh.practice.testng;
import org.testng.annotations.Test;
public class timeOutTest {
@Test(timeOut = 3000)
public void testSuccess() throws InterruptedException {
Thread.sleep(2000);
}
@Test(timeOut = 3000)
public void testUnSuccess() throws InterruptedException {
Thread.sleep(4000);
}
}
5、传参
package com.lh.practice.testng.paramter;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class parameterTest {
@Test
@Parameters({"name","age"})
public void parameter(String name,int age) {
System.out.println("my Name is"+" "+name+"\n"+"my Age is"+" "+age);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="parameter">
<test thread-count="5" name="Test">
<parameter name="name" value="lh"/>
<parameter name="age" value="26"/>
<classes>
<class name="com.lh.practice.testng.paramter.parameterTest"></class>
</classes>
</test>
</suite> <!-- Suite -->
5、分组
package com.lh.practice.testng.groups;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class groupsOnMethod {
@Test(groups="server")
public void test1() {
System.out.println("这是服务端组的测试方法111111");
}
@Test(groups="server")
public void test2() {
System.out.println("这是服务端组的测试方法222222");
}
@Test(groups="client")
public void test3() {
System.out.println("这是客户端组的测试方法111111");
}
@Test(groups="client")
public void test4() {
System.out.println("这是客户端组的测试方法222222");
}
@BeforeGroups(groups="server")
public void serverBeforeGroups() {
System.out.println("这是服务端之前运行的方法");
}
@AfterGroups(groups="server")
public void serverAfterGroups() {
System.out.println("这是服务端之前运行的方法");
}
}