JUnit notes 3

Source: Internet
Author: User

In this section, we will learn how to perform batch processing. If we have many tests and how to perform batch processing, please follow me.

---------------------------------------------

Assume that we have written twoProgram. As follows:

T. Java

 Package  Com. junit4.cc;

Public Class T {
Public Int Add ( Int X, Int Y ){
Return X + Y;
}
Public Static Void Main (string ARGs []) {
Int Z = New T (). Add ( 3 , 5 );
System. Out. println (z );
}
}

User. Java

PackageCom. junit4.cc;

PublicClassUser {
PublicString gename (){
Return"Liu ruoying"; // PS: She is my idol :)
}
}

------------------------------------ Test class -------------------------

The following describes the test classes for the preceding two classes. (For how to create a test class, see my first JUnit Notes 1 if you don't understand it.)

Ttest. Java corresponds to the T. Java test class.

 Package  Com. junit4.cc. test;

Import Static Org. JUnit. assert. * ;

Import Org. JUnit. test;
Import Com. junit4.cc. * ;

Public Class Ttest {
@ Test
Public Void Testadd (){
Int Z = New T (). Add ( 5 , 3 );
Assertequals ( 8 , Z );
}
}

Usertest. Java corresponds to user. Java

 Package  Com. junit4.cc. test;

Import Static Org. JUnit. assert. * ;

Import Org. JUnit. test;
Import Com. junit4.cc. user;

Public Class Usertest {

@ Test
Public Void Testgename (){
Assertequals ( New User (). gename (),( " Liu ruoying " ));
}
}

The list after completion is as follows:

There are two methods to run the above two test classes in batches.

Method 1

Right-click the test package "com. junit4.cc. Test" ---> Run as ---> RUN deployments.

This method is easy to set, but the flexibility is not high. Suppose there are 100 test classes. I just want to run some of the test classes. The second method is shown below.

 

Method 2

This method introduces the concept of "test suite,JUnit provides a method to run the test class in batches, called the test suite.

The test suite should follow the following principles:

1. Create an empty class as the entrance to the test suite;

2. Use annotations org. JUnit. Runner. runwith and org. JUnit. Runners. Suite. suitclasses to modify this empty class.

3. Input org. JUnit. Runners. Suite as a parameter to the annotation runwith, prompting JUnit to execute this test using the suite runner.

4. An array of test classes that need to be put into this test suite will be used as a parameter to annotate suiteclasses

5. Make sure that this empty class is modified using public, and there are public constructors without any parameters.

The following is a test class created under the test package (COM. junit4.cc. Test). The content is as follows:

Alltest. Java

 

PackageCom. junit4.cc. test;


ImportOrg. JUnit. Runner. runwith;
ImportOrg. JUnit. Runners. Suite;
ImportOrg. JUnit. Runners. Suite. suiteclasses;
@ Runwith (suite.Class)
@ Suiteclasses ({
Ttest.Class,
Usertest.Class // here, the test class to be run is freely added.
})
PublicClassAlltest {

}

 

List after creation:

 

Result of running alltest. Java (here, the running results of the two methods are the same ):

 

PS: For comparison between the two methods, the first method is simple. You can run all the test programs in batch by selecting the test package. The second method is to re-create a test class, but it is very flexible. You only need to add the class to be run in this class to flexibly run the class to be tested.

 

 

 

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.