Global Unit test program based on JUnit

Source: Internet
Author: User

Background information

Recently participated in a new product research and development work. The new product is a modular development approach, with a number of functional modules, each module is a stand-alone Java project. In the product, in order to guarantee the function of each module, currently it has the corresponding JUnit test program. As product functionality gradually improved, we found that in the program only the JUnit test test files, all add up to already have hundreds of. Since these files are distributed in different subdirectory structures of dozens of different projects, there is no good tool to run all the unit tests at once. The manual running of these unit tests is tedious, time-consuming for programmers, or can be scripted to run all test files, but as we take the agile development model, the JUnit test testing set continues to grow, with each additional JUnit test file , you need to modify the script immediately; If you forget to modify it, the new test file may not be visible, and it is not a good way to execute the test file with your script, and it still gives us extra work for our development. Here we have an idea to do a global unit test program to automatically retrieve all of the JUnit test test programs in the project set. This global unit test program will be run based on JUNIT4.

Core mechanism: JUNIT4 supports running multiple test programs at a time

First we need to understand that JUNIT4 supports multiple class collections as input, and calls the Org.junit.runner.Runner.run () method to run the input collection of test classes. JUNIT4 has already defined some default Runner that can handle different sets of input classes: for example, Blockjunit4classrunner, which is the default handler for a JUNIT4 test class with @Test, and a Suite that can handle compatible JUNIT3 Test class of the runtime, and so on.

We can look at the following Runner chart:

Figure 1. JUNIT Runner structure diagram

The Runner class defines the interfaces that run test cases, the suite class inherits from the Runner class, and the Suite class supports JUNIT3-style test classes that can be used to execute multiple test cases. So our idea is to customize a Runner that inherits from the Suite class, which is the Allclassrunner class in the diagram above. This Runner input will be a collection of all the JUnit test classes found in the engineering set, so that you can run all of the JUnit test files in the different directories of the project at once. Here is the code for our Allclassrunner class:

Listing 1. Code for the Allclassrunner class

public class Alljunittestrunner 

extends Suite {public    
  alljunittestrunner (class<?> clazz, Runnerbuilder Builder) throws
      Initializationerror { 
      //Invoke the Parent-class Suite method to run all compliant JUnit Test objects directly to
      super (builder, Clazz, Loadalltestclass (Filterclassnamelist ( 
      clazz, Loadallclassesname (Clazz)))); 
   } 

The Loadallclassesname method will find a collection of all class filenames in the engineering set, and Filterclassnamelist will filter the found collection of class files for the file name. Loadalltestclass will find all junittest Class collections that meet the criteria.

How to find all the required JUnit test class collections

Let's look at the following flowchart:

Figure 2. Flowchart for finding the desired JUnit Test Class object Collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.