TestNG基础教程
-
- 一、常用断言
- 二、执行顺序
- 三、依赖测试
- 四、参数化测试
-
- 1、通过dataProvider实现
- 2、通过xml配置(这里是直接跑xml)
- 五、testng.xml常用配置方式
-
- 六、TestNG并发测试
-
- 七、异常测试
- 八、忽略测试
- 九、软断言
一、常用断言
二、执行顺序
public class ExecutionOrder {
@BeforeSuite
public void beforeSuite(){
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(enabled = false)
public void test1(){
System.out.println("test1");
}
@Test
public void test2(){
System.out.println("test2");
}
@AfterMethod
public void afterMethod(){
System.out.println("afterMethod");
}
@AfterClass
public void afterClass(){
System.out.println("afterClass");
}
@AfterTest
public void afterTest(){
System.out.println("afterTest");
}
@AfterSuite
public void afterSuite(){
System.out.println("afterSuite");
}
}
三、依赖测试
import org.testng.Assert;
import org.testng.annotations.Test;
public class DependOnMethods {
@Test
public 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)
<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常用配置方式
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");
}
@BeforeGroups("server")
public void beforeGroupsOnServer(){
System.out.println("这是服务端组运行之前运行的方法");
}
@AfterGroups("server")
public void afterGroupsOnServer(){
System.out.println("这是服务端组运行之后运行的方法!!!!!");
}
}
<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、类维度配置
<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、通过注解来实现
public class ConcurrencyDemo {
@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 {
@Test
public 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 {
@Test
public void test() {
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(1, 2);
softAssert.assertEquals(2, 1);
softAssert.assertEquals(3, 1);
softAssert.assertAll();
}
}