0

I am new to JUNIT 4, I want to write code such as test suite get inputs from excelsheet and based on input testsuite invoke diffrent test cases. As per JUNIT 4 we can define all test cases in @SuiteClasses({ ParameterSampleTest.class,SampleJUnitTest.class }) but this is when we have predefine test cases. but here i want to use custome JUNIT runner that takes decision based on input? can some one help to resolve this problem?

@RunWith(Suite.class) 
 public class AllTests {
  public static TestSuite suite() {
        TestSuite suite = new TestSuite();
        suite.addTest(new JUnit4TestAdapter(ParameterSampleTest.class));
        suite.addTest(new JUnit4TestAdapter(SampleJUnitTest.class));
        return suite;
  }
}

Thanks, Priyank Shah

Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
Priyank Shah
  • 609
  • 3
  • 9
  • 25

2 Answers2

0

This should work...

@RunWith(Suite.class) 
public class AllTests {
public static TestSuite suite() {
        TestSuite suite = new TestSuite();
        //if(your logic based decision) {
            suite.addTest(new JUnit4TestAdapter(ParameterSampleTest.class));
        //}
        suite.addTest(new JUnit4TestAdapter(SampleJUnitTest.class));
        return suite;
  }
}
Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
  • 1
    I am getting error: java.lang.Exception: class 'com.example.junittest.AllTests' must have a SuiteClasses annotation – Priyank Shah Sep 10 '12 at 10:38
0

You can implement your own test runner. It is not so difficult. You just have to create class that extends org.junit.runner.Runnerand implement its getDescription() and run() methods. The runner may find sub-cases according to its business logic and run them.

AlexR
  • 114,158
  • 16
  • 130
  • 208