Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestNg skipping @BeforeSuite and @BeforeClass when running in group

Tags:

java

testng

When running TestNg with groups it is skipping @BeforeClass and @BeforeSuite methods. however running without group it execute @BeforeClass and @BeforeSuite

Code :

class TestStructure {

 @BeforeClass
 public void loadData() {
  System.out.println("loaded");
 }

 @BeforeSuite
 public void loadSysData() {
  System.out.println("loaded2");
 }
}


class Test extends TestStructure
{

    @Test(groups={"UAP"})
    public void test1
    {
     System.out.println("Test 2");
    }

}

The testng.xml file is

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="UAP suite" parallel="false" thread-count="1" verbose="1">
   <listeners>
      <listener class-name="com.module.listener.TestNGListener" />
   </listeners>

   <test name="TestSet 1" parallel="false" preserve-order="true">
      <groups>
         <run>
            <include name="UAP" />
         </run>
      </groups>
      <classes>
         <class name="Test" />
      </classes>
   </test>
</suite>

If I remove group from the xml then @BeforeClass and @BeforeSuite are executed. Please help.

like image 977
Hexgear Avatar asked May 18 '17 05:05

Hexgear


1 Answers

There are two options you need to consider in order to resolve this issue.

  1. Add an attribute alwaysRun=true for all your beforeXXX & afterXXX methods. If you want these methods to run irrespective of group being tested
  2. If you would like to associate beforeXXX & afterXXX to only specific set of groups, in that case you need to bind these methods to that group using attribute groups.

Based on your use case, you need update your annotations to either

@BeforeClass(alwaysRun=true) and @BeforeSuite(alwaysRun=true)

or

@BeforeClass(groups={"UAP"}) and @BeforeSuite(groups={"UAP"})

Hope this answers your question.

For further Reference - http://testng.org/doc/documentation-main.html#annotations

like image 157
Patton Avatar answered Nov 04 '22 18:11

Patton