部分基础略
BeforeMethod
BeforeClass
BeforeSuite
忽略测试 @Test(enabled = true)
组测试中的方法分组
组测试中的类分组
@Test(groups = "stu") //配置在类上
public class GroupsOnClass1 {
public void stu1(){
System.out.println("GroupsOnClass1 中 stu1 运行");
}
public void stu2(){
System.out.println("GroupsOnClass1 中 stu2 运行");
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<suite name="suitename">
<test name="runAll">
<classes>
<class name="groups.GroupsOnClass1"/>
<class name="groups.GroupsOnClass2"/>
<class name="groups.GroupsOnClass3"/>
</classes>
</test>
<test name="onlyRunstu">
<groups> //只跑设定的类
<run>
<include name="stu"/>
</run>
</groups>
<classes>
<class name="groups.GroupsOnClass1"/>
<class name="groups.GroupsOnClass2"/>
<class name="groups.GroupsOnClass3"/>
</classes>
</test>
</suite>
异常测试
import org.testng.annotations.Test;
public class ExpectedException {
/**
* 期望结果为某一个异常
*/
// 测试结果失败的异常测试
@Test(expectedExceptions = RuntimeException.class)
public void RunTimeExceptionFailed(){
System.out.println("这是异常测试");
}
// 成功的异常测试
@Test(expectedExceptions = RuntimeException.class)
public void RuntimeExceptionSuccess(){
System.out.println("这是异常测试");
throw new RuntimeException();
}
}
依赖测试
public class DependTest {
@Test
public void test1() {//场景 前置条件,如登陆
System.out.println("test1");
// throw new RuntimeException(); //当抛出异常时,test2 "Test ignored."
}
@Test(dependsOnMethods = {"test1"})
public void test2() {
System.out.println("test2 after test1");
}
}
参数化 xml文件参数化
public class ParamterTest {
@Test
@Parameters({"name", "age"})
public void paramTest1(String name, int age) {
System.out.println("name = " + name + "; age = " + age);
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<suite name="param">
<test name="param">
<classes>
<parameter name="name" value="zhangsan"/>
<parameter name="age" value="10"/>
<class name="videolearn.parameter.ParamterTest"/>
</classes>
</test>
</suite>
参数化 DataProvider参数化
第一种
@Test(dataProvider = "draftGetUrl", priority = 2)
public void testDraftGetUrl(DraftGetUrlRequest request, DraftGetUrlResponse expected)
@DataProvider(name = "draftGetUrl")
public Object[][] createDataDraftGetUrl() {
return new Object[][]{
{new DraftGetUrlRequest("1", "123456", UUID.randomUUID().toString(), "123456", PROJECT_SN),
new DraftGetUrlResponse(1, 0, null)},
{new DraftGetUrlRequest("1", "123456", UUID.randomUUID().toString(), "123456", "not_exist"),
new DraftGetUrlResponse(1, 0, null)},
};
}
第二种 通过反射 类名 传递不同参数
import java.lang.reflect.Method;
public class DataProviderTest {
@Test(dataProvider = "methodData")
public void test1(String name, int age) {
System.out.println(" test1 方法 name = " + name + "; age = " + age);
}
@Test(dataProvider = "methodData")
public void test2(String name, int age) {
System.out.println(" test2 方法 name = " + name + "; age = " + age);
}
@DataProvider(name = "methodData")
public Object[][] methodDataTest(Method method) {
Object[][] result = null;
if (method.getName().equals("test1")) {
result = new Object[][]{
{"zhangsan", 20},
{"lisi", 25}
};
} else if (method.getName().equals("test2")) {
result = new Object[][]{
{"wangwu", 30},
{"zhaoliu", 35}
};
}
return result;
}
}
多线程测试
<?xml version="1.0" encoding="utf-8" ?>
<suite name="thread" parallel="classes" thread-count="2">
<!--
tests级别: 不同的test tag下的用例可以在不同的线程下执行;相同的tag只能在同一个线程执行
classs级别:相同的class tag下的用例在同一个线程中执行
methods级别:所有用例都可以在不同的线程下去执行
thread-count:最大并发线程数
xml文件配置 不能指定线程池,只有方法上才可以指定线程池
-->
<test name="demo1">
<classes name="name1">
<class name="learn.multiThread.MultiThreadOnXml"/>
</classes>
</test>
<test name="demo2">
<classes name="name2">
<class name="learn.multiThread.MultiThreadOnXml"/>
</classes>
</test>
</suite>
超时测试
public class TimeOutTest {
@Test(timeOut = 3000) //单位毫秒
public void testSuccess() throws InterruptedException {
Thread.sleep(2000);
}
@Test(timeOut = 2000)
public void testFailed() throws InterruptedException {
Thread.sleep(3000);
}
}