TestNG测试方法的依赖执行

17 篇文章 0 订阅

前言

特定场景下,测试方法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()是一个没有依赖关系的独立方法。

执行的结果:

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");
    }
}

执行结果:

被依赖的方法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");
    }
}

执行结果:

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

总结

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

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

 

三人行,必有我师焉。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
TestNG支持测试用例的依赖关系,即一个测试方法依赖于另外一个测试方法执行结果。如果一个测试方法依赖于另外一个测试方法,那么在运行测试套件时,TestNG会先运行被依赖测试方法,如果该方法执行成功,再运行依赖方法测试方法。这样能够确保测试方法执行顺序和依赖关系。 TestNG提供了两种方式来实现测试用例的依赖: 1. 通过dependsOnMethods属性实现依赖关系 在@Test注解中使用dependsOnMethods属性来指定被依赖测试方法。例如: ``` @Test public void loginTest() { //登录测试逻辑 } @Test(dependsOnMethods = {"loginTest"}) public void searchTest() { //搜索测试逻辑 } ``` 在上面的例子中,searchTest方法依赖于loginTest方法,因此在运行测试套件时,TestNG会先运行loginTest方法,如果该方法执行成功,再运行searchTest方法。 2. 通过dependsOnGroups属性实现依赖关系 在@Test注解中使用dependsOnGroups属性来指定被依赖测试组。例如: ``` @Test(groups = {"login"}) public void loginTest() { //登录测试逻辑 } @Test(groups = {"search"}, dependsOnGroups = {"login"}) public void searchTest() { //搜索测试逻辑 } ``` 在上面的例子中,searchTest方法依赖于一个名为“login”的测试组,而loginTest方法属于该测试组。因此在运行测试套件时,TestNG会先运行loginTest方法,如果该方法执行成功,再运行searchTest方法。 使用dependsOnMethods或dependsOnGroups属性可以方便地实现测试用例的依赖关系,但是需要注意依赖关系的正确性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值