How to get testgroup name in TestNG

I am new to testNG, I have 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 need the group name in front of the method, I tried using ITestAnnotation

, but when I run the test, I get the following error

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

      

Can you please help the parameter that I have to pass from XML?

+3


source to share


3 answers


Use reflection method to get the test group as shown 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.
}

      

+4


source


If you want to find out the name of all groups to which a method belongs @Test

:



@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]);
        }
    }

      

+1


source


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

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

      

-1


source







All Articles