TestNG测试方法的依赖执行

前言

特定场景下,测试方法A必须在测试方法B已执行完成的情况下再执行,这个时候就需要应用TestNG提供的依赖功能。

正文

1、利用@Test注解属性(dependsOnMethods = {"被依赖的方法名"})来确定依赖关系。

public class TestNGDependency {
    //声明被测试类的对象,全部测试方法都可以引用
    ClassToBeTested test;
    @BeforeClass
    public void beforeClass(){
        //在beforeClass()方法中创建被测试类的对象
        test = new ClassToBeTested();
        System.out.println("beforeClass");
    }
    @AfterClass
    public void afterClass(){
        System.out.println("afterClass");
    }
    @Test(dependsOnMethods = {"testMethod2"})
    public void testMethod1(){
        System.out.println("testMethod1");
    }
    @Test
    public void testMethod2(){
        int result = test.add(2, 6);
        Assert.assertEquals(result, 8);
        System.out.println("testMethod2");
    }
    @Test(dependsOnMethods = {"testMethod1"})
    public void testMethod3(){
        System.out.println("testMethod3");
    }
    @Test
    public void testMethod4(){
        System.out.println("testMethod4");
    }
}

以上代码可以看出,testMethod1()依赖testMethod2(),testMethod3()依赖testMethod1(),testMethod4()是一个没有依赖关系的独立方法。

执行的结果:

TestNG测试方法的依赖执行

testMethod2()先执行,然后testMethod1()、testMethod3(),testMethod4()是示例中唯一独立的,可以执行在任何时候。

方法1是在方法2执行完后执行,但是并不表示紧跟在方法2后,方法2执行后和方法1执行前,任何独立的方法都可以执行。

2、当被依赖的方法执行失败,依赖的方法跳过执行。

public class TestNGDependency {
    //声明被测试类的对象,全部测试方法都可以引用
    ClassToBeTested test;
    @BeforeClass
    public void beforeClass(){
        //在beforeClass()方法中创建被测试类的对象
        test = new ClassToBeTested();
        System.out.println("beforeClass");
    }
    @AfterClass
    public void afterClass(){
        System.out.println("afterClass");
    }
    @Test(dependsOnMethods = {"testMethod2"})
    public void testMethod1(){
        System.out.println("testMethod1");
    }
    @Test
    public void testMethod2(){
        int result = test.add(2, 6);
        Assert.assertEquals(result, 9);
        System.out.println("testMethod2");
    }
    @Test(dependsOnMethods = {"testMethod1"})
    public void testMethod3(){
        System.out.println("testMethod3");
    }
    @Test
    public void testMethod4(){
        System.out.println("testMethod4");
    }
}

执行结果:

TestNG测试方法的依赖执行

被依赖的方法2执行失败,方法1和方法3跳过执行。

因为依赖关系,测试方法跳过执行。如果不想跳过执行,需要加属性 alwaysRun = true

代码如下:

public class TestNGDependency {
    //声明被测试类的对象,全部测试方法都可以引用
    ClassToBeTested test;
    @BeforeClass
    public void beforeClass(){
        //在beforeClass()方法中创建被测试类的对象
        test = new ClassToBeTested();
        System.out.println("beforeClass");
    }
    @AfterClass
    public void afterClass(){
        System.out.println("afterClass");
    }
    @Test(dependsOnMethods = {"testMethod2"}, alwaysRun = true)
    public void testMethod1(){
        System.out.println("testMethod1");
    }
    @Test
    public void testMethod2(){
        int result = test.add(2, 6);
        Assert.assertEquals(result, 9);
        System.out.println("testMethod2");
    }
    @Test(dependsOnMethods = {"testMethod1"})
    public void testMethod3(){
        System.out.println("testMethod3");
    }
    @Test
    public void testMethod4(){
        System.out.println("testMethod4");
    }
}

执行结果:

TestNG测试方法的依赖执行

测试方法2虽然执行失败了,应为方法1加了属性alwaysRun=true,所有没有跳过执行。

总结

1、怎样测试方法具有依赖关系?   dependsOnMethods属性;

2、被依赖的测试方法执行失败了,依赖的测试方法跳过执行;如果不想跳过执行,设置属性alwaysRun=true 。

 

三人行,必有我师焉。