简体   繁体   中英

A better way to run lots of Integration Tests using JUnit?

What is the best way run a lot of integration tests using JUnit?

I crudely discovered that the code below can run all the tests... but it has a massive flaw. The tearDown() method in each of those classes is not called until they have all been run.

public class RunIntegrationTests extends TestSuite {
   public RunIntegrationTests(){
   }

   public static void main (String[] args){
      TestRunner.run(testSuite());
   }

   public static Test testSuite(){

      TestSuite result = new TestSuite();

      result.addTest(new TestSuite(AgreementIntegrationTest.class));
      result.addTest(new TestSuite(InterestedPartyIntegrationTest.class));
      result.addTest(new TestSuite(WorkIntegrationTest.class));
      // further tests omitted for readability

      return result;
   }
}

The classes being run connect to the database, load an object, and display it in a JFrame. I overrode the setVisible method to enable testing. On our build machine, the java vm runs out of memory when running the code above as the objects it has to load from the database are pretty large. If the tearDown() method was called after each class finished it would solve the memory problems.

Is there a better way to run them? I'm having to use JUnit 3.8.2 by the way - we're still on Java 1.4 :(

Not sure if this is the problem, but according to the JUnit Primer you should just add the tests directly, instead of using the TestSuite:

  result.addTest(new AgreementIntegrationTest()));
  result.addTest(new InterestedPartyIntegrationTest()));
  result.addTest(new WorkIntegrationTest()));

That's very strange. setUp and tearDown should bookend the running of each test method, regardless of how the methods are bundled up into suites.

I typically do it slightly differently.

TestSuite suite = new TestSuite( "Suite for ..." ) ;

suite.addTestSuite( JUnit_A.class ) ;
suite.addTestSuite( JUnit_B.class ) ;

And I just verified that tearDown was indeed being called the correct number of times. But your method should work just as well.

Are you sure tearDown is properly specified -- eg it's not "teardown"? When you run one test class on its own, is tearDown properly called?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM