0

I am trying to run all the test methods irrespective of whether they belong to any group or not.

Below is the Selenium code:

public class BaseClass
{

    @BeforeMethod(onlyForGroups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod()
    public void bmeth()
    {
        System.out.println("Before Method called");
    }

    @AfterMethod(onlyForGroups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod()
    public void ameth()
    {
        System.out.println("After Method called");
    }

}

public class TC_003 extends BaseClass
{

    @Test(groups= {"P1"})
    public void tCase6()
    {
        System.out.println("Inside testcase 6");
    }

    @Test()
    public void tCase9()
    {
        System.out.println("Inside testcase 9");
    }

}

Below is the testng.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test1">
    <classes>
        <class name="testing.TC_003"/>
    </classes>
</test>
</suite>

Actual Output:

Before Method called
Before Method1 called
Inside testcase 6
After Method called
After Method1 called
Before Method called
Inside testcase 9
After Method called

Expected Output:

Before Method1 called
Inside testcase 6
After Method1 called
Before Method called
Inside testcase 9
After Method called
1

This is onlyForGroups description from the javadocs:

Causes this method to be invoked only if the test method belongs to a listed group. It can be used if different setups are needed for different groups. Omitting this or setting it to an empty list will cause this method to run before every test method, regardless of which group it belongs to. Otherwise, this method is only invoked if the test method being invoked belongs to one of the groups listed here.

So this is designed behavior. You could assign some default group to the tests you wouldn't like to be pre-configured with certain methods.

5
  • But if I give onlyForGroups = {""} to the second @BeforeMethod/@AfterMethod above, then it did not invoke the the second @BeforeMethod/@AfterMethod for the second test method. Below is the output: Before Method1 called Inside testcase 6 After Method1 called Inside testcase 9 – Sandesh Sawant Feb 10 at 15:17
  • 1
    Not sure what you mean, but I believe that {""} is not an empty list, {} is. – Alexey R. Feb 10 at 15:30
  • 1
    @SandeshSawant you can run System.out.println(new String[]{""}.length); vs System.out.println(new String[]{}.length); so that the former will show 1 while the latter will show 0. – Alexey R. Feb 10 at 15:35
  • One more observation: If we have certain @BeforeGroups/@AfterGroups methods which belong to a particular group and certain @BeforeGroups/@AfterGroups methods which do not belong to any group, and if we use the above testng.xml file, then none of the @BeforeGroups/@AfterGroups are invoked. Is there any workaround for this? – Sandesh Sawant Feb 10 at 15:56
  • 1
    For the above observation about @BeforeGroups/@AfterGroups methods, the workaround is same as mentioned above, i.e. assign some default group to the tests you wouldn't like to be pre-configured with certain methods. – Sandesh Sawant Feb 11 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.