Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run method before suite in TestNG via XML

Tags:

xml

testng

I have an XML TestNG suite:

<suite name="mySuite" parallel="classes" thread-count="5">
    <test name="myTest">
        <packages>
            <package name="mypack.*"/>
        </packages>
    </test>
</suite>

and I'd like to run a method every time before the suite.

Is it possible to have something like this:

<suite name="mySuite" parallel="classes" thread-count="5">
    <before-suite>...</before-suite> <!-- Here I want to run a single method  -->
    <test name="myTest">
        <packages>
            <package name="mypack.*"/>
        </packages>
    </test>
</suite>

?

like image 876
brand Avatar asked Oct 17 '22 09:10

brand


2 Answers

There are 2 options you have:

  1. Use @BeforeSuite annotation on your method which you want to run before each suite
  2. Implement the ISuiteListener onStart method, add the implemented listener to your xml in the listeners section.
like image 174
niharika_neo Avatar answered Oct 21 '22 03:10

niharika_neo


As per the documentation of the TestNG (http://testng.org/doc/documentation-main.html), we don't have an option to specify the before suite method in testng.xml.

As an option, you can use before suite annotation as follows to satisfy the requirement.

public class UtilitiesTest {
     @BeforeSuite
     public void init() {
         // Initialize the system before the test suite.
     }
}

If you refer the documentation, you will find there are lots of annotations such as @BeforeTest, @BeforeClass, @BeforeMethod ... might be helpful.

Note: You can place this class anywhere in the test suite.

like image 37
Hiran Perera Avatar answered Oct 21 '22 03:10

Hiran Perera