Showing posts with label JUnit. Show all posts
Showing posts with label JUnit. Show all posts

Monday, March 19, 2012

Ant task/script for generating JUnit report

Here is an example explaining an ant task for running compiled classes and generating JUnit report after execution of classes.
  1. <target name="run-classes" depends="compile-classes">
  2.     <!-- Define the Ant tasks for running classes -->
  3.     <junit fork="yes" printsummary="no" haltonfailure="no">    
  4.         <batchtest fork="yes" todir="${build.loc}/reports" >
  5.           <fileset dir="${classes.dir}">
  6.             <include name="**/.class" />
  7.           </fileset>
  8.         </batchtest>
  9.         <formatter type="xml" />
  10.         <classpath refid="classpath" />
  11.     </junit>
  12.    
  13.     <!-- Ant task for generating reports -->
  14.     <junitreport todir="${build.loc}/reports">
  15.         <fileset dir="${build.loc}/reports">
  16.             <include name="TEST-*.xml"/>
  17.         </fileset>
  18.         <report todir="${build.loc}/reports"/>
  19.     </junitreport>     
  20.     <echo> Genereated JUnit Reports </echo>
  21. </target>

In the above ant target there are 2 sub-tasks i.e junit and junitreport.The junit task is for running test classes and junitreport is for generating JUnit report.

Explanation:
fork
     if true, it 'll run the tests in separate virtual machine.
printsummary:
     if yes, at the end of the test, a one-line summary will be printed.
haltonfailure:
     if yes, the build process will be stopped if the test fails.
The batchtest defines a number of tests based on pattern matching.
In this case formatter type is xml so an XML result will be output to result xml file

        The batchtest task of junit task 'll run all the tests classes that are in ${classes.dir} directory and produce xml files in the ${build.loc}/reports directory then the junitreport task take all the xml files that are starting with 'TEST-' and 'll generate report in the same directory.

Thursday, March 15, 2012

Selenium RC with JUnit4

Here is an example explaining how to use JUnit4 with Selenium RC i.e using JUnit4 to run selenium rc tests.

//GoogleTestJunit4.java
  1. package test.example;
  2. import org.junit.AfterClass;
  3. import org.junit.BeforeClass;
  4. import org.junit.Test;
  5. import com.thoughtworks.selenium.DefaultSelenium;
  6. import com.thoughtworks.selenium.Selenium;
  7. /**
  8.  * Search Google example.
  9.  *
  10.  * @author aksahu
  11.  */
  12. public class GoogleTestJUnit4 {
  13.    
  14.     Selenium selenium;
  15.    
  16.     @BeforeClass
  17.     public void startSelenium(){
  18.         selenium = new DefaultSelenium("localhost"4444"*firefox""http://www.google.com");
  19.         selenium.start();
  20.         selenium.windowMaximize();
  21.     }
  22.    
  23.     @Test
  24.     public void testGoogleSearch(){    
  25.         selenium.open("/");
  26.         selenium.type("id=lst-ib""selenium");
  27.         selenium.click("name=btnK");
  28.         selenium.click("link=Selenium - Web Browser Automation");
  29.         selenium.waitForPageToLoad("30000");
  30.     }
  31.    
  32.     @AfterClass
  33.     public void stopSelenium(){    
  34.         selenium.stop();
  35.     }
  36. }

This test launches firefox browser and executes the google search test.The selenium session is started in startSelenium() method which is called before any test run in the test class and ends with stopSelenium() method which 'll call after the tests run.

Selenium RC with JUnit3

Here is an example explaining how to use JUnit3 with Selenium i.e using JUnit3 to run selenium test.

//GoogleTestJunit3.java
  1. package test.example;
  2. import junit.framework.TestCase;
  3. import com.thoughtworks.selenium.DefaultSelenium;
  4. import com.thoughtworks.selenium.Selenium;
  5. /**
  6.  * Search Google example.
  7.  *
  8.  * @author aksahu
  9.  */
  10. public class GoogleTestJUnit3 extends TestCase{
  11.    
  12.     Selenium selenium;
  13.    
  14.     public void setUp(){
  15.         selenium = new DefaultSelenium("localhost"4444"*firefox""http://www.google.com");
  16.         selenium.start();
  17.         selenium.windowMaximize();
  18.     }
  19.    
  20.     public void testGoogleSearch(){      
  21.         selenium.open("/");
  22.         selenium.type("id=lst-ib""selenium");
  23.         selenium.click("name=btnK");
  24.         selenium.click("link=Selenium - Web Browser Automation");
  25.         selenium.waitForPageToLoad("30000");
  26.     }
  27.    
  28.     public void tearDown(){    
  29.         selenium.stop();       
  30.     }
  31. }

            This test launches firefox browser to run the google search test.The selenium session is started in setUp() method which is called before every test run and ends with tearDown() method which is called after every test run.Since setUp() and tearDown() are called before and after every test run so we can use only one test method to be run in each class if you want to start and stop selenium sessions with setUp() and tearDown() methods.Multiple tests with this fashion is a drawback i.e if you want to write multiple tests in a same class selenium session 'll start and stop for every test run.

Saturday, December 31, 2011

JUnit 4 Vs TestNG

1. Annotation @Test is used in both JUnit4 and TestNG but from different class:
  1. import org.junit.Test; // JUnit 4
  2. ... or
  3. import org.testng.annotations.Test; // TestNG
  4. public class MyTestClass {
  5.     @Test
  6.     public void aTestMethod() throws ... { ... }
  7. }
2. According to Annotation Support:

FeatureJUnit 4TestNG
Test annotation@Test@Test
Run before all tests in this suite have run@BeforeSuite
Run after all tests in this suite have run@AfterSuite
Run before the test@BeforeTest
Run after the test@AfterTest
Run before the first test method that belongs to any of these groups is invoked@BeforeGroups
Run after the last test method that belongs to any of these groups is invoked@AfterGroups
Run before the first test method in the current class is invoked@BeforeClass@BeforeClass
Run after all the test methods in the current class have been run@AfterClass@AfterClass
Run before each test method@Before@BeforeMethod
Run after each test method@After@AfterMethod
Ignore test@ignore@Test(enbale=false)
Expected exception@Test(expected = ArithmeticException.class)@Test(expectedExceptions = ArithmeticException.class)
Timeout@Test(timeout = 1000)@Test(timeout = 1000)
3. Exception Test

It says what exception will throw from the unit test.
//JUnit4
  1. @Test(expected = ArithmeticException.class)  
  2.     public void divisionByZeroException() {  
  3.       int i = 1/0;
  4.     }
//TestNG
  1. @Test(expectedExceptions = ArithmeticException.class)  
  2.     public void divisionByZeroException() {  
  3.       int i = 1/0;
  4.     }
4. Ignore Test

It says how to ignore the unit test.
//JUnit
  1. @Ignore("Not Ready to Run")  
  2. @Test
  3. public void IgnoreTest() {  
  4.     System.out.println("Method is not ready yet");
  5. }
//TestNG
  1. @Test(enabled=false)
  2. public void IgnoreTest() {  
  3.     System.out.println("Method is not ready yet");
  4. }
5. Suite Test

It says how to bundle a few unit test and run together.
//JUnit4
The "@RunWith" and "@Suite" are use to run the suite test. The below class means both unit test "JunitTest1" and "JunitTest2" run together after "JunitTest3" executed. All the declaration is define inside the class.
  1. @RunWith(Suite.class)
  2. @Suite.SuiteClasses({
  3.         JunitTest1.class,
  4.         JunitTest2.class
  5. })
  6. public class JunitTest3 {
  7. }
//TestNG
XML file is use to run the suite test. The below XML file means both unit test “TestNGTest1” and “TestNGTest2” will run it together.
  1. <suite name="My test suite">
  2.   <test name="My test">
  3.     <classes>
  4.        <class name="example.test.TestNGTest1" />
  5.        <class name="example.test.TestNGTest2" />
  6.     </classes>
  7.   </test>
  8. </suite>
6. Dependency Test

It says which test will execute after what test. If the dependent method fails, then all subsequent tests will be skipped, not marked as failed.
//JUnit4
JUnit framework is focus on test isolation; it did not support this feature at the moment.
//TestNG
It use "dependOnMethods" to implement the dependency testing as following
  1. @Test
  2. public void testMethod1() {
  3.     System.out.println("This is method 1");
  4. }
  5. @Test(dependsOnMethods={"testMethod1"})
  6. public void testMethod2() {
  7.     System.out.println("This is method 2");
  8. }
7. Parameterized Test

It says how to pass parameters to an unit test dynamically.
//JUnit4
The "@RunWith" and "@Parameter" is use to provide parameter value for unit test, @Parameters have to return List[], and the parameter will pass into class constructor as argument.
  1. @RunWith(value = Parameterized.class)
  2. public class JunitTest {
  3.      private int number;
  4.      public JunitTest(int number) {
  5.         this.number = number;
  6.      }
  7.      @Parameters
  8.      public static Collection<Object[]> data() {
  9.        Object[][] data = new Object[][] { { 1 }{ 2 }{ 3 }{ 4 } };
  10.        return Arrays.asList(data);
  11.      }
  12.      @Test
  13.      public void pushTest() {
  14.        System.out.println("Parameterized Number is : " + number);
  15.      }
  16. }
It has many limitations here; we have to follow the “JUnit” way to declare the parameter, and the parameter has to pass into constructor in order to initialize the class member as parameter value for testing. The return type of parameter class is “List []”, data has been limited to String or a primitive value for testing.
//TestNG
XML file or “@DataProvider” is used to provide vary parameter for testing.
(see the post: Parameterized Test TestNG and observe the difference)