Saturday, December 31, 2011

Simple Test Class using TestNG

In TestNG you don’t need to extend any special class or use any naming convention for test methods(You may remember the  test case names need to start with test in Junit 3), Now we just use the annotation @Test to signal to the framework the methods of the class that are tests. There have two test methods in following illustrates. Note that the assert Java instruction is used to check error conditions.
//TestNGSimpleTest .java
  1. package example.test;
  2. import org.testng.annotations.*;
  3. public class TestNGSimpleTest {
  4.     int testInt;
  5.     @BeforeMethod
  6.     public void setUp() {
  7.         testInt = 0;
  8.     }
  9.     @Test
  10.     public void addTest() {
  11.         testInt++;
  12.         assert (testInt == 1);
  13.         System.out.println("add test");
  14.     }
  15.     @Test
  16.     public void subtractTest() {
  17.         testInt--;
  18.         assert (testInt == -1);
  19.         System.out.println("subtract test");
  20.     }
  21. }
Note: The method setUp() will be invoked before any test method is run.

No comments:

Post a Comment