Reputation: 125
I was looking for some way to have some @beforeMethod logic executing only for a group of java unit tests (using TestNG ). I found on TestNG documentation:
onlyForGroups: Only for @BeforeMethod and @AfterMethod. If specified, then this setup/teardown method will only be invoked if the corresponding test method belongs to one of the listed groups.
Which seems to be what I'm looking for.
But when I try to use it, it's not implemented in @BeforeMethod and @AfterMethod annotations. I couldn't find find any information on the web about it missing.
Maven dependency :
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.2</version>
<scope>test</scope>
</dependency>
Thanks for the help
Upvotes: 1
Views: 414
Reputation: 8413
Notice the version of TestNG used in the dependency.
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestNgBeforeMethodAnnotation {
@BeforeMethod(onlyForGroups = {"Group1"}, groups = {"Group1"})
public void setupForGroup1()
{
System.out.println("Before Method for Group 1 called");
}
@Test(groups = {"Group1"})
public void testCaseInGroup1()
{
System.out.println("testCaseInGroup1");
}
}
Upvotes: 0