TestNG基础教程
- 一、常用断言
- 二、执行顺序
- 三、依赖测试
- 四、参数化测试
- 1、通过dataProvider实现
- 2、通过xml配置(这里是直接跑xml)
- 五、testng.xml常用配置方式
- 1、分组维度控制
- 2、类维度配置
- 3、包维度配置
- 六、TestNG并发测试
- 1、通过注解来实现
- 2、通过xml来实现
- 七、异常测试
- 八、忽略测试
- 九、软断言
一、常用断言
二、执行顺序
/*** 执行顺序* beforeSuite - beforeTest - beforeClass -beforeMethod - * test - afterMethod -afterClass - afterTest - afterSuite*/
public class ExecutionOrder {@BeforeSuitepublic void beforeSuite(){System.out.println("beforeSuite");}@BeforeTestpublic void beforeTest(){System.out.println("beforeTest");}@BeforeClasspublic void beforeClass(){System.out.println("beforeClass");}@BeforeMethodpublic void beforeMethod(){System.out.println("beforeMethod");}@Test(enabled = false)public void test1(){System.out.println("test1");}@Testpublic void test2(){System.out.println("test2");}@AfterMethodpublic void afterMethod(){System.out.println("afterMethod");}@AfterClasspublic void afterClass(){System.out.println("afterClass");}@AfterTestpublic void afterTest(){System.out.println("afterTest");}@AfterSuitepublic void afterSuite(){System.out.println("afterSuite");}
}
三、依赖测试
import org.testng.Assert;
import org.testng.annotations.Test;public class DependOnMethods {@Testpublic void start() {System.out.println("start");Assert.assertEquals(1,2);}@Test(dependsOnMethods = "start")public void test1() {System.out.println("test1");}@Test(dependsOnMethods = "test1")public void test2() {System.out.println("test2");}
}
四、参数化测试
1、通过dataProvider实现
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;public class DataProviderTest {@DataProvider (name = "myData")public Object[][] data() {Object[][] datas = null;return datas = new Object[][]{{"package1", 1, 2, 3},{"package2", 1, 1, 1},};}@Test(dataProvider = "myData")public void test(String name, int a, int b, int c) {System.out.println(name + "的装配数量是:" + a + "," + b + "," + c);}
}@Test(dataProvider = "methodData")public void test1(String name,int age){System.out.println("test111方法 name="+name+";age="+age);}@Test(dataProvider = "methodData")public void test2(String name,int age){System.out.println("test222方法 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",50},{"zhaoliu",60}};}return result;}
2、通过xml配置(这里是直接跑xml)
<?xml version="1.0" encoding="UTF-8" ?><suite name="parameter"><test name="param"><classes><parameter name="name" value="zhangsan"/><parameter name="age" value="10"/><class name="com.course.testng.paramter.ParamterTest"/></classes></test></suite>
五、testng.xml常用配置方式
- 配置需要结合代码和xml
1、分组维度控制
- 方法1
public class Groups {@Test(groups = "group1")public void test1() {System.out.println("test1");}@Test(groups = {"group1","group2"})public void test2() {System.out.println("test2");}@Test(groups = "group1")public void test3() {System.out.println("test3");}
}
<?xml version="1.0" encoding="UTF-8" ?><suite name="suitename"><test name="runAll"><classes><class name="com.course.testng.groups.group1"/><class name="com.course.testng.groups.group2"/></classes></test><test name="onlyRunOne"><groups><run><include name="group1"/></run></groups></test>
- 方法2:只写代码也能实现,就是不够灵活
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;public class GroupsOnMethod {@Test(groups = "server")public void test1(){System.out.println("这是服务端组的测试方法11111");}@Test(groups = "server")public void test2(){System.out.println("这是服务端组的测试方法2222");}@Test(groups = "client")public void test3(){System.out.println("这是客户端组的测试方法33333");}@Test(groups = "client")public void test4(){System.out.println("这是客户端组的测试方法4444");}@BeforeGroups("server")public void beforeGroupsOnServer(){System.out.println("这是服务端组运行之前运行的方法");}@AfterGroups("server")public void afterGroupsOnServer(){System.out.println("这是服务端组运行之后运行的方法!!!!!");}@BeforeGroups("client")public void beforeGroupsOnClient(){System.out.println("这是客户端组运行之前运行的方法");}@AfterGroups("client")public void afterGroupsOnClient(){System.out.println("这是客户端组运行之后运行的方法!!!!!");}
}
2、类维度配置
<classes><class name="examples.packageA.ClassATest"><methods><include name ="testCaseA01"></include><exclude name="testCaseA02"></exclude></methods></class>
</classes>
3、包维度配置
<packages><package name="examples.packageA"/><package name="examples.packageB"/>
</packages>
六、TestNG并发测试
1、通过注解来实现
/*** 并发测试(多个以上需要在xml文件配置)*/
public class ConcurrencyDemo {//单个的可以在这里测试(2个线程运行12次)@Test(invocationCount = 12,timeOut = 2000,threadPoolSize = 2)public void test1(){long id = Thread.currentThread().getId();System.out.println("test1的线程"+id+"正在进行计算");}@Test(invocationCount = 12,timeOut = 2000,threadPoolSize = 2)public void test2(){long id = Thread.currentThread().getId();System.out.println("test2的线程"+id+"正在进行计算");}
}
2、通过xml来实现
七、异常测试
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();}
}
import org.testng.annotations.Test;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);}
}
八、忽略测试
import org.testng.annotations.Test;public class IgnoreTest {@Testpublic void ignore1(){System.out.println("ignore1 执行!");}@Test(enabled = false)public void ignore2(){System.out.println("ignore2 执行");}@Test(enabled = true)public void ignore3(){System.out.println("ignore3 执行");}
}
九、软断言
- SoftAssert的特点:
如果一个断言失败,会继续执行这个断言下的其他语句或者断言
也就是一个用例有多个断言,失败了其中一个,不影响其他断言的运行
不要忘记调用
assertAll()在该用例的最后一个断言后面。
/*** 软断言:一个错误了也不影响后面用例的执行*/
public class SoftAssertDemo {@Testpublic void test() {SoftAssert softAssert = new SoftAssert();softAssert.assertEquals(1, 2);softAssert.assertEquals(2, 1);softAssert.assertEquals(3, 1);// 最后一句一定要加哦!softAssert.assertAll();}
}