Prasad Magre
Prasad Magre

Reputation: 41

How to Get test group name in TestNG

I am new to testNG, I have the below code:

@BeforeMethod
public void getGroup(ITestAnnotation annotation){
    System.out.println("Group Name is --" + annotation.getGroups()) ;
}

@Test(groups = { "MobilSite" })
public void test1(){
    System.out.println("I am Running Mobile Site Test");
}

I want the group name in before method, I tried using ITestAnnotation but when I run the test I am getting the below error

Method getGroup requires 1 parameters but 0 were supplied in the @Configuration annotation.

Can you please help the parameter which I should pass from the XML?

Upvotes: 4

Views: 7305

Answers (3)

Atul Dwivedi
Atul Dwivedi

Reputation: 1462

In case if you want to know the all groups name to which executing @Test method belongs to:

@BeforeMethod
    public void beforeMethod(Method method){
        Test testClass = method.getAnnotation(Test.class);

        for (int i = 0; i < testClass.groups().length; i++) {
            System.out.println(testClass.groups()[i]);
        }
    }

Upvotes: 3

niharika_neo
niharika_neo

Reputation: 8531

Use the reflected method to get the group of the test as below :

@BeforeMethod
public void befMet(Method m){
        Test t = m.getAnnotation(Test.class);
        System.out.println(t.groups()[0]);//or however you want to use it.
}

Upvotes: 6

Garry
Garry

Reputation: 4533

If I understand you correctly, you want to add group to beforeMethod.

@BeforeMethod(groups = {"MobilSite"}, alwaysRun = true)

Upvotes: 0

Related Questions