TestNG stands for Testing Next Generation. It is an automation testing framework used for Java programming language. It was developed by Credric Beust, and it comes after inspiration from the JUnit framework.
Three basic steps are required to write a test case.
We can run a test script in TestNG by right-clicking on the TestNG class, clicking on the “Run As” option, and then selecting “TestNG test”.
Annotations in TestNG are a set of code that controls how methods below them have to be executed i.e., the order of execution of methods below them will be decided by annotations that we give.
The following annotations used in the TestNG are:
The sequence of execution of all the annotations in TestNG is given below
A method annotated with @Test is called the test method which serves as a unit test. In the @Test method, we will write the logic of the application that we want to automate.
If we do not prioritize these tests or methods, then the methods are selected alphabetically and executed while execution. We can set priorities like
@Test(priority=1), @Test(priority=2)
To disable the test case you don’t want, you can use annotations
@Test(enabled = false)
The group is an attribute in TestNG that allows you to execute multiple test cases. For example, if we have 100 test cases of it_department and 10 test cases of hr_department, and if you want to run all the test cases of it_department together in a single suite, this can be possible only through the grouping.
We can define grouping in TestNG using the groups attribute as shown below:
@Test(groups=”title”)
There are some methods on which many methods are dependent.
For Example, if we want to test any application, and if the login page of the application is not working then we won’t be able to test the rest of the scenarios.
So, LoginTest is the method on which many tests are dependent. If LoginTest() is passed, only then does the other method get executed.
Two ways to write dependency tests. Dependency can be on methods or groups. If any method depends upon another method, mention the method name to show dependency.
@Test(dependsOnMethods=”LoginTest”)
If any method depends upon any group, mention the group name to show dependency.
@Test(dependsOnGroups=”functional”)
The grouping of test methods belonging to multiple groups can be done by providing the group names as an array in the groups attribute of the @Test annotation.
@Test(groups = { "GroupName1", "GroupName2", "GroupName3" .... })
The exclusion of a group in TestNG indicates that this group will not run throughout the execution and will be ignored by TestNG. Furthermore, the name of the group to be excluded is defined in the XML file using the following syntax:
<groups> <run>
<exclude name="groupname"> </exclude>
</run> </groups>
While running test cases, there can be a case when some test cases take much more time than expected. In such a case, we can mark the test case as a failed test case by using timeOut.
@Test(timeOut = 700)
Here, the test method will be given 700 ms to complete its execution otherwise it will be marked as a failed test case.
The thread count in TestNG is an attribute that is used to run the maximum number of threads for each suite if the parallel mode is enabled (otherwise ignore it).
For example, thread-count = “2”: It will run your tests with two threads means two methods run parallel in threads.
Testng.xml file is a configuration file (XML file) for TestNG in which we can create test suites, and test groups, mark tests for parallel execution, add listeners, and pass parameters to test script. It defines the runtime definition of a test suite.
Parameterization is a feature provided by TestNG that allows the users to pass parameter values to test methods as arguments.
Using this feature, we can run the same test over and over again with different values. The parameterization test is supported by using Parameter and DataProvider annotations of TestNG.
There are mainly two ways through which we can directly pass parameter values to test methods.
You can run the test script in TestNG by clicking right click on the TestNG class, click on "Run As" and then select "TestNG test".
We can use the timeOut attribute of @Test annotation. The value assigned to this timeOut attribute will act as an upper bound. If the test doesn’t get executed within this time frame then it will fail with timeout exception.
Using XML Suite Files: You can define test suites and tests in an XML file. This method provides the most flexibility in terms of test execution sequence, grouping, and parameterization.
Using Annotations: You can annotate methods with @Test to signify they're test methods. Then you can run these directly from an IDE.
In TestNG, a test method is a method that contains the actual test logic, while a configuration method is a method that is used to set up or tear down the test environment. Configuration methods include: @BeforeSuite, @BeforeClass etc
Groups can also include other groups. These groups are called MetaGroups. For example, you might want to define a group all that includes smokeTest and functionalTest.
<groups>
<define name="all">
<include name="smokeTest"/>
<include name="functionalTest"/>
</define>
<run>
<include name="all" />
</run>
</groups>
You can use the parameter tag within the test tag to pass parameters in the test case via the TestNG XML file. First, you need to specify the parameter name and value within the tag. These parameters can be accessed in the test case by using the @Parameters annotation in the method
@Test
@Parameters({"username", "password"})
public void myTestMethod(String username, String password) {
//code
}
Now use the XML file to pass the values to the parameters "username" and "password":
// TestNG XML file
<test name="MyTest">
<parameter name="username" value="myname" />
<parameter name="password" value="123" />
<classes>
<class name="MyTestClass" />
</classes>
</test>
</suite>
The @Factory annotation is used to create test instances at runtime. It enables you to generate test classes or instances dynamically based on runtime conditions or parameters.
You can use the @Factory annotation to create multiple instances of the same test class with different data sets or parameters
The @Factory annotation is used to create multiple instances of the same test class, each with a different set of input parameters or configurations. This is useful when running the same test with different data sets or configurations or when running tests in parallel to save time during testing.
@Factory
public Object[] factoryMethod() {
return new Object[] {
new SimpleTest("one"),
new SimpleTest("two")
};
}
Meanwhile, @DataProvider annotation supplies test data to a test method. It allows you to separate the test data from the test logic, making your test code easier to maintain and reuse.
@DataProvider
public Object[][] dataMethod() {
return new Object[][] { { "one" }, { "two" } };
}
Assertion assists us in determining whether the expected and actual results are equal. To determine whether the test case passed or failed, we use the built-in "Assert" class and many of its methods in TestNG.
In TestNG, the @Listener annotation is used to define listeners, which are classes that listen to events that occur during the execution of a TestNG test. Listeners can be used to customize or enhance TestNG's behavior by adding extra functionality such as logging, reporting, or customizing the test execution flow.
After all of the test methods have been completed, TestNG generates several reports by default. Such as:
The emailable report is generated by default in the TestNG project's output directory, in the folder named "test-output." The emailable report's filename is "emailable-report.html."
The index.html report is one of TestNG's default reports, providing an overview of the test results as well as links to other reports such as the emailable report, testng-results.xml, and testng-failed.xml.
The index.html report is generated by default in the TestNG project's output directory under the folder named "test-output." The index.html report's filename is "index.html."
The TestNG Reporter class is a built-in class for logging messages during test execution. Import the Reporter class and then pass a message string to the method as an argument as shown
Reporter.log("Log message");
TestNG is designed to support the parallel execution of tests, which allows testers to run multiple tests concurrently, reducing overall test execution time.
We can run our test methods/classes/tests in parallel by using the “parallel” attribute for Test Suite in the testng.xml file. The parallel attribute for the
<suite name = "Parallel Test Suite" parallel = "methods">
We can use the “parallel” attribute in testng.xml to accomplish parallel test execution in TestNG. The parallel attribute of the suite tag can accept four values:
The Time-Out test in TestNG is nothing but the time allotted to perform unit testing. If the unit test fails to finish within that specific time limit, TestNG will abandon further software testing and mark it as a failure.
The “@Factory” as the name suggests is an annotation that can produce tests at runtime using the given data-sets/conditions. This annotation solves a common problem of executing tests with different datasets without explicitly creating them.
Another advantage it brings is to allow the parameters to pass to test classes while instantiating them.
In order to run the tests in parallel just add these two key value pairs in the suite-
Parallel states that what to run parallel whether it be methods/tests/classes. Thread count denotes the number of threads you want to run simultaneously.
<suite name="TestingTestSuite" parallel="methods" thread-count="5">
To create a TestNG test file, you need to create a Java class and add the necessary annotations and methods. Here's an example:
import org.testng.annotations.Test;
public class MyTest {
@Test
public void testMethod() {
// Test code here
} }
In this example, the class is named MyTest and contains a test method annotated with @Test. You can run this test file using TestNG by creating an XML file that specifies the test classes to run.
To set a time limit for a test in TestNG, you can use the @Test annotation's timeOut attribute and specify the time limit in milliseconds. For example:
@Test(timeOut = 1000)
public void timedTestMethod() {
// code
}
Verbose is an attribute in TestNG that finds application in the <suite> tag of the testng.xml configuration file. It can accept values from 1 to 10. The higher the value you will use, the more detailed will be the test results log in the Eclipse IDE -> 'console' window.
Mention the name of the group that we want to exclude is defined in the XML file by the following syntax:
<groups>
<run>
<exclude name = "groupname"/>
</run>
</groups>
We specify the time unit in test suites and test cases in milliseconds.
TestNG offers two ways to produce a report.
Listeners implement the interface org.testng.ITestListener and are notified in real time of when a test will start, pass, fail, etc…
Reporters implement the interface org.testng.IReporter and are notified when all the suites have been run by TestNG. The IReporter instance receives a list of objects that describe the entire test run.
Copyright © 2023 - Proleed Academy | All Rights Reserved.