JUnit 4 – Executing multiple Test Suites

So far we have learnt JUnit 4 from the aspect of running a single test case.  In this post, we will explore how to run multiple test cases with JUnit 4.

I am less bothered about how the JUnit is used for unit testing, but my concern is how we can utilize JUnit (referring to JUnit 4) for functional testing along with Selenium.  We will learn executing multiple test cases using JUnit with a simple test.

Note: If you are a newbie to Selenium / JUnit / Eclipse, then please refer to my previous posts that explain various topics such as Setting up Eclipse with Selenium, Creating test cases in Eclipse, etc.

Let us write three simple tests and then using @RunWith and @SuiteClasses annotations we will execute all of them together as a TestSuite.

  1. Create a new Package (e.g. com.selftechy.testsuite)
  2. Create three JUnit Test Cases in Eclipse under this package (First, Second, and Third)
  3. Create a fourth JUnit test case as RunTestSuite
  4. Right Click on RunTestSuite –> Run As –> JUnit Test
  5. Console output should be as in the below picture

RunSuite

ExecutionSuite-Output

 

Create three JUnit test cases First, Second, Third, and RunTestSuite using the below code and execute RunTestSuite

package com.selftechy.testsuite;
import org.junit.Test;

public class First {
    @Test
    public void testOne(){
        System.out.println("Executing first test");
    }

}
package com.selftechy.testsuite;
import org.junit.Test;


public class Second {
    @Test
    public void testTwo(){
        System.out.println("Executing second test");
    }

}
package com.selftechy.testsuite;

import org.junit.Test;


public class Third {
    @Test
    public void testThree(){
        System.out.println("Executing third test");
    }

}
package com.selftechy.testsuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({First.class,Second.class,Third.class})
public class RunTestSuite {
}

Comments 21

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.