This Tutorial Explains Various Types of Execution Processes in TestNG i.e. Batch Execution, Controlled Batch, Test Execution Report and Parallel Execution:
Process Execution in TestNG tends to save your time to a great extent while executing Test scripts or suites.
What You Will Learn:
Why Different Process Execution Methods?
Whenever we are executing the test scripts or suites in Java Eclipse environment, we should hit the Play button for every test script that needs to be executed. This indeed is a tedious and time-consuming process if one has many numbers of test scripts or suites.
To overcome this drawback, TestNG has a concept of grouping and executing the test scripts or suites based on the different types of Execution Processes that are listed below.
- Batch Execution
- Controlled Batch Execution
- Test Execution Report
- Parallel Execution
Let’s see each and every process execution in detail.
- It is the process of executing all the test scripts or suites in sequential order.
- There is no need for any manual intervention to run each test suite.
- Create a Selenium WebDriver project with the project name as TestNg_Demo.
- Install the TestNG plugin in eclipse.
Two test scripts (User and UserOperations) file has been created using TestNG annotations and one common script file (Base_class) where the order of execution is written.
Step 1: User.java
Step 2: UserOperations.java
Step 3: Base_Class.java
Step 4: Executing the test scripts in Batch by right-clicking the project and selecting Run As TestNG Test.
Step 5: Xml file will be generated in the test-output folder with the file name as Default test.xml.
Step 6: Add the test methods that belong to a particular test script or suite.
Step 7: Click the Run button and the Output will be displayed in the Console window as shown below.
Test Scripts have been successfully executed using Batch processing.
Controlled Batch Execution
It is the process of selectively executing a particular module that contains a group of test suites or scripts.
A module called UserOperations has been created. Under that, we have three users called Create, Edit and Delete respectively. Here, we are going to perform an action by verifying the CreateUser script using the Assert class. For that, we are setting dependencies for Edit and Delete Users using the ‘dependsOnMethods’ test annotation.
Let’s explore how to verify the scripts and see how its corresponding dependent scripts are managed.
Step 1: UserOperations. Java
If Create user script is successfully verified, then the other dependency scripts will be performed. Or Else, the scripts would be failed or even skipped.
Step 2: Add the test methods of the UserOperations module in the Default test.xml file.
Step 3: Verifying the Test Scripts.
#1) If the expected and Actual messages are equal.
String expected = “create a user”; String actual = “create a user”; Assert.assertEquals (expected, actual);
The corresponding output will be displayed in the Console window as shown below.
#2) If the Expected and Actual messages are not equal.
String expected = “create a user”; String actual = “creating an user”; Assert.assertEquals (expected, actual);
The corresponding output will be displayed in the Console window.
Test scripts in a particular module (UserOperations) are successfully executed and verified.
Test Execution Report
- Report generation can be done using TestNG that helps us to identify the status (pass/fail/skip) of each test method in a group of scripts or suites.
- Test Report will be generated in .html format.
Step 1: After executing test scripts, refresh the project by Right-clicking and selecting the Refresh option. A test-output folder will be generated automatically.
Step 2: By expanding the folder, under the Default suite, click the Default test.html file.
Step 3: Right-click and select Open With => Web Browser
Step 4: Test Execution Report showing all the information on test methods in a script will be generated.
- It is the process of running multiple test scripts at the same time. It helps us to save our execution effort and cover the maximum number of tests.
- We can also perform cross-browser testing that will make the application more stable.
- TestNG internally handles threading concepts that will allow us to run the test in multiple threads.
In TestNG, all the @Test methods/classes will be executed in the same thread. If we want to run our methods/classes in separate threads then we need to set the “parallel” attribute for methods/classes in the Default test.xml file.
Step 1: Configure the “Parallel” attribute as ‘methods’ in the Default test.xml file.
Step 2: Parallel_Demo.java
Here we are executing two test methods in parallel. We are printing Thread ID, on which the thread method should be executed first.
Step 3: Click the Run button and the Output will be displayed in the Console as shown below.
The above result shows the two methods that were executed using different threads. The thread that completes the execution of one method, will pick and execute the other test method.
Through this tutorial, we understood the different processes of TestNG execution. First, we started off creating a new Selenium WebDriver project using TestNG annotations and moved to different types of the execution processes and finally verified one of the test script using Assert statements.
Thus, executing many numbers of test scripts or suites using batch processing is much easier and it indeed saves our execution time too. Here, we implemented and executed the process flow of each and every type of execution for our easy understanding.