Junit test that creates other tests

Usually I will have one junit test that appears on my integration server of choice, as one test that passes or fails (in this case I use teamcity). What I need for this particular test is the ability to scroll through a directory structure check that all data files can be analyzed without exception.

Since we have 30,000+ files that in 1-5 seconds to analyze this test will be executed in their own package. The problem is that I need a way so that one piece of code runs as one junit test for each file, so if out of 12 files out of 30,000 files fail, I can see which of 12 failed, not just the one which failed, threw the runtime and stopped the test.

I understand that this is not a real β€œsingle” test way to do something, but this simulation is very important to make sure that our content providers are in control and do not check for invalid files.

Any suggestions?

+3
source share
3 answers

I think you need parameterized tests. It is available if you are using JUnit4 (or TestNG). Since you mention JUnit, you need to look at the documentation @RunWith(Parameterized.class) and @Parametersannotations.

+5
source

I would write one test that read all the files, either in a loop, or some other means, and collected all the failed files in some kind of collection for the message.

, TestNG DataProvider . TestNG .

+3

Junit3: TestSuite, , TestCase . JVM, TestCase (setUp, tearDown , , , , ..).

, TestCase , .

suite() TestSuite. Ant JUnit , , .

public class DynamicTest extends TestCase {
   String filename ;

   public DynamicTest ( String crntFile ) {
      super("testMethod");
      filename = crntFile ;
   }

   // This is gross, but necessary if you want to be able to
   // distinguish which test failed - otherwise they all share
   // the name DynamicTest.testMethod.
   public String getName() {
      return this.getClass().getName() + " : " + filename ;
   }



   // Here the actual test
   public void testMethod() {
      File f = new File( filename ) ;
      assertTrue( f.exists() ) ;
   }

   // Here the magic
   public static TestSuite suite() {

      TestSuite s = new TestSuite() ;

      for ( String crntFile : getListOfFiles() ) {
          s.addTest( new DynamicTest(crntFile ) ) ;
      }

      return s ;
   }
}

Of course, you can separate TestSuite from TestCase if you want. However, TestCase does not stand still, so you will need to be careful about naming conventions if your tests are automatically detected.

+2
source

Source: https://habr.com/ru/post/1741318/


All Articles