java - TestNG - @AfterMethod 的优先级

标签 java testng

是否可以按特定顺序调用@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 之后1 之后2 默认 之后1 之后2 t3 之后1 之后2 t2 之后1 之后2 t1 之后1 之后2

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

附言。我知道我可以编写一个 AfterMethod,然后按特定顺序调用方法,但我想到了很多 AfterMethod 注释。

最佳答案

@AfterMethod 不支持priority 参数。但它有 dependsOnMethodsdependsOnGroups 可以代替使用。

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, if any of these methods was not a SUCCESS, 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");
}

关于java - TestNG - @AfterMethod 的优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36712711/

相关文章:

java - 将 JavaFX 与包一起使用会生成 "Missing JavaFX application class"错误

java - JPanel 表单扩展以填充 JScrollpane - 违背了目的

java - 如何收集递归方法的结果

java - 如何在eclipse插件中使用ElementTreeSelectionDialog

java - 解析搜索查询

testing - TestNG - 将 TestNG 工厂中的每个实例作为单独的测试运行

java - testng - 抽象方法错误

java - TestNG ExpectedExceptions 抛出 TestException

java - 如何解决 Can Injected only one of <ITestContext, XmlTest> into a BeforeTest annotated setBrowser 错误

maven-3 - "default-test"在 maven-surefire 插件中代表什么