Category Menu

Categories

Interview Questions Category Menu

Interview Questions

Cheat Sheet Category Menu

Cheat Sheets

TestNG Cheat Sheet


Annotations

Annotations are used to provide additional information or instructions to the testing framework. Annotations are added to methods or classes and influence the behavior of the test execution. In TestNG, it is the set of code that controls how method below them has to be executed. TestNG provides various annotations to control test execution, test configuration, and test reporting.

Commonly used TestNG annotations:

  • @Test: This annotation is used to mark a method as a test method. TestNG executes methods annotated with @Test during test execution.
  • @BeforeSuite: This annotation is used to specify a setup method that runs before all tests in a suite.
  • @AfterSuite: This annotation is used to specify a teardown method that runs after all tests in a suite.
  • @BeforeClass: This annotation is used to specify a setup method that runs before all test methods within a class.
  • @AfterClass: This annotation is used to specify teardown method that runs after all test methods within a class.
  • @BeforeMethod: This annotation is used to specify a setup method that runs before each test method.
  • @AfterMethod: This annotation is used to specify a teardown method that runs after each test method.
  • @DataProvider: This annotation is used to specify a method that provides test data to test methods.
  • @Parameters: This annotation is used to specify parameters for a test method.
  • @Listeners: This annotation is used to specify a listener class that implements TestNG listener interfaces to perform additional actions during test execution.

Annotation Hierarchy

  1. @BeforeSuite
  2. @BeforeTest
  3. @BeforeClass
  4. @BeforeMethod
  5. @Test
  6. @AfterMethod
  7. @AfterClass
  8. @AfterTest
  9. @AfterSuite

Annotation Attributes

  • description: @Test(description = "some description")
  • priority: @Test(priority = 1)
  • enabled: @Test(enabled = true)
  • timeOut: @Test(timeOut = 5000)
  • dependsOnMethods: @Test(dependsOnMethods = {"method1"})
  • groups: @Test(groups = {"group1", "group2"})
  • invocationCount: @Test(invocationCount = 5)

Hard Assertion

A hard assert is the default assertion mechanism in TestNG. When a hard assert statement fails, it immediately stops the execution of the current test method and moves on to the next test method.


Java
Copy
import org.testng.Assert;
import org.testng.annotations.Test;
public class HardAssertions {
    String str;
    @Test
    public void assertionMethod()
    {
        Assert.assertEquals((2 + 2), 4);
        Assert.assertNotEquals(5 , 4);

        Assert.assertTrue(2 > 0);
        Assert.assertFalse(2 < 0);
        
        Assert.assertNull(str);
        str = "test";
        Assert.assertNotNull(str, "Assertion failed");
    }
}

Commonly used assertion methods in TestNG

  • assertEquals(expected value, actual value): Checks if the expected value is equal to the actual value.
  • assertNotEquals(expected, actual): Checks if the expected value is not equal to the actual value.
  • assertTrue(condition): Checks if the given condition is true.
  • assertFalse(condition): Checks if the given condition is false.
  • assertNull(object): Checks if the object is null.
  • assertNotNull(object): Checks if the object is not null.

Soft Assertions

Soft Assert is an assertion mechanism that allows you to continue running a test even after an assertion failure occurs. Unlike regular (hard) assertions, which stop the test execution immediately upon failure, soft assertions collect all assertion failures and report them at the end of the test.


Java
Copy
import org.testng.asserts.SoftAssert;
import org.testng.annotations.Test;

public class SoftAssertExample {
    String str;
    @Test
    public void assertionMethod()
    {
        SoftAssert softAssert = new SoftAssert();
        softAssert.assertEquals((2 + 2), 4);
        softAssert.assertNotEquals(5 , 4);

        softAssert.assertTrue(2 > 0);
        softAssert.assertFalse(2 < 0);

        softAssert.assertNull(str);
        str = "test";
        softAssert.assertNotNull(str, "Assertion failed");
        softAssert.assertAll();
    }
}

TestNG XML

TestNG XML is a configuration file used in TestNG. It is used to to define and customize the execution of test suites, test cases, and test methods.

TestNG XML Elements

  • suite: The root element of the XML file, representing a test suite. You can define multiple test suites within a single XML file.
  • test: Represents an individual test case or a set of test cases. You can define multiple test elements within a suite.
  • classes: Specifies the test classes to be included in a test. You can specify one or more classes as child elements.
  • methods: Specifies individual test methods to run. You can include or exclude specific methods.
  • groups: Defines groups of test methods. You can specify which groups to include or exclude.
  • packages: Specifies packages to be included in the test run.
  • parameter: Allows you to pass parameters to test methods. You can define parameters with their names and values.
  • listeners: Specifies listener classes for test events, such as test start, test failure, or test success. Listeners can be used for custom reporting or logging.
  • parallel: Configures parallel execution of tests at the suite, test, class, or method level.
  • suite-files: Allows you to include other TestNG XML files within the current suite.
  • include and exclude: Used within various elements to include or exclude specific test classes, test methods, or groups.

listeners

  • IAnnotationTransformer : Annotation Transformers are used to updated the annotation attribute in the run time. This method will be invoked by TestNG to modify any TestNG annotation read from your test classes.
  • IHookable : Using IHookable listener TestNG allows you to override and possibly skip the invocation of test methods. If a test class implements this interface, its run() method will be invoked instead of each @Test method found within.
  • IInvokedMethodListener : It gets invoked before and after a method is invoked by TestNG. This listener will be invoked for configuration and test methods irrespective of whether they pass/fail or get skipped.
  • IMethodInterceptor : It is used to alter the list of test methods that TestNG is about to run. An instance of this class will be invoked right before TestNG starts invoking test methods.
  • IReporter: IT is used for customized report. It implements a method called generateReport() which is invoked when all the suites of TestNG are executed.
  • ISuiteListener: This will run at a suite level. It has got two methods, onStart invoked before the suite execution starts and onFinish invoked after the suite execution finished.
  • ITestListener: This is one of the commonly and frequently used listener in TestNG. ITestListener listens to the events and the class implements this listener overrides it's methods and execute it accordingly.