01 Apr
01Apr

TB-1: How to exclude the test case in TestNG class file?

Example:          @Test (enabled = false) public void testCase_1() {
             //write your testCase code
         }

TB-2: What is TestNG?

TestNG is a automation testing framework designed to simplify a broad range of testing needs, from unit testing to integration testing.

TB-3: What are the advantages of TestNG? 

  • By using TestNG, we can run multiple testcases together as a test suite
  • It allows to execute parallel methods
  • It allows to set the order of execution of test cases by mentioning the priority
  • It allows grouping the test methods into different test groups
  • It supports the parameterization of testcases by using @Parameters annotation
  • It allows data driven testing by using @DataProvider annotation.
  • Validation becomes easy by using Assert class methods
  • Detailed html reports available to check the test execution results

TB-4: What are the annotations available in TestNG?  

@BeforeTest
@AfterTest
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
@BeforeSuite
@AfterSuite
@BeforeGroups
@AfterGroups
@Test

TB-5: What is the importance of testng.xml file? 

In a Selenium TestNG project, we use testng.xml file to configure the complete test suite in a single file. Some of the features are as follows.

  • testng.xml file allows to include or exclude the execution of test methods and test groups
  • It allows to pass parameters to the test cases
  • Allows to add group dependencies
  • Allows to add priorities to the test cases
  • Allows to configure parallel execution of test cases
  • Allows to parameterize the test cases

TB-6: What is TestNG Assert and list out common TestNG Assertions?  

TestNG Asserts help us to verify the condition of the test in the middle of the test run. Based on the TestNG Assertions, we will consider a successful test only if it is completed the test run without throwing any exception.

Some of the Asserts are as follows:

  • assertEqual(String actual,String expected)
  • assertEqual(String actual,String expected, String message)
  • assertEquals(boolean actual,boolean expected)
  • assertTrue(condition)
  • assertTrue(condition, message)
  • assertFalse(condition)
  • assertFalse(condition, message)

TB-7: What is Soft Assert in TestNG? 

Soft Assert collects errors during @Test. Soft Assert does not throw an exception when an assert fails and would continue with the next step after the assert statement.

If there is any exception and you want to throw it then you need to use assertAll() method as a last statement in the @Test and test suite again continue with next @Test as it is.

TB-8: What is Hard Assert in TestNG? 

Hard Assert throws an AssertException immediately when an assert statement fails and test suite continues with next @Test 

TB-9: How to set test case priority in TestNG? 

We use priority attribute to the @Test annotations. In case priority is not set then the test scripts execute in alphabetical order.

package testNGScripts;
import org.testng.annotations.Test;

public class TestNG_1 {       

@Test (priority = 1)  
public void Testcase_1() {       
    System.out.println("Test case one");  
}   
@Test (priority = 2)  
public void Testcase_2() {       
     System.out.println("Test case two");  
}
}

TB-10: Generating Test Reports with TestNG

TestNG provides built-in support for generating test reports, which can be useful for analyzing test results and identifying issues. TestNG generates HTML reports by default, but you can also integrate it with other reporting tools like ExtentReports or ReportNG for more advanced reporting capabilities. Here's how to generate test reports with TestNG:

Using TestNG Default HTML Reports:

Run TestNG Tests: Execute your TestNG tests either through an IDE or using the TestNG command-line interface.Generate Reports: After the tests are executed, TestNG automatically generates an HTML report in the test-output directory of your project.

Integrating with ExtentReports:

ExtentReports is a popular reporting library for generating interactive HTML reports with rich visualizations.Add Dependencies: Include ExtentReports dependency in your project's pom.xml file if you're using Maven or add the JAR files to your project if you're not using Maven.<dependency>

<groupId>com.aventstack</groupId>

<artifactId>extentreports</artifactId>

<version>4.1.5</version>

</dependency>

Create ExtentReports Instance: Create an instance of ExtentReports class in your test class.

ExtentReports extent = new ExtentReports();

Start Test: Start a test with the createTest method, specifying the test name and description.

ExtentTest test = extent.createTest("TestName", "Description");

Log Test Status: Log the status of the test using the pass, fail, or skip methods.

test.pass("Test Passed");

test.fail("Test Failed");

test.skip("Test Skipped");

Generate Report: After all tests are executed, call the flush method to generate the ExtentReports HTML

 report.extent.flush(); 

TB-11: Reading Test Data from Excel or CSV Files

Reading test data from Excel or CSV files is a common practice in test automation. You can use libraries like Apache POI for Excel files and OpenCSV for CSV files in Java. Here's how you can read test data from Excel and CSV files in TestNG:

Reading Test Data from Excel Files (using Apache POI):

Add Dependencies: Include Apache POI dependencies in your project's pom.xml file if you're using Maven.<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>5.1.0</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>5.1.0</version>

</dependency>

Create ExcelReader Class: Implement a class to read data from Excel files.import org.apache.poi.ss.usermodel.*;public class ExcelReader {

public Object[][] readTestData(String filePath, String sheetName) throws Exception {

Workbook workbook = WorkbookFactory.create(new File(filePath));

Sheet sheet = workbook.getSheet(sheetName);

int rowCount = sheet.getLastRowNum();

int columnCount = sheet.getRow(0).getLastCellNum();

Object[][] data = new Object[rowCount][columnCount];

for (int i = 0; i < rowCount; i++) {

Row row = sheet.getRow(i + 1);

for (int j = 0; j < columnCount; j++) {

data[i][j] = row.getCell(j).toString();

}

}

return data;

}

}

Use ExcelReader in TestNG Test Class

Use the ExcelReader class to read test data in your TestNG test class.

import org.testng.annotations.DataProvider;

public class YourTestClass {

ExcelReader excelReader = new ExcelReader();

@DataProvider(name = "testdata")

public Object[][] testData() throws Exception {

return excelReader.readTestData("path/to/your/excel/file.xlsx", "Sheet1");

}

@Test(dataProvider = "testdata")

public void loginTest(String username, String password) {

// Test logic using username and password

}

}

Comments
* The email will not be published on the website.