Global Unit test program based on JUnit in Java programs

Source: Internet
Author: User
Keywords Java test program JUnit global unit
Tags aliyun based class development different file files html

And each program module contains a large number of unit tests, at this time if the programmer also runs the unit test each time manually, the workload will be huge, and this is a kind of tedious duplication of work. This article will introduce a jhttp://www.aliyun.com/zixun/aggregation/29926.html ">unit" Global unit test program that programmers need to execute only one file, It can automatically execute all the unit test files in the whole project, thus saving the programmer's valuable time and improving the SOFTWARE product development progress.

Recently participated in a new product 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 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 11545.html "> 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 test class collection. 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, 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's 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 object 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 eligible Junittest Class collections.

Related Article

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.