Sunday, December 2, 2012

TestNG with an Example

1. How to install TestNG

Steps to install TestNG
1. Open Eclipse
2. Click on Help => install new Software
3. In the Work with text box, enter this url/text, 
(Testng - http://beust.com/eclipse)
4. Click on Add button next to text box.
5. you will see TestNG added in to below text area.
6. Select it and click on next button. Go on with next window.
7. click on finish.

Steps to check if TestNg is installed in eclipse
1. In Eclips => Click on Window=>Show View
2. TestNG should appear in the list, if it does not appear, then
3. Click on other=>java. Now you should be able to see TestNG icon appearing in the list.
...................................................................................................

2. Example of TestNG



Try out the following example with TestNG.
Steps to create a Project, package and files

1. Create a Java Project
   File=>New=>Java Project (example: Sample1)

2. Create a Package.
  File=>New=>Package. (example: com.pack1)

3. Create a TestNG class.
  File=>New=>Other=>TestNG=>TestNG Class. (example: Test1)

4. Create a Build.xml file under Project Folder.
   Right Click on Project Sample1=>New=>File (example: Build.xml)

This example includes the following files.
1. Test1.java   (package: com.pack1)
2. Build.xml     (create this file under Project Folder)

Write a Method under the annotaion @Test as shown below.
...................................................................................................
Test1.java
...................................................................................................

import org.testng.annotations.Test;


public class Test1 {
  @Test
       public void Simple_test() {
 System.out.println("My First TestNG example");
       }

}
...................................................................................................

Build.xml
...................................................................................................

<?xml version="1.0" encoding="UTF-8"?>
<suite name="New Example">

<test name="My First Test">
<classes>
<class name="com.pack1.Test1"></class>
</classes>
</test>
</suite>
...................................................................................................

NOTE: In Build.xml file, you can give any Suite name, any class name as per your understanding. But inside class name you have to specify the correct class name with package as shown in the example.
     You can add any number of Tests inside a Test Suite tag. Each Test should contain a Test tag, classes tag and class tags.

...................................................................................................

3. Steps to RUN the java file



RightClick on JAVA file=>select RUN AS => TestNG
...................................................................................................


4. Steps to Check Reports of TestNG



1. Expand the test-output folder under Project folder in Eclipse.
2. Click on index.html.(you can find all details of test result)

...................................................................................................

Total Pageviews