Run Test in TestNG

You will see, How can you Run Test in TestNG. TestNG is Next Generation testing framework. As we have seen TestNG have many advantages over JUnit Framework. It’s not mandatory to use JUnit with selenium to design the Automation Scripts. You can use TestNG as well. In these days TestNG + Webdriver is a famous combination in the Automation testing world.

 

Let’s see an example of TestNG test case. How we can create the TestNG test class.

 

Create Test in TestNG

It’s very easy to create the TestNG test class. You can create the TestNG test class like other java class. You just need to declare the test methods attached with @Test annotations. We are creating the sample TestNG test and will discuss the reporting as well.

 

Video Tutorial – Execute Selenium Test using TestNG

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

Add TestNG Library in Eclipse

We have seen how we can add JUnit JAR files into reference library, With TestNG, you don’t need to add external libraries.

As we have downloaded and configured the TestNG in our eclipse IDE. So as well as you will declare the TestNG annotations, You will get the compilation error in your class file. Once you hover the mouse over the declared annotation, Eclipse will show you the suggestion to “Add TestNG Library“.

Add TestNG Library

 

Click the “Add TestNG library” option. As you clicked it, Error will be removed and TestNG library will be added to your project. like this –

Add TestNG

Now your Test Class is ready to execute, You can execute this using TestNG. To execute it using TestNG. You need to select the TestNG option from class executors.

Right -click on your class, Hover mouse on Run As … and chose TestNG as class executor.

Run TestNG

 

Output:

Before Suite
Before Test
Before Class
 Before method
Hi, I am from first Test
After Method
 Before method
Hi, I am from second Test
After Method
After Class
After Test
PASSED: firstTest
PASSED: secondTest

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

After Suite

===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Now you can see the class output in your console window.

 

TestNG Report

As we have discussed TestNG generates the HTML report. Now the question is how can we see this.

To see the TestNG HTML report you need to refresh your project ( Select your Project and press F5) .

You noticed new folder name “test-Output” is been created in your project.

Test Output TestNG

 

Now to see the report.

  • Right -Click this test – output.
  • Select the last option Properties. Within the Location tag, you will get the location of this folder.
  • Go to that location in your machine. Open the directory test-Output.
  • Here, you will find the Index.html file.
  • Click on this file.

 

What happened, Wow your HTML report is ready. Now here you can see, Your test result. An error occurred during the execution and time spent to execute the test case. Every time you execute the TestNG test class or suite this report will refresh himself and display the latest result to you.

You can forward this report to your client, stakeholders, and senior management as well.

That’s all how you can create, execute and see the TestNG report.

0 Comment

Leave a Reply