加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – TestNG – @AfterMethod的优先级

发布时间:2020-12-15 04:33:49 所属栏目:Java 来源:网络整理
导读:是否可以按特定顺序调用@AfterMethod方法? 我有一个示例代码: public class PriorityTest {@BeforeClass(alwaysRun = true)public void setUp() throws Exception { System.out.println("BeforeClass PriorityTest.java");}@Testpublic void defaultPriori
是否可以按特定顺序调用@AfterMethod方法?
我有一个示例代码:

public class PriorityTest {

@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
    System.out.println("BeforeClass PriorityTest.java");
}

@Test
public void defaultPriority(){
    System.out.println("default");
}
@Test (priority = 3)
public void t1(){
    System.out.println("t1");
}
@Test (priority = 2)
public void t2(){
    System.out.println("t2");
}
@Test (priority = 1)
public void t3(){
    System.out.println("t3");
}
@Test (priority = -1)
public void t_1(){
    System.out.println("t -1");
}

@AfterMethod
public void after2(){
    System.out.println("after2");
}
@AfterMethod
public void after1(){
    System.out.println("after1");
}

}

@Test的优先级完美.我想对@AfterMethod做同样的事情,但是当我编写代码@AfterMethod(priority = 1)时,它是编译错误.当我没有优先权时,总是按字母顺序排列(只有方法名称很重要).
这是输出:

BeforeClass PriorityTest.java
t -1
after1
after2
默认
after1
after2
T3
after1
after2
T2
after1
after2
T1
after1
after2

是否有可能按特定顺序调用该方法(例如特殊参数或注释)?

PS.我知道我可以编写一个AfterMethod,然后按特定的顺序调用方法,但我想到许多AfterMethod注释.

解决方法

@AfterMethod不支持优先级参数.但它有依赖于USEOnMethods和dependsOnGroups可以代替使用.

dependsOnMethods

The list of methods this method depends on. There is no guarantee on the order on which the methods depended upon will be run,but you are guaranteed that all these methods will be run before the test method that contains this annotation is run. Furthermore,if any of these methods was not a SUCCESS,this test method will not be run and will be flagged as a SKIP. If some of these methods have been overloaded,all the overloaded versions will be run.

dependsOnGroups

The list of groups this method depends on. Every method member of one of these groups is guaranteed to have been invoked before this method. Furthermore,this test method will not be run and will be flagged as a SKIP.

在你的情况下,可以使用dependsOnMethods.

@AfterMethod
public void after2(){
    System.out.println("after2");
}
@AfterMethod(dependsOnMethods = "after2")
public void after1(){
    System.out.println("after1");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读