Tuesday 7 January 2014

TestNG vs. Junit

TestNG Vs.Junit?
Advantages of TestNG over Junit
·         In Junit we have to declare @BeforeClass and @AfterClass which is a constraint where as in TestNG there is no constraint like this.
·         Additional Levels of setUp/tearDown level are available in TestNG like @Before/AfterSuite,@Before/AfterTest and @Before/AfterGroup
·         No Need to extend any class in TestNG.
·         There is no method name constraint in TestNG as in Junit. You can give any name to the test methods in TestNG
·         In TestNG we can tell the test that one method is dependent on another method where as in Junit this is not possible. In Junit each test is independent of another test.
·         Grouping of testcases is available in TestNG where as the same is not available in Junit.
·         Execution can be done based on Groups. For ex. If you have defined many cases and segregated them by defining 2 groups as Sanity and Regression. Then if you only want to execute the “Sanity” cases then just tell TestNG to execute the “Sanity” and TestNG will automatically execute the cases belonging to the “Sanity” group.
·         Also using TestNG your selenium test case execution can be done in parallel.




3 comments:

  1. How we can tell that one test is dependent on another in testng?

    ReplyDelete
    Replies
    1. import org.testng.Assert;
      import org.testng.annotations.Test;

      public class DependencyTestUsingAnnotation {
      String message = "Manisha";
      MessageUtil messageUtil = new MessageUtil(message);

      @Test
      public void testPrintMessage() {
      System.out.println("Inside testPrintMessage()");
      message = "Manisha";
      Assert.assertEquals(message, messageUtil.printMessage());
      }

      @Test(dependsOnMethods = { "initEnvironmentTest" })
      public void testSalutationMessage() {
      System.out.println("Inside testSalutationMessage()");
      message = "Hi!" + "Manisha";
      Assert.assertEquals(message, messageUtil.salutationMessage());
      }

      @Test
      public void initEnvironmentTest() {
      System.out.println("This is initEnvironmentTest");
      }
      }

      Delete
    2. import org.testng.Assert;
      import org.testng.annotations.Test;

      public class DependencyTestUsingAnnotation {
      String message = "Manisha";
      MessageUtil messageUtil = new MessageUtil(message);

      @Test(groups = { "init" })
      public void testPrintMessage() {
      System.out.println("Inside testPrintMessage()");
      message = "Manisha";
      Assert.assertEquals(message, messageUtil.printMessage());
      }

      @Test(dependsOnGroups = { "init.*" })
      public void testSalutationMessage() {
      System.out.println("Inside testSalutationMessage()");
      message = "Hi!" + "Manisha";
      Assert.assertEquals(message, messageUtil.salutationMessage());
      }

      @Test(groups = { "init" })
      public void initEnvironmentTest() {
      System.out.println("This is initEnvironmentTest");
      }
      }

      Delete