testng Flashcards
TestNG
an easy-to-use testing framework written in Java, designed for unit, functional, and end-to-end integration tests providing many functionalities. Many plugins are integrated with TestNG.
Writing tests with TestNG is very simple
Write the business logic inside a method. Then annotate the method with @Test.
testng.xml
a file that contains the test configuration, and it allows us to organize test classes and define test suites and tests.
testng essential dependencies
import org.testng.Assert;
import org.testng.annotations.Test;
Java - XML Connection in TESTNG
package com.example.tests; import org.testng.Assert; import org.testng.annotations.Test; public class TestClass { @Test // is an annotation to indicate this is a test method public void testA() { Assert.assertTrue(true); } }
Testng documentation:
https://testng.org/doc/documentation-main.html
TestNG supports annotations:
@Test annotated method is considered as a test method …at class level as well. When given at test method level, the one at method level will take precedence. A test method can be disabled by setting @Test(enabled = false).
@BeforeSuite annotated method will run once per test suite before all the tests.
@AfterSuite annotated method will run once per test suite after all the tests.
@BeforeTest annotated method will run once per test before all the test methods.
@AfterTest annotated method will run once per test after all the test methods.
@BeforeClass annotated method will run once per every test class instance before all the test methods.
@AfterClass annotated method will run once per every test class instance after all the test methods.
@BeforeMethod annotated method will run once per every test method instance before all the test methods.
@AfterMethod annotated method will run once per every test method instance after all the test methods.
@BeforeGroup will run once per every test method instance before all the test methods that belong to the given group.
@AfterGroup will run once per every test method instance after all the test methods that belong to the given group.
@Parameter annotation on test method is to pass parameters to test methods.
@DataProvider to create test methods or test classes at runtime with different parameters. TestNG will create multiple test methods at runtime. It can also be added to the test class constructor in conjunction with @Factory to pass parameters to create test class instances at runtime.
@Factory used on a method that returns instances of test classes or on a test class constructor with @DataProvider.
@Listeners is used at test class level to takes the array of classes that implements a plethora of implementations of ITestNGListener like IAlterSuiteListener, IAnnotationTransformer, IMethodInterceptor, IReporter, etc. for different purposes.
order for the execution of annotated methods:
@BeforeSuite @BeforeTest @BeforeClass @BeforeMethod @Test @AfterMethod @AfterClass @AfterTest @AfterSuite
How to control parallelism?
Parallelism and thread count can be set at suite level or test level like below.
Types of parallelism
methods: run test methods in parallel in different threads. All dependent methods will be run in different threads, respecting the priority of tests.
tests: run tags in parallel in separate threads.
classes: run test classes in parallel in separate threads, but test methods in those test classes will run in the same thread.
instances: run instances of test methods/classes in parallel in different threads.
What is the grouping of tests?
TestNG allows to group related tests using tags. TestNG allows having configuration methods that can run before starting or ending of tests belonging to a group.
How to group tests?
Each test can belong to one or more groups. This grouping can be done to test methods or test classes. When giving at both places, the one given at the test method takes precedence. This becomes Partial test grouping, as all the test methods in the class belong to a group, and this method belongs to another.
Parameterisation
Tests can be parameterized using @Parameter. They can be passed from testng.xml. Parameters can be set at suite level or test level. The same parameter can be overridden at the test level as well.
To use parameter in the test method
To use parameter in the test method
@Parameters(“browser”)
@Test
public void testMethod(String browser) {}
@Parameters(“browser”)
@BeforeMethod
public void initMethod(String browser) {}
@DataProvider
for running a test case multiple times by passing different sets of parameters.
Creating @DataProvider method
@DataProvider( name = “createData” )
public Object[][] createTestData() {
Object[][] data = new Object[2][2]; data[0] = new Object[] { "Sam", 21 }; data[1] = new Object[] { "Smith", 22 }; return data; }
If the @DataProvider is not present in the same class than the test method:
public class DataProviderFactory { @DataProvider( name = "createData" ) public static Iterator createTestData() { return list.iterator(); } } public class TestClass { @Test(dataProvider = "createData", dataProviderClass = DataProviderFactory.class) public void testMethod(String name, int age) { } }
to ensure the order and type of the parameters list that is given as an argument to the test method.
//If the test method and data provider are in the same class. @Test(dataProvider = "createData") public void testMethod(String name, int age) {}
//If the test method and data provider are in different classes @Test(dataProvider = "createData", dataProviderClass = MyDataProviderClass.class) public void testMethod(String name, int age) {}
@Factory
creating test classes at runtime.
a @Factory method that creates two instances of the test class
public class TestClass { private String browser; public TestClass(String browser) { this.browser = browser; } @Test public void testMethod() { } @Factory public Object[] createTestClasses() { Object[] data = new Object[2]; data[0] = new TestClass("chrome"); data[1] = new TestClass("firefox"); return data; } }
The default constructor is required when the data provider method is not static.
public class TestClass { private String browser; public TestClass() { } @Factory(dataProvider = "data") public TestClass(String browser) { this.browser = browser; } @Test public void testMethod() { } @DataProvider public Iterator data() { List arr = new ArrayList<>(); arr.add(new Object[] { "firefox" }); arr.add(new Object[] { "chrome" }); return arr.iterator(); } }
if the @DataProvider annotated method is not present in the same class
we need to provide @Factory(dataProviderClass = DataProviderFactory.class, dataProvider = “createData”), and the @DataProvider annotated method needs to be public static.
TestNG listeners
several interfaces that allow us to change the behavior of TestNG at runtime
Programmatically re-running failed tests
import org.testng.IRetryAnalyzer; import org.testng.ITestResult; public class SampleRetryAnalyzer implements IRetryAnalyzer { private int retryCount = 0; private static final int maxRetryCount = 2; @Override public boolean retry(ITestResult result) { if (retryCount < maxRetryCount) { retryCount++; return true; } return false; } }
To avoid adding @Test(retryAnalyzer = SampleRetryAnalyzer.class) to every test method.
public class RetryListener implements IAnnotationTransformer { public void transform( ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod ) { annotation.setRetryAnalyzer(SampleRetryAnalyzer.class); } } //testng.xml
Writing Custom Annotations
create custom annotations and use them using TestNG configurations methods to make annotations more customizable per your needs.
Example of custom annotation
create a single @DataProvider method that reads from different files with a file to read given in a test method.