Execution of TestNG Suite (testng.xml)

Execution of TestNG Suite (testng.xml)

Execution Order of TestNG annotations

In this blogpost, we will go through execution order of TestNG annotations, with the simple working code example.

The hierarchical representation of execution order of annotations used in the below code snippet is shown as below.

  • BeforeSuite
    • BeforeTest
      • BeforeClass
        • BeforeMethod
          • Test
        • AfterMethod
      • AfterClass
    • AfterTest
  • AfterSuite

Create a new TestNG class, with the name TestClass1.java 

package com;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestClass1 {

@BeforeSuite
public void beforeSuite() {
System.out.println(“Inside Before Suite Method.”);
}

@BeforeClass
public void beforeClass() {
System.out.println(“Inside Before Class of TestClass1.”);
}

@BeforeTest
public void beforeTest() {
System.out.println(“Inside Before Test Method of TestClass1.”);
}

@BeforeMethod
public void beforeMethod1() {
System.out.println(“Inside Before Method 1 of TestClass1.”);
}

@BeforeMethod
public void beforeMethod2() {
System.out.println(“Inside Before Method 2 of TestClass1.”);
}

@Test
public void testMethod1() {
System.out.println(“Inside Test Method 1 of TestClass1.”);
}

@Test
public void testMethod2() {
System.out.println(“Inside Test Method 2 of TestClass1.”);
}

@AfterTest
public void afterTest() {
System.out.println(“Inside After Test Method of TestClass1.”);
}

@AfterMethod
public void afterMethod1() {
System.out.println(“Inside After Method 1 of TestClass1.”);
}

@AfterMethod
public void afterMethod2() {
System.out.println(“Inside After Method 2 of TestClass1.”);
}

@AfterClass
public void afterClass() {
System.out.println(“Inside After Class Method of TestClass1.”);
}

@AfterSuite
public void afterSuite() {
System.out.println(“Inside After Suite Method.”);
}

}

Now, execute the TestNG class as TestNG Test, and you will see the output as below.

Output:

Inside Before Suite Method.
Inside Before Test Method of TestClass1.
Inside Before Class of TestClass1.
Inside Before Method 1 of TestClass1.
Inside Before Method 2 of TestClass1.
Inside Test Method 1 of TestClass1.
Inside After Method 1 of TestClass1.
Inside After Method 2 of TestClass1.
Inside Before Method 1 of TestClass1.
Inside Before Method 2 of TestClass1.
Inside Test Method 2 of TestClass1.
Inside After Method 1 of TestClass1.
Inside After Method 2 of TestClass1.
Inside After Class Method of TestClass1.
Inside After Test Method of TestClass1.
PASSED: testMethod1
PASSED: testMethod2

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

Inside After Suite Method.

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

Let us jot down the key observations from the above execution result.

  • BeforeSuite and AfterSuite methods would be executed only once.
  • BeforeTest and AfterTest methods would be executed only once per test class.
  • BeforeClass and AfterClass methods would be executed only once per class.
  • BeforeMethod and AfterMethod methods would be executed for all @Test methods.
  • Test method contains actual business logic of making a test pass/ fail.
  • If the TestNG class contains more than one methods marked with @BeforeMethod and @AfterMethod, then all the methods would be executed before and after every @Test method.

That is all from this blogpost, hope it helps you!

Cheers 🙂

 

 

Annotations in TestNG

In this blogpost, we will focus on understanding the various annotations supported in TestNG.

TestNG annotation is a piece of code that is inserted in the program to control how the methods below are to be run.

The important annotation of TestNG are depicted in the below table.

Annotation Syntax Description
@BeforeSuite public void The method will be run only once, before all the tests in the suite are executed.
@BeforeGroups public void The method will be run once, before all the configured group tests are executed.
@BeforeClass public void The method will be run only once before the first test method in the class in called.
@BeforeTest public void The method will be run before any test method belonging to the classes inside the tag is run.
@BeforeMethod public void The method will be run before each test method (@Test).
Annotation Syntax Description
@AfterSuite public void The method will be run only once, after all the tests in the suite are executed.
@AfterGroups public void The method will be run once, after all the configured group tests are executed.
@AfterClass public void The method will be run only once after the first test method in the class in called.
@AfterTest public void The method will be run after any test method belonging to the classes inside the tag is run.
@AfterMethod public void The method will be run after each test method (@Test).
@Test public void Used with class or method, used to annotate test class or method

Visit Execution order of TestNG Annotations, to learn more about execution order of the above mentioned annotations, and a working example of annotations.

That is all from this blogpost, hope it helps you!

Cheers 🙂

#testng-annotations

Getting started with TestNG in Eclipse

In this blogpost, we will see how to configure how to a TestNG project in Eclipse IDE, and run a simple TestNG test case.

If you have not installed TestNG plugin in Eclipse IDE, visit Installation of TestNG in Eclipse IDE.

Step 1 : Create a Java project in Eclipse

Open Eclipse IDE.

Go to File -> New -> Java Project, and finish the process by giving project name and selecting JRE version .

For more information on this, visit this link.

Step 2 : Add TestNG library to build path of the project

Once after creating a Java project, we need to add the TestNG library to the build path of the project in order to utilize TestNG capabilities.

In order to add the library to build path, follow below steps

Right click on the project -> Build Path -> Configure build Path

TestNG_Create_Simple_TestNG_Project.png

After clicking on the Configure Build Path button, a dialog box for configuring project properties will be prompted. Click on Add Library button from the dialog box.

 

TestNG_Create_Simple_TestNG_Project_Add_Library.png

After clicking Add Library button, the wizard for adding library will be prompted, select TestNG from the list, click Next/ Finish button.

TestNG_Create_Simple_TestNG_Project_Add_Library2.png

Once after clicking Next/ Finish button, TestNG would have been added in the Libraries, and you would be able to see it in Project Explorer, as shown below.

TestNG_Create_Simple_TestNG_Project_Explorer_Library_added

Step 3 : Create a TestNG class

Go to src folder of the Eclipse project, and follow the below steps.

Right click on src folder -> New -> Other -> TestNG class

You will be able to see a window as shown below. Click Next to continue.
Create-testng-class-in-eclipse

Eclipse provides the options to  customize out TestNG class during creation. Here, we can select the annotations as per our needs, and click Finish.

 

Create-testng-class-in-eclipse2

On clicking Finish, the test class will be created with all the selected annotations methods in the previous step.

Step 4 : Run TestNG class in Eclipse

Now, it is time to run the test class created in previous step. Follow below series of steps to run the TestNG class.

Right click on TestNG class -> Run As -> TestNG Test

run-testng-testcase.png

You will be able to see result of the run as below.

result-of-testng-run.PNG

That is all from this blogpost. Hope it helps you!

Cheers 🙂

 

#create-a-testng-project-in-eclipse, #getting-started-with-testng

Installation of TestNG in Eclipse IDE

In this blogpost, we will learn how to install TestNG in Eclipse IDE. There are mainly two ways to install TestNG in Eclipse, as listed below.

  • Installing TestNG through Eclipse Marketplace
  • Installing TestNG through Install New Software option

In case you do not have Eclipse in your system, visit the link, and get latest version of Eclipse.

Let us get started with the first option to get TestNG in Eclipse IDE.

Installing TestNG through Eclipse Marketplace:

Step 1: Once you have Eclipse installed in your system, open the Eclipse IDE, go to Help menu, and click on Eclipse Marketplace menu item.

TestNG_Tutorial_Installation_of_TestNG.png

Step 2: Type TestNG in search bar,  beside Find label, and hit Enter. You will get TestNG plugin in result, click on Install button to install the plugin in Eclipse IDE.

TestNG_Tutorial_Installation_of_TestNG_Marketplace.png

Step 3: Once the installation is completed, search for TestNG one more time, and you will be able to see Installed button in place of Install.

Installing TestNG through Install New Software feature:

Step 1: Once you have Eclipse installed in your system, open the Eclipse IDE, go to Help menu, and click on Install New Software.. menu item. It will open Install dialog box.

TestNG_Tutorial_Installation_of_TestNG_Install_New_Software

Step 2: Enter http://beust.com/eclipse in Work with text field, and click on Add button.

TestNG_Tutorial_Installation_of_TestNG_Install_New_Software2.png

Step 3: Enter TestNG in the Name text field, and http://beust.com/eclipse in the Location field, and click OK.

TestNG_Tutorial_Installation_of_TestNG_Install_New_Software3

Step 4: After clicking the OK button, Eclipse will list down available versions of TestNG plugin as below. Click on the checkbox just beside TestNG, and you will see two TestNG plugins listed. Select both the plugins, and click Next button.

TestNG_Tutorial_Installation_of_TestNG_Install_New_Software4.png

Step 5: After clicking the Next button, you will be redirected to Review Licence dialog box, select I accept the terms of the licence agreements radio button, and click the Finish button.

TestNG_Tutorial_Installation_of_TestNG_Install_New_Software5.png

Step 6: Once after pressing the Finish button, the installation of TestNG will begin, and you will see installation wizard as below.

TestNG_Tutorial_Installation_of_TestNG_Install_New_Software6.png

Step 7: Once after the installation is completed, you will be asked to restart Eclipse for the changes to take effect. Click on Yes button, once the Eclipse is restarted, you will be able to see TestNG installed in Eclipse IDE.

TestNG_Tutorial_Installation_of_TestNG_Install_New_Software7.png

Verify installation of TestNG plugin in Eclipse IDE:

In order to verify whether TestNG plugin is installed in Eclipse IDE properly or not, open Eclipse and go to File -> New -> Other.

TestNG_Verify_Installation_of_Testng.png

After clicking the Other menu item, a select wizard will be prompted on the screen. Try to Scroll down, and you will be able to see TestNG folder as shown in the image below.

TestNG_Verify_Installation_of_Testng2.png

If the TestNG folder is visible in the wizard, TestNG plugin is installed successfully in Eclipse IDE, and you can get started with your first TestNG test.

That is all from this blogpost, hope it helps you!!

Cheers 🙂

 

 

 

 

#installation-of-testng, #installation-of-testng-through-eclipse-marketplace, #installation-of-testng-through-install-new-software, #verify-installation-of-testng

JUnit and TestNG Comparison

In this blogpost, we are going to compare JUnit with TestNG framework.

JUnit and TestNG both are Testing framework, but TestNG comes up with a few additional features that JUnit does not provide. So, TestNG is much more powerful than JUnit in terms of features and functionalities.

Sr. No Feature/ Description JUnit TestNG
1 Framework Unit Testing Unit, Functional, Integration, End-to-End Testing
2 Test annotation @Test @Test
3 Executed Before each test method @Before @BeforeMethod
4 Executed After each test method @After @AfterMethod
5 Executed Before the first test method in a class @BeforeClass @BeforeClass
6 Executed After all the test methods in a class @AfterClass @AfterClass
7 Ignore a test method @Ignore @Test(enabled=false)
8 Timeout test @Test(timeout=2000) @Test(timeout=2000)
9 Exception test @Test(expected = ExceptionClass.class) @Test(expectedExceptions = ExceptionClass.class)
10 Dependency test Not Available Available
11 Executed Before all the tests in the suite Not Available @BeforeSuite
12 Executed After all the tests in the suite Not Available @AfterSuite
13 Executed Before a test Not Available @BeforeTest
14 Executed After a test Not Available @AfterTest
15 Executed Before first test method is executed belonging to any group Not Available @BeforeGroups
16 Executed After first test method is executed belonging to any group Not Available @AfterGroups
17 Reporting Not Available Available

#compare-junit-with-testng, #comparison-between-junit-and-testng, #junit-and-testng-comparison, #junit-vs-testng

Timeout test in JUnit

In this blogpost, we are going to have a look at timeout feature provided by JUnit API.

If a test takes more than expected time to finish, then JUnit will automatically mark the test as FAILED with TestTimedOutException.

In order to give timeout to a particular test, timeout attribute is set along with @Test annotation. Timeout is in milliseconds.

@Test(timeout=1000)

public void test(){}}

Let us have a look at the code snippet to see this in action.

We have given 2000 milliseconds as a timeout value, meaning if the test method does not complete its execution within 2 seconds, JUnit will mark it as FAILED, and throw TestTimedOutException.

import static org.junit.Assert.*;

import org.junit.Test;

public class TimeoutClass {

@Test(timeout = 2000)
public void test() throws InterruptedException {
               System.out.println(“Inside test method…”);
               System.out.println(“Waiting for 5 seconds…”);
               Thread.sleep(5000);
           }

}

Thread.sleep(5000); statement in the test  method forces the method to take 5 seconds to finish the execution, the test method will be marked FAILED by JUnit.

junit-timeout-test.PNG

Hope it works for you!!

Cheers 🙂

#testtimedoutexception, #timeout-test-in-junit