kosmit
kosmit

Reputation: 671

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? I can't use afterGroup annotation.

Upvotes: 2

Views: 3867

Answers (2)

dmytro Minz
dmytro Minz

Reputation: 417

Try dependsOnGroups For exapmle if you want to run method only after group2 tests use this:

@AfterMethod(dependsOnGroups = {"group2"})

The same should works for @Before configurations

Upvotes: 0

juherr
juherr

Reputation: 5740

Replace @AfterMethod by @AfterClass and you will have the expected output.

Upvotes: 2

Related Questions