TestNG - Basic Annotations - BeforeSuite

@BeforeSuite annotated method will be run before the execution of all the test cases defined in the folder or inside a TestNG suite. For example, this annotation is used when you have separate URLs to test all your test cases. Environment variables can be set in a @BeforeSuite annotated method. Next, before executing all the test cases, you need to first load all the environment variables for your framework, and then start executing the test cases.
The following is a list of attributes supported by the @BeforeSuite annotation:
alwaysRun
For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to.
For after methods (afterSuite, afterClass, ...): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.
dependsOnGroups
The list of groups this method depends on.
dependsOnMethods
The list of methods this method depends on.
enabled
Whether methods on this class/method are enabled.
groups
The list of groups this class/method belongs to.
inheritGroups
If true, this method will belong to groups specified in the @Test annotation at the class level.
onlyForGroups
Only for @BeforeMethod and @AfterMethod. If specified, then this setup/teardown method will only be invoked if the corresponding test method belongs to one of the listed groups.
Create a java class to be tested, say, MessageUtil.java in /work/testng/src.
/*
* This class prints the given message on console.
*/

public class MessageUtil {

   private String message;

   //Constructor
   //@param message to be printed
   public MessageUtil(String message) {
      this.message = message;
   }

   // prints the message
   public String printMessage() {
      System.out.println(message);
      return message;
   }
}
Create a java test class, say, TestAnnotationBeforeSuite.java in /work/testng/src.
Create a java test class, say, TestAnnotationBeforeSuite.java in /work/testng/src.
Add a test method testMethod() to your test class.
Add a test method testMethod() to your test class.
Add an Annotation @Test to method testMethod().
Add an Annotation @Test to method testMethod().
Add a method beforeSuite to the test class with annotation @BeforeSuite
Add a method beforeSuite to the test class with annotation @BeforeSuite
Implement the test condition and check the behaviour of @BeforeSuite annotation.
Implement the test condition and check the behaviour of @BeforeSuite annotation.
Following are the TestAnnotationBeforeSuite.java contents:
  import org.testng.Assert;
  import org.testng.annotations.Test;
  import org.testng.annotations.BeforeSuite;

  public class TestAnnotationBeforeSuite {
    MessageUtil messageUtil = new MessageUtil("Test method");
    @BeforeSuite
    public void beforeSuite(){
      System.out.println("Before Suite method");
    }
    @Test
    public void testMethod(){
      Assert.assertEquals("Test method", messageUtil.printMessage());
    }
  }
Next, let's create testng.xml file in /work/testng/src, to execute test case(s). This file captures your entire testing in XML. This file makes it easy to describe all your test suites and their parameters in one file, which you can check in your code repository or e-mail to coworkers. It also makes it easy to extract subsets of your tests or split several runtime configurations (e.g., testngdatabase.xml would run only tests that exercise your database).
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
  <suite name="Suite">
    <test thread-count="5" name="Test">
      <classes>
        <class name="TestAnnotationBeforeSuite"/>
      </classes>
    </test> <!-- Test -->
  </suite> <!-- Suite -->
Compile the test case using javac.
/work/testng/src$ javac TestAnnotationBeforeSuite.java MessageUtil.java

Now, run the testng.xml, which will run the test case defined in <test> tag. As you can see the @BeforeSuite is called before all other test cases.
/work/testng/src$ java org.testng.TestNG testng.xml

Verify the output.
  Before Suite method
  Test method

  ===============================================
  Suite
  Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
  ===============================================


    38 Lectures 
     4.5 hours 

 Lets Kode It

    15 Lectures 
     1.5 hours 

 Quaatso Learning

    28 Lectures 
     3 hours 

 Dezlearn Education
 Print
 Add Notes
 Bookmark this page