Search code examples
javadynamicjunitruntimejunit3

Junit Dynamically Created Tests Not Working


public class NewTest extends SeleneseTestCase {

public static Test suite() throws Exception
{
   TestSuite suite = new TestSuite();

   TestSuite s = new TestSuite("TestCase Name");
      GeneratedTest t = new GeneratedTest("testName");
      t.setFailure("TestCase Name: testName");
      s.addTest(t);
      t = new GeneratedTest("testAge");
      s.addTest(t);
      suite.addTest(s);
   s = new TestSuite("TestCase Name2");
      t = new GeneratedTest("testOOGABOOGA");
      t.setFailure("TestCase Name2: testOOGABOOGA");
      s.addTest(t);
      suite.addTest(s);
   s = new TestSuite("TestCase Name4");
      t = new GeneratedTest("testName");
      t.setFailure("TestCase Name4: testName");
      s.addTest(t);
      t = new GeneratedTest("testAge");
      s.addTest(t);  
      suite.addTest(s);
   s = new TestSuite("TestCase Name3");
      t = new GeneratedTest("testName");
      t.setFailure("TestCase Name3: testName");
      s.addTest(t);
      t = new GeneratedTest("testAge");
      s.addTest(t);
      suite.addTest(s);

   return suite;
}

}


public class GeneratedTest extends TestCase
{
  public String testFailMessage;

  public GeneratedTest(String name)
  {
    ((TestCase)this).setName(name);
  }

  public void runTest()
  {
    if (testFailMessage != null)
    {
      fail(testFailMessage);
    }
  }

  public void setFailure(String msg)
  {
    testFailMessage = msg;
 }
}

As you can see (or maybe you can't) i'm adding tests to junit at runtime. This is all fine and dandy, except that it doesn't properly display them. Here, see what I mean:

click here for image

As you can see, tests with the same name don't even display that they've been run, except for the last test with duplicate name, and that test has the error messages from all the other tests with the same name.

Is this simply just a flaw with the way that i'm doing it (junit3 style)? Would I have to change it to use junit4 parameterization to fix it?


Solution

  • I noticed something similar in Eclipse's test runner. For JUnit 3.8 style parametrized tests, the names were not being displayed. Switching to JUnit 4 style solved the problem.

    While this isn't exactly your scenario, I think it is something you'll have to live with until you can update the tests to JUnit 4. Eclipse does still run the tests which is the important thing.