0

I need to create a testsuite, with subsuites containing tests that check data for me (using a java library). The goal of it is to test if the library works ok. Think of it like a virus scanner definitions check. I have experience with the old Junit/Nunit, and expected it to be quite easy. However, it turns out it is not with Junit4.

I have the following filesystem layout:

typename(dir)
    - file1
    - file2
typename(dir)
    - file1
    - file2
    - file3
etc.

I need to run a unittest that tests each file, and lives in a "typename" testsuite. The idea is to process the filesystem and then generate the testcases and suites dynamically:

for (File typeNameDir: typenameDirs)
{
  TestSuite typenameSuite= new TestSuite();
  typenameSuite.setName(typename);
  for (File file: filesInThisDir)
  {
     typenameSuite.addTest(new FileTest(File));
  }
}

Problem now is that addTest expect a class that implements Test, but that is an interface of the framework and not usable, because of the low level methods it needs to have implemented. I also tried to extend TestCase but this also doesn't work: I need to set the testname there manually and it still doesn't run normally. Also it is discouraged to extends the junit.framework classes.

So, it looks like I need to use Parameterized option, but that doesn't allow me to input dynamically generated data... So what should I do?

I am thinking of just moving to Junit 3.

1 Answer 1

1

You may have a look at this post. The author describe a solution to generate test case at runtime.

And just for curiosity, why @Parameterized doesn't allow you to input dynamically generated data? It doesn't work like this?

@Parameters
public static Collection<Object[]> data() {
          List<Object[]> data = new ArrayList<Object[]>();
          //load files
          for (File file: filesInThisDir) {
               data.add(file);
          }

          return data;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, but since this method is static I can't supply that method with for example the dynamic path where it needs to get it's files.
@Peterdk JUnit need to know how many test cases will be run before it executes any of them, so the method is static. Could you given an dynamic path example? I'm sorry I don't get it.
As seen in the startpost, I need a seperate testsuite for every typename folder. So the parametrized test needs to know which folder it needs: filesInThisDir needs to receive somehow dynamically the filepath. That can't be done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.