简体   繁体   English

仅在 testng 中指定的测试方法之后运行带有 AfterMethod 注释的方法

[英]Run method annotated with AfterMethod only after specified Test method in testng


I have this example code:我有这个示例代码:

public class A {

@BeforeTest(groups = "group1")
public void beforeTest() {
    System.out.println("Before test");
}

@BeforeMethod(groups = "group1")
public void beforeMethod() {
    System.out.println("Before method");
}

@Test(groups = { "group1" })
public void test1() {
    System.out.println("Test1");
}

@Test(groups = {"group2"})
public void test2() {
    System.out.println("Test2");
}

@AfterMethod(groups = { "group2" }, alwaysRun = false)
public void afterMethod() {
    System.out.println("After method");
}

@AfterTest(groups = "grupa1")
public void afterTest() {
    System.out.println("AfterTest");
}
}

And the output is:输出是:

Before test
Before method
Test1
After method
Before method
Test2
After method
AfterTest

But I would like to recieve something like this:但我想收到这样的东西:

Before test
Before method
Test1
Before method
Test2
After method
AfterTest

Is there any option to call afterMethod method only after second test method?是否有任何选项可以仅在第二个测试方法之后调用 afterMethod 方法? I can't use afterGroup annotation.我不能使用 afterGroup 注释。

@AfterMethod替换为@AfterClass ,您将获得预期的输出。

Try dependsOnGroups For exapmle if you want to run method only after group2 tests use this:尝试dependsOnGroups例如,如果您只想在 group2 测试之后运行方法,请使用此方法:

@AfterMethod(dependsOnGroups = {"group2"})

The same should works for @Before configurations同样适用于@Before配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM