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

 

#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

Handling exceptions in JUnit

In this blogpost, we will have a look at several ways to handle run exceptions in JUnit.

Using @Test annotation attribute:

This is one of the simplest ways to handle a run time exception in JUnit test case. This can be achieved by setting expected attribute value with the type of exception, that might occur.

@Test(expected=ArithmeticException.class)

Create a class containing a @test method as below.

import org.junit.Test;
import org.junit.rules.ExpectedException;

public class ExceptionTest {

@Test(expected= ArithmeticException.class)
public void exceptionTest() {
System.out.println(“Inside exception test…”);
int i = 1/0;
System.out.println(“After exception…”);
}

}

Run the test class as JUnit Test. The divide by zero exception will be handled by expected attribute.

Using @Rule annotation:

This is another way of handling exceptions in JUnit, refer to the below code snippet.

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class ExceptionTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void test() {
thrown.expect(ArithmeticException.class);
thrown.expectMessage(“exc caught”);
System.out.println(“Inside exception test”);
int i = 1 / 0;
System.out.println(i);
System.out.println(“After exc”);
}

Run the test case class as JUnit test case, and you will see the output as below.

junit-exception-test-runwith.PNG

Hope it works for you!!

Cheers 🙂

#runwith, #exception-test-in-junit, #expected, #handling-exception-in-junit

Ignore Test (@Ignore) in JUnit

In this blogpost, we are going to learn how to ignore a particular test case from a test suite.

This can be useful when we do not want to run a particular test method or a group of test methods contained in a class.

JUnit provides @Ignore annotation, that can be used at two levels.

  1. If you want to ignore a particular test method, you can use @Ignore at the method level.
  2. If you want to ignore all the test cases of a class, you can use @Ignore at the class level.

Using @Ignore at the method level:

Let us understand how to ignore a particular test method using @Ignore annotation.

Create a class containing all the @Test methods. Two test methods are annotated with @Ignore and @Ignore(“message”) annotation. @Ignore(“message”) works in the same way, but additionally allows us to mention any message for the test method

import org.junit.Ignore;
import org.junit.Test;

public class IgnoreTestExample {

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

@Test
public void testMethod2() {
System.out.println(“Inside test method 2”);
}

@Ignore
@Test
public void ignoreTestMethod1() {
System.out.println(“Inside ignore test method 1”);
}

@Ignore(“The feature is not ready yet.”)
@Test
public void ignoreTestMethod2() {
System.out.println(“Inside test method 2”);
}

}

Create a test runner class to run the test case class.

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class IgnoreTestRunner {

public static void main(String[] args) {
Result result = JUnitCore.runClasses(IgnoreTestExample.class);

for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println();
System.out.println(“Test Suite execution was successful: ” + result.wasSuccessful());
}
}

Since ignoreTestMethod1() and ignoreTestMethod2() are marked with @Ignore annotation, the code enclosed within it will not be executed.

Using @Ignore at the class level

Now, we will see how to ignore all the test methods contained in a particular class.

Create a class annotated with @Ignore annotation, containing two test methods, one annotated with @Ignore annotation.

Here, since we have annotated a class with @Ignore annotation, none of the test methods of the class will be executed by JUnit. So, this is the way we can ignore multiple test methods at once, instead of annotating each test method with @Ignore annotation.

import static org.junit.Assert.*;

import org.junit.Ignore;
import org.junit.Test;

@Ignore
public class IgnoreTestClass {

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

@Ignore
@Test
public void ignoreMethod1() {
System.out.println(“Inside ignore method 1”);
}

}

Create  a test runner class to run the test class’ methods.

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class IgnoreClassTestRunner {

public static void main(String[] args) {
Result result = JUnitCore.runClasses(IgnoreTestClass.class);

for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}

System.out.println(“Test RunCount : ” + result.getRunCount());

}
}

If you run the IgnoreClassTestRunner class, you will get the output as below.

ignore-test-class-in-junit.PNG

Hope it helps you!!

Cheers 🙂

#ignore-annotation, #ignore-test, #ignore-test-class-in-junit, #ignore-test-method-in-junit

Assertion in JUnit

In this blogpost, we are going to cover the use of Assert class and its useful methods.

Assert class provides a bunch of methods, used while writing test scripts in order to make a test case pass or fail, based on certain conditions.

In order to see all the assertion methods provided by JUnit in Assert class, just press control and space keys together after writing Assert.

Assertion-methods-in-junit.png

Assert-class-methods-in-junit.png

We can make use of these methods to make our test script pass or fail, based on the condition specified.

Assert.assertTrue(boolean condition):

The method asserts that the condition specified is TRUE, if the condition is not TRUE, it throws an AssertionError. An overloaded version of the method is also provided, which allows to specify the error message along when the condition is not TRUE.

Assert.assertTrue(String message, boolean condition);

Assert.assertFalse(boolean condition):

The method asserts that the condition specified is FALSE, if the condition is not FALSE, it throws an AssertionError. An overloaded version of the method is also provided, which allows to specify the error message along when the condition is not FALSE.

Assert.assertFalse(String message, boolean condition);

Assert.fail():

The method fails the test, without any condition specified. We can specify the failure message as well, using an overloaded version of the method.

Assert.fail(String message);

Assert.assertNull(Object obj):

The method asserts that the object specified is null, if it is not null, then it throws AssertionError.

Assert.assertNull(String message, Object obj):

The method works in the same way as the previous, but it throws AssertionError with the message specified if the object is not null.

Assert.assertNotNull(Object obj):

The method asserts that the object specified is not null, if it is null, then it throws AssertionError.

Assert.assertNotNull(String message, Object obj):

The method works in the same way as the previous, but it throws AssertionError with the message specified if the object is null.

Assert.assertEquals(expected, actual):

This is one of the important method of Assert class.

The method has many overloaded versions, which accepts different type of arguments like boolean, byte, char, short, int, long, double, float, double, String, Object etc.

Example: assertEquals method for boolean and double datatypes.

Assert.assertEquals(boolean expected, boolean actual);

Assert.assertEquals(double expected, double actual);

The method compares the expected value with the actual value, and throws AssertionException is both do not match.

Assert.assertEquals(String message, expected, actual):

The method works in the same way as the previous, but it throws AssertionError with the message specified if the expected and actual values are not the same.

Here is the sample code snippet containing several assert methods.

import org.junit.Test;
import junit.framework.Assert;

public class AssertionDemo {

@Test
public void testMethod() {
boolean b1 = (5 > 4);
boolean b2 = “hello”.equalsIgnoreCase(“world”);
String s1 = “test”;
String s2 = “test”;
String s3 = “testing”;
Object o1 = null;

Assert.assertTrue(b1);
Assert.assertFalse(b2);
Assert.assertEquals(s1, s2);
Assert.assertEquals(“String values are not same”, s1, s3);

Assert.assertNull(o1);
Assert.assertNotNull(o1);

}
}

Hope it works for you!!

Cheers 🙂

#assert, #assert-in-junit, #assertequals, #assertfalse, #assertion-methods, #assertnotnull, #assertnull, #asserttrue, #fail