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) {}