Writing a TestNG script is a three-step process:
- Write the business logic of our test and insert TestNG annotations in the code.
- Add the information about our test (e.g. the class name, the groups you wish to run, etc…) in a testng.xml or build.xml file. In this post we will configure using testing.xml file.
- Run TestNG using Command Line, Ant, IDEs. We will be executing our test using Eclipse IDE.
Let’s start with our first TestNG script. We will use the same Java Project ‘FrameworkDesign’ that we created in Keyword Driven Framework Development…
- First step is to add the TestNG libraries into our project. Right click on the project ‘FrameworkDesign’ and click Properties -> Java Build Path -> Add Library.
- In Add Library window, select TestNG and click on Next. Click on Finish to add the TestNG Library. Click OK to close the Properties window.
- Once the above steps are completed, we will see TestNG Library added to our Java Project.
Test Case:
Let’s create our first test case using TestNG. This time we will use a different test website ‘http://www.newtours.demoaut.com‘ for creating our test cases.
First Test Case:Navigate to New Tours application and perform successful login.
Second Test Case:Navigate to New Tours application and perform successful registration.
We will use all the annotations in our script just to give an understanding on the workflow of a TestNG script. We will also use Data Providers for test data input.
- Right click on ‘com.selenium.testcase’ package inside ‘FrameworkDesign’ Java project and select TestNG -> Create TestNG Class as shown below.
- Give Class Name as ‘FirstTestNScript’ and select all annotations. This is just for learning purpose as you can create a TestNG script with out selecting any of the annotations selected below. However in that case, the TestNG script will have ‘@Test‘ annotation as default. Remember that TestNG class don’t have static main methods so method with annotations will start the execution.
- As we click Finish, a new class ‘FirstTestNScript’ is created with all the annotations and corresponding methods. To begin with we will just add ‘System.out.println’ code in all methods to print different messages in the console. This will give an idea on the workflow of the TestNG class.
package com.selenium.testcase; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.BeforeClass; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeTest; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeSuite; import org.testng.annotations.AfterSuite; public class FirstTestNScript { @Test(dataProvider = "dp") public void f(Integer n, String s) { System.out.println("This is Test Method"); } @BeforeMethod public void beforeMethod() { System.out.println("This is Before Method"); } @AfterMethod public void afterMethod() { System.out.println("This is After Method"); } @DataProvider public Object[][] dp() { return new Object[][] { new Object[] { 1, "a" }, new Object[] { 2, "b" }, }; } @BeforeClass public void beforeClass() { System.out.println("This is Before Class"); } @AfterClass public void afterClass() { System.out.println("This is After Class"); } @BeforeTest public void beforeTest() { System.out.println("This is Before Test"); } @AfterTest public void afterTest() { System.out.println("This is After Test"); } @BeforeSuite public void beforeSuite() { System.out.println("This is Before Suite"); } @AfterSuite public void afterSuite() { System.out.println("This is After Suite"); } }
- To execute our TestNG script, right click on ‘FirstTestNScript’ and click TestNG Test. The execution will be completed in no time and it will print some messages in the console. If we look closely on the console output, we can see that the messages are printed in the console in a particular order.
- From the test output we can easily conclude that ‘@Before/AfterSuite’ annotations are the very first and the last ones to be executed. Then ‘@Before/AfterTest’, followed by ‘@Before/AfterClass’. The ‘@Before/AfterMethod’ has executed twice alone with ‘@Test’ method. This is because the data provider ‘dp‘ has two sets of test data ({{ 1, “a” }, {2, “b”}}) and ‘@Test’ method executes twice using different set of test data from the data provider. Since ‘@Test’ is executed twice, so do ‘@Before/AfterMethod’ for each ‘@Test’ method.
- If we refresh our Java Project, a new folder named ‘test-output‘ is created with many HTML reports. To view an HTML file, right click on the file and click Open With -> Web Browser. This reports are generated by TestNG once the execution is completed.
In our next post we will continue to update this ‘FirstTestNScript’ class with the logic of two test cases mentioned above, followed by test execution and analysis of TestNG reports. We will also learn to configure our TestNG script using testing.xml file. Till then, Happy Learning!
Additional Information:
What is Data Provided in TestNG?
‘@DataProvider‘ marks a method as supplying data for a test method. The annotated method must return an Object[][] (2D Array) where each Object[] can be assigned the parameter list of the test method. In the above example we are passing two sets of data. This means the ‘@Test‘ method (which has the test case logic) will be executed twice, each with one set of test data. As we move on to next post, the use of Data Provider will be more understandable.