TestNG is an open-source automation framework for Java. TestNG contains more features than Junit, which makes it a more robust framework. TestNG was created by Cedric Beust and is mostly used by developers and testers.
TestNG provides features like annotations, data-driven testing, test sequencing, and parallel testing which makes tests execute more effectively and efficiently.
The benefits are :
Why use TestNG with selenium?
Installing TestNG
4. Now search on Google Maven Repository and search for TestNG dependency.
5. Download the file and go to File > project structure > modules > dependencies > plus sign in IntelliJ and select the downloaded file.
6. Click on the checkbox and then apply.
Annotations:
1. @Test: This annotation: his annotation is used to indicate that a method is a test method. TestNG will run the methods annotated with @Test.
@Test public void myTestMethod() { // Test logic goes here }Â
2. @BeforeSuite: This annotation indicates that the method will be run before all tests in a suite.
@BeforeSuite public void setUp() { // Setup logic goes here }Â
3. @AfterSuite: This annotation indicates that the method will be run after all tests in a suite.
@AfterSuite public void tearDown() { // Teardown logic goes here }Â
4. @BeforeClass: This annotation indicates that the method will be run before the first test method in the current class.
@BeforeClass public void beforeClass() { // Before class setup logic goes here }Â
5. @AfterClass: This annotation indicates that the method will be run after all the test methods in the current class.
@AfterClass public void afterClass() { // After class teardown logic goes here }Â
6. @BeforeTest: This annotation indicates that the method will be run before any test method belonging to the classes inside the <test> tag is run.
@BeforeTest public void beforeTest() { // Before test setup logic goes here }Â
7. @AfterTest: This annotation indicates that the method will be run after all the test methods belonging to the classes inside the <test> tag have run.
@AfterTest public void afterTest() { // After test teardown logic goes here }Â
These annotations provide a way to control the execution order of your test methods and to set up and tear down resources before and after tests. They are an integral part of the TestNG framework and help in organizing and managing your test suite effectively.
import org.testng.annotations.*;
public class Example {
  @BeforeSuite
  public void beforeSuite(){
    System.out.println(“This is before suite”);
  }
  @BeforeTest
  public void beforeTest(){
    System.out.println(“This is before test”);
  }
  @BeforeClass
  public void beforeClass(){
    System.out.println(“This is before class”);
  }
  @BeforeMethod
  public void beforeMethod(){
    System.out.println(“This is before method”);
  }
  @Test(priority = 1, description = “Test_001_First method description”, enabled = true)
  public void testOne(){
    System.out.println(“This is first test”);
  }
  @Test(priority = 2, description = “Test_002_Second method description”, enabled = true)
  public void testTwo(){
    System.out.println(“This is second test”);
  }
  @AfterMethod
  public void afterMethod(){
    System.out.println(“This is after method”);
  }
  @AfterClass
  public void afterClass(){
    System.out.println(“This is after class”);
  }
  @AfterTest
  public void afterTest(){
    System.out.println(“This is after Test”);
  }
  @AfterSuite
  public void AfterSuite(){
    System.out.println(“This is after suite”);
  }
}
The above class output is given below.
TestNG Assertions
Assert.assertEquals(actual, expected);Â
2. assertNotEquals: Verifies that two values are not equal.
Assert.assertNotEquals(actual, expected);Â
3. assertTrue: Verifies that the given condition is true.
Assert.assertTrue(condition);Â
4. assertFalse: Verifies that the given condition is false.
Assert.assertFalse(condition);Â
5. assertNull: Verifies that the given object reference is null.
Assert.assertNull(object);Â
6. assertNotNull: Verifies that the given object reference is not null.
Assert.assertNotNull(object);Â
7. assertSame: Verifies that two object references refer to the same object.
Assert.assertSame(actual, expected);Â
8. assertNotSame: Verifies that two object references do not refer to the same object.
Assert.assertNotSame(actual, expected);Â
9. fail: Forces a test to fail with a specified message.
Assert.fail(“Test failure message”);Â
These assertions are typically used within test methods annotated with @Test to validate the expected behavior of the code being tested. If an assertion fails, TestNG will mark the test as failed, and the failure details will be reported in the test results.
What is Parameterization in TestNG?
Parameterization in TestNG refers to the ability to run the same test method with different sets of data. This allows you to test a piece of functionality with various inputs or conditions, increasing the coverage and effectiveness of your tests. TestNG provides built-in support for parameterization, making it easy to execute tests with different input values.
Conclusion:
TestNG is a widely used automation framework by testers and developers and is good for reporting and programming. Cross-browser Testing can also be performed. It is better to choose TestNG over Junit.
Diksham works as a QA Automation EngineerQACraft. He is a mechanical engineer with a degree. He has 3+ years of experience in manual and automation testing. Currently, he is upgrading his skills in automation testing by taking on new challenges day by day. In his free time, he loves to travel.
© Copyright 2024 QACraft Pvt. Ltd. All rights reserved.
Contact : +91 9157786796
Error: Contact form not found.
Diksham