Exception Test in TestNG

In this blog post, we are going to learn about one of the important capability of TestNG Framework, and that is expectedExceptions property.

Sometimes, out test method might throw some unhandled exceptions (run-time exceptions), so it is important to handle it through our code, to endure smooth termination of the program.

TestNG provides a mechanism to handle run time exceptions in our code, using expectedExceptions property inside @Test annotation.

Let us understand the capability with working example.

Create a TestNG class, and name it as ExceptionTestTestNG.

package com;

import java.io.IOException;

import org.testng.annotations.Test;

public class ExceptionTestTestNG {

@Test(expectedExceptions = {
IOException.class
})
public void exceptionMethod1() throws Exception {
System.out.println(“Inside Exception Method 1..”);
throw new IOException();
}

@Test(expectedExceptions = {
IOException.class, ArrayIndexOutOfBoundsException.class
})
public void exceptionMethod2() throws Exception {
System.out.println(“Inside Exception Method 2..”);
throw new Exception();
}

}

Run the above TestNG class, and your console window will show something as below.

Inside IO Exception..Inside IO Exception..Inside IO & Null Pointer Exception..PASSED: exceptionTestOneFAILED: exceptionTestTwojava.lang.Exception at com.ExceptionTestTestNG.exceptionTestTwo(ExceptionTestTestNG.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108) at org.testng.internal.Invoker.invokeMethod(Invoker.java:661) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:744) at org.testng.TestRunner.run(TestRunner.java:602) at org.testng.SuiteRunner.runTest(SuiteRunner.java:380) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) at org.testng.TestNG.runSuitesLocally(TestNG.java:1226) at org.testng.TestNG.runSuites(TestNG.java:1144) at org.testng.TestNG.run(TestNG.java:1115) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

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

===============================================Default suiteTotal tests run: 2, Failures: 1, Skips: 0===============================================

If you notice, in the first method (exceptionMethod1), the code throws IOException, and it is handled properly using expectedExceptions property, hence the console does not log failure messages.

On the other hand, in second test method (exceptionMethod2), the code throws Exception, and it is not handled using expectedExceptions property, which results into a run time exception, and console prints a lengthy error log.

To avoid this situations, we should handle the exception(s) that might occur in our code properly.

That is it from this blog post, hope it works for you!

Cheers 🙂

Ignore Test in TestNG (enabled=false)

In this blog post, we are going to learn about one of the important capability of TestNG Framework, and that is IgnoreTest property.

We might across situations when we do not want a particular test method to be executed, along with the other test methods. And it is possible that the particular test method might be used in multiple TestNG suites, so removing/ commenting the call to that test method from all the places would be daunting and boring task.

TestNG provides a mechanism, using that we can make a particular test method disabled, so that the piece of code inside that method would not be executed while running the TestNG suite.

Let us understand the capability with working example.

Step 1. Create a TestNG class file, and name it as IgnoreTest.class.

package com;

import org.testng.annotations.Test;

public class IgnoreTestTestNG {

@Test(enabled = false)
public void ignoreMethod1() {
System.out.println(“Inside Test Method 1.”);
}

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

}

Step 2. Create/ Edit TestNG.xml, as below.

 <?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>

<suite name=”Suite”>
<test name=”TestSuiteDemo”>
<classes>
<class name=”com.IgnoreTestTestNG” />
</classes>
</test>
</suite>

Step 3. Run the TestNG suite, and notice the output on console.

Output:

Inside Test Method 1.

===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

If you notice the above output, the ignoreMethod1() which is marked as enable=false, is not called during the suite execution.

So, this is how we can ignore any test method while execution, especially when the test method is still under development.

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

Cheers 🙂

TestNG Reporting in HTML Format

In this blospost, we will learn how to generate HTML Reports for the execution of TestNG Suite.

As we already know that, JUnit does not have inbuilt Reporting Capabilities, but TestNG has a powerful way of generating HTML Reports of our test execution.

Let us go through the working example of executing a TestNG Suite and generating HTML report of the execution.

Step 1: Create one TestNG class file, and name it as 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.”);
}
}

Step 2: Create a TestNG.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>

<suite name=”Suite”>
<test name=”TestSuiteDemo”>
<classes>
<class name=”com.TestClass1″ />
</classes>
</test>
</suite>

Step 3. Run the TestNG suite, Right Click on TestNG.xml -> Run As -> TestNG Suite. When the execution is completed, a new folder with the name test-output will be created under the project.

testng_reporting-html.PNG

Open index.html, in your preferred web browser, and you will be able to see the detailed HTML Report of TestNG suite execution.

html_reports_in_testng.PNG

That is it from this blogpost, hope it works for you!

Cheers 🙂

Execution of TestNG Suite (testng.xml)

In this blogpost, we are going to learn how to execute the TestNG test suite using TestNG.

So far, we have seen how to execute a single TestNG class, but in practical, we need to run multiple test classes one after the another, and this can be achieved by creating a TestNG suite, in the form of XML file.

Let us understand the concept using a working example.

Step 1: Create first TestNG class, and name it as 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.”);
}

}

Step 2: Create second TestNG class, and name it as TestClass2.java.

package com;

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

public class TestClass2 {

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

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

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

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

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

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

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

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

}

Step 3. Create an XML file, and name it as TestNG.xml, and Save it in the project folder.

TestNG_XML_Location

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>

<suite name=”Suite” parallel=”tests”>
<test name=”FirefoxTest”>
<classes>
<class name=”com.TestClass1″ />
<class name=”com.TestClass2″ />
</classes>
</test>
</suite>

Step 4. Right click on TestNG.xml file, Click on Run As, and TestNG Suite. You will get the results as shown in the image below.

TestNG_XML_Result.PNG

That’s all from this blog post, hope it works for you!

Cheers 🙂

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