TestNg - Test Automation Framework
Agenda
To understand TestNG framework and explore different concepts that would help to make our testing framework more robust.
Test run with @Test annotation
- you can simple run your test with @Test annotation
@Test
public void testHelloWorld(){
System.out.println("HelloWorld");
}
@Test
public void testBye(){
System.out.println("test bye");
}
output would like
Creation of testng.xml
- allows to run tests via suite file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
<test name="TestNGBasics">
<classes>
<class name="android.AndroidTest"/>
<class name="browser.BrowserTest"/>
<class name="com.basics.day1.HelloWorld"/>
<class name="ios.IOSTest"/>
</classes>
</test>
</suite>
Note: under test tag, your classes can be defined.
Excluding a test case
provide the method name which need to be excluded
<test name="Beneficiary">
<classes>
<class name="beneficiaries.Beneficiary">
<methods>
<exclude name="deleteBeneficiary"></exclude>
</methods>
</class>
</classes>
</test>
Including a test case
provide the method name which need to included, remaining all methods in that class will be excluded
<test name="Contributions">
<classes>
<class name="contribution.Contribution">
<methods>
<include name="addContribution"/>
</methods>
</class>
</classes>
</test>
we can have a regex as well like below, which will run all add scenarios
<test name="Contributions">
<classes>
<class name="contribution.Contribution">
<methods>
<include name="add.*"/>
</methods>
</class>
</classes>
</test>
Run test cases under package
Runs all the tests under that package
<test name="Beneficiary">
<packages>
<package name="contribution"></package>
</packages>
</test>
Annotations
Like the name implies we have different annotations
- BeforeSuite --> runs before suite
- AfterSuite --> runs after suite
- BeforeTest --> runs before every test tag
- AfterTest --> run after every test tag
- BeforeClass --> runs before class
- AfterClass --> runs after class
- BeforeMethod --> runs before every method
- AfterMethod --> runs after every method
package beneficiaries;
import org.testng.annotations.*;
public class Beneficiary {
@BeforeSuite
public void runBeforeSuite(){
System.out.println("run before suite");
}
@BeforeTest
public void runBeforeTest(){
System.out.println("run before test tag");
}
@BeforeClass
public void runBeforeClass(){
System.out.println("run before class");
}
@BeforeMethod
public void runBeforeMethod(){
System.out.println("run before method");
}
@Test
public void addBeneficiary(){
System.out.println("Adding beneficiary");
}
@Test
public void deleteBeneficiary(){
System.out.println("deleting beneficiary");
}
@Test
public void updatingBeneficiary(){
System.out.println("updating beneficiary");
}
@AfterSuite
public void runAfterSuite(){
System.out.println("run after suite");
}
@AfterTest
public void runAfterTest(){
System.out.println("run after test tag");
}
@AfterClass
public void runAfterClass(){
System.out.println("run after class");
}
@AfterMethod
public void runAfterMethod(){
System.out.println("run after method");
}
}
output
run before suite
run before test tag
run before class
run before method
Adding beneficiary
run after method
run before method
deleting beneficiary
run after method
run before method
updating beneficiary
run after method
run after class
run after test tag
run after suite
Grouping your testcases
- you can mark your test cases as a group, so that group can be executed via testng.xml
@Test(groups = {"smoke"})
public void addBeneficiary(){
System.out.println("Adding beneficiary");
}
this group need to be included in test suite file
<suite name="All Test Suite">
<test name="Beneficiary">
<groups>
<run>
<include name="smoke"/>
</run>
</groups>
<packages>
<package name="contribution"/>
<package name="beneficiaries"/>
<package name="enrollments"/>
<package name="notifications"/>
</packages>
</test>
</suite>
in all the packages, it will execute smoke test cases :-)
Attributes
depends on methods
As the name suggests, we let testng know that, to executes few methods before running actual test
@Test(dependsOnMethods = {"addBeneficiary", "updatingBeneficiary"})
public void deleteBeneficiary(){
System.out.println("deleting beneficiary");
}
enable or disable test case
as name implies, it enables your test case by setting it to true, if not false.
@Test(enabled = false)
public void deadContribution(){
System.out.println("dead contribution");
}
timeout
we can explicitly define timeout on a test case, where we expect its timeouts might happen
Parameterize Testng.xml
@Parameters({"URL", "TOKEN"})
@Test(groups = {"smoke"})
public void activeNotification(String url, String token){
System.out.println("drop an email for active notification");
System.out.println("printing parameter : "+url);
System.out.println("printing parameter : "+token);
}
suite level
<suite name="All Test Suite">
<parameter name="URL" value="google.com"/>
<parameter name="TOKEN" value="ab34yux"/>
<test name="Beneficiary">
<classes>
<class name="beneficiaries.Beneficiary">
<methods>
<exclude name="deleteBeneficiary"/>
</methods>
</class>
</classes>
</test>
parameter is available across all tests.
Note: we can even define parameters at test suite level as well.
Data Provider
- Used to parameterize tests
public class TestData {
@DataProvider(name="getData")
public Object[][] getData(){
return new Object[][]
{
{"one"},{"two"}
};
}
}
- This class can be used in test and can be parameterized for content one and two
@Test(dataProvider = "getData", dataProviderClass = TestData.class)
public void testDragAndDrop(String iteration) throws IOException, InterruptedException {
Capture Screenshot on test failures (Listeners)
- Here we will make use of ITestListener and implement onTestFailure method
- Created a Listeners class
public class Listeners implements ITestListener {
@Override
public void onTestFailure(ITestResult result) {
String name = result.getName();
try {
Utility.getScreenshot(name);
} catch (IOException e) {
e.printStackTrace();
}
}
}
- Sample code to create a screenshot
public static void getScreenshot(String name) throws IOException {
File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File(System.getProperty("user.dir")+"\\"+name+".png"));
}
- We have to let suite file aware of the listener that we created
<suite name="All Test Suite">
<listeners>
<listener class-name="org.mobile.listeners.Listeners"/>
</listeners>
<test verbose="2" preserve-order="true" name="AppiumTestNGFramework">
<classes>
<class name="org.mobile.DragAndDrop"/>
</classes>
</test>
</suite>
Run tests Parallelly
you can execute your tests parallelly by tests, classes, methods
<suite name="All Test Suite" parallel="tests" thread-count="5">
Resources
- Official documentation can be found here - TestNG
- you can refer to my github project for more details - itsvinayr/TestNGBasics: To understand basic concepts in TestNG framework that helps us to build a test framework. (github.com)
Comments
Post a Comment