TestNG is a testing framework inspired by JUnit, but it offers additional features like parallel test execution, flexible test grouping, and better reporting.
Use the @Test annotation, like this:
@Test
public void myTestMethod() {
// Test code here
}
@BeforeTest is used to run setup code before any test method in the test class, and @AfterTest is used to run cleanup code after all test methods in the class.
Use the @DataProvider annotation to supply data to a test method, like this:
java
@DataProvider(name = "myData")
public Object[][] testData() {
return new Object[][] { { 1, 2 }, { 3, 4 } };
}
@Test(dataProvider = "myData")
public void myTestMethod(int a, int b) {
// Test code here using a and b
}
Use the parallel attribute in the <suite> tag of your testng.xml file or annotate your test class with @Listeners({ParallelListener.class}) to enable parallel execution.
It specifies that a test method depends on the successful execution of one or more other test methods before it can run.
Use the priority attribute in the @Test annotation to specify the execution order, where lower values execute first.
@Parameters is used to specify parameters for a test method, and you can define these parameters in your testng.xml file.
Use the groups attribute in the @Test annotation to assign a test method to one or more groups, then include or exclude groups in your testng.xml file.
You can use the preserveOrder attribute in the <suite> tag of your testng.xml file to specify that test methods should run in their declared order.
@Listeners is used to add custom listeners to your test class, which can perform actions before or after test methods.
Soft Assert allows you to continue executing test steps even after an assertion fails. Here's an example:
java
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(actual, expected);
// Continue with test steps
softAssert.assertAll(); // This will report all failures at the end
TestNG listeners are interfaces that allow you to customize test execution. Here's an example of a custom listener:
java
public class MyListener implements ITestListener {
// Override listener methods like onTestStart, onTestSuccess, etc.
}
Use the @Test(enabled = false) annotation or the @Test annotation with enabled = true or false to skip or execute a test method.
@DataProvider supplies data to test methods, while @Factory creates instances of test classes, allowing dynamic test creation.
TestNG provides built-in HTML reports. You can also integrate it with tools like ExtentReports or TestNG's IReporter interface for custom reporting.
Add the TestNG dependency in your pom.xml, and then configure your test classes and suites in a testng.xml file.
@BeforeSuite runs setup code before all test methods in a suite, and @AfterSuite runs cleanup code after all test methods.
Define parameters in the <parameter> tag inside <test> or <suite> tags in testng.xml, and reference them using @Parameters in your test class.
Use the java -cp command with the org.testng.TestNG class and specify the testng.xml file as an argument.
<suite name="API Automation Smoke Suite">
<test name="Automation Test Cases">
<classes>
<class name="<nameofthepackage>.payment"/>
</classes>
</test>
</suite>
<suite name="API Automation Smoke Suite">
<test name="Automation Test Cases">
<classes>
<class name="apiautomationeleven.test"/>
<class name="apiautomationeleven.registration"/>
<class name="apiautomationeleven.payment"/>
</classes>
</test>
</suite>
Click Here For Answer
<suite name="API Automation Smoke Suite">
<test name="Automation Test Cases">
<packages>
<package name="apiAutomationeleven" />
</packages>
</test>
</suite>
<suite name="API Automation Smoke Suite">
<test name="Automation Test Cases">
<groups>
<run>
<exclude name="brokenTests" />
<include name="SmokeSuite" />
</run>
</groups>
</test>
</suite>
@Test(description="B_Users:Validate 200 status code for /users GET API",dependsOnGroups="Auth_OAUTH")
<suite name="API Automation Smoke Suite">
<test name="Automation Test Cases">
<dependencies>
<group name="B_User" depends-on="Auth_Oauth" />
</dependencies>
</test>
</suite>
@Parameters({ "first-name" })
@Test
public void testSingleString(String firstName) {
System.out.println("Invoked testString " + firstName);
assert "Cedric".equals(firstName);
}
SCENARIO BASED REAL TIME
Answer: Parallel execution in TestNG can be achieved using the parallel attribute at the suite, test, or method level. Key attributes include methods, tests, instances, and classes.
Answer: Data-driven testing is achieved using the @DataProvider annotation and associating it with the dataProvider attribute in the @Test annotation.
Answer: TestNG allows dependency management using the dependsOnMethods and dependsOnGroups attributes within the @Test annotation.
Answer: Tests can be grouped using the @Test(groups = "group_name") annotation. Execution control involves including or excluding specific groups in the XML suite file.
Answer: Listeners like IInvokedMethodListener and ITestListener allow you to customize test behavior and generate detailed reports. Utilize tools like Extent Reports for enhanced reporting.
Answer: Test parameterization is achieved using the @Parameters annotation in conjunction with the parameter attribute in the @Test annotation.
Answer: A TestNG suite is a collection of test classes. Configuration is done using the XML suite file where you define test classes, groups, and parameters.
Answer: Use the @Test(expectedExceptions = Exception.class) annotation to handle expected exceptions during test execution.
Answer: Soft assertions, provided by the SoftAssert class, allow the execution of subsequent test steps even if an assertion fails, providing a comprehensive test report.
Answer: TestNG and Selenium integration involves creating test scripts using TestNG annotations, allowing better test organization, parallel execution, and reporting.
****
Note:
This session is part of my Long-Term Mentorship Program and End-to-End Automation Course.Find the course link below to explore more and start your journey! If you're preparing for ๐ง๐ฒ๐๐ ๐๐๐๐ผ๐บ๐ฎ๐๐ถ๐ผ๐ป ๐๐ป๐ด๐ถ๐ป๐ฒ๐ฒ๐ฟ (๐ฆ๐๐๐ง) ๐ถ๐ป๐๐ฒ๐ฟ๐๐ถ๐ฒ๐๐, Iโve created a Complete End-to-End Test Automation Course Kit on API+Web+Mobile+Generative AI along with unlimited 1:1 call for doubts, 4 mock interviews and 2 coding session:
๐ฅ Use Code ๐ฆ๐๐๐๐๐ฅ๐ง๐๐ญ๐ฌ to get 10% Discount: https://lnkd.in/giCxnJJ7
********************************************
โ ๐๐ผ๐ป๐ป๐ฒ๐ฐ๐ ๐๐ถ๐๐ต ๐บ๐ฒ ๐ผ๐๐ฒ๐ฟ ๐ฎ ๐ญ:๐ญ ๐ฐ๐ฎ๐น๐น: https://topmate.io/sidharth_shukla/
โ Highest Selling ๐๐ป๐๐ฒ๐ฟ๐๐ถ๐ฒ๐ ๐ค&๐ ๐ฃ๐ฎ๐ฐ๐ธ๐ฎ๐ด๐ฒ ๐ณ๐ผ๐ฟ ๐ง๐ฒ๐๐ ๐๐๐๐ผ๐บ๐ฎ๐๐ถ๐ผ๐ป & ๐ฆ๐๐๐ง: https://topmate.io/sidharth_shukla/605319
โ Generative AI For Software Testing: https://topmate.io/sidharth_shukla/411804
******: ๐ฏ