Thursday 4 December 2014

Making migration from JUnit to TestNG bit easier

JUnit, TestNG is used in both unit testing and automation framework.
If you want to switch from JUnit to TestNG for using the advanced features of TestNG API, this can be done easier(i.e. Junit to TestNG migration) by specifying 'mixed mode' option(junit="true") in TestNG <<suite file>>.xml file.

By using 'mixed mode' option developer can execute JUnit scripts(i.e. already developed scripts) and TestNG scripts(i.e. new scripts) in a TestNG suite.
If you already have a framework developed using JUnit API and want to migrate to TestNG this can be done by easy by following this approach.

Below procedure demonstrates executing both JUnit & TestNG testScripts in a TestNG suite(i.e. TestNG <<suite file>>.xml file).  

1. Download jar files:
download TestNg jar files from  below url
http://testng.org/beta/

download Junit, hamcrest-core jars from below url
https://github.com/junit-team/junit/wiki/Download-and-Install

2. Copy the downloaded jar files in to 'lib' folder of the project.
  i.e. testng-6.8.6beta.jar, junit-4.11.jar & junit-4.11.jar

3. Set the java build path(i.e. classpath) for the jar files present in 'lib' folder of the project.

4. 'src' folder contains both the scripts developed using JUnit & TestNG API.

JUnit Scripts:

JUnit Script 1:
package junit;
import org.junit.Assert;
import org.junit.Test;
public class JunitTest1 {
   
   @Test
   public void junittest1_a() {
      Assert.assertEquals("A", "A");
   }

   @Test
   public void junittest1_b() {
      Assert.assertEquals("A", "B");
   }
}  

JUnit Script 2:
package junit;
import org.junit.Assert;
import org.junit.Test;
public class JunitTest2 {
   
   @Test
   public void junittest2_c() {
      System.out.println("...junittest_c C ...");
      Assert.assertEquals("C", "C");
   }
}

TestNG Scripts:

TestNG Script 1:
package testng;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestNgTest1 {
   
   @Test
   public void testngtest1_a() {
      Assert.assertEquals("A", "A");
   }
  
   @Test
   public void testngtest1_b() {
      Assert.assertEquals("A", "B");
   }  
}

TestNG Script 2:
package testng;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestNgTest2 {
   
   @Test
   public void testngtest2_d() {
      System.out.println("...testngtest_d D ...");
      Assert.assertEquals("D", "D");
   }
}

Suite.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Converted JUnit suite" >
    <test name="JUnitTests" junit="true">
        <classes>
            <class name="junit.JunitTest1" />
            <class name="junit.JunitTest2" />
        </classes>
    </test>

    <test name="TestNgTests">
        <classes>
            <class name="testng.TestNgTest1" />
            <class name="testng.TestNgTest2" />
        </classes>
    </test>
</suite>



Executing suite file containing both JUnit and TestNG scripts:


Results:

Cons:Doesn't executes all the scripts(i.e. JUnit & TestNG scripts), if both the JUnit & TestNG scripts are placed in single <test><classes> tag. 
Ex: Below example suite xml executes only JUnit scripts, but not TestNG scripts.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Converted JUnit suite" >
    <test name="JUnitTestNgTests" junit="true">
        <classes>
            <class name="junit.JunitTest1" />
            <class name="junit.JunitTest2" />
            <class name="testng.TestNgTest1" />
            <class name="testng.TestNgTest2" />
        </classes>
    </test>
</suite>

No comments:

Post a Comment