JUnit Notes 1

Source: Internet
Author: User

JUnit is a regression testing framework (regression testing framework ). JUnit test isProgramStaff testing, the so-called white box testing, because the programmer knows that the testedSoftwareHow to complete the function and what the function is. JUnit is a framework that inherits the testcase class and can be automatically tested using JUnit.

Although he is a programmer in most cases, it is still necessary for a tester to understand that, after all, it is also a test job. To improve the technology of his own, the idea of extension is the main thing. He takes notes while learning, in this way, you will not forget what you have learned ~!

========= Learning Environment ============================

Java Editor: Eclipse SDK 3.5.6

JUnit test package: Eclipse comes with the JUnit 4 test package

======================================

1. Open eclipse and create a project named junit4,

Create a package named com. junit4.cc,

Create a class T under the package

CodeAs follows:

 
PackageCom. junit4.cc;
PublicClassT {
PublicIntAdd (IntX,IntY ){
ReturnX+Y;
}

}

How can we do the test? Of course, the above Code can see the result at a Glance. Do we need to test it? Here we just want to explain the problem. If we don't need to test the framework, we will use the write main method to verify the above Code. The Code is as follows.

 
PackageCom. 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 ); // Pass two parameters 3 and 5 to the T class
System. Out. println (z); // print Z to check whether addition operations are performed on 3 and 5.
}
}

The code above the cloud line checks the result to determine whether the add method is correct.

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

The above unit tests have many drawbacks:

@ It takes a lot of time to write test code

@ The test code cannot be reused. A test code corresponds to a tested code. The tested code is changed, and the test code is useless.

@ Cannot run multiple test codes at the same time. If there are one hundred tested codes, the test code should be run one by one, and the results should be observed with the naked eye, which is less efficient and prone to errors.

This is my summary of several shortcomings.

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

 

2. We need to use the unit test framework to submit the efficiency.

Next we will use eclipse to create a test. Or test the above Code as an example.

To make it clearer, we usually place the test code and the tested code under different packages.

Create a new package under the JUnit project named com. junit4.cc. Test.

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

Create a test class: ttest

Note:: A test class is created here: Right-click the com. junit4.cc. Test package and choose --> New --> JUnit test case

Click "Next" to select the method to be tested. Click "finish" to create the method.

Creation completed, for example:

The ttest. Java code is as follows:

 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 );
}

}

 

3. If we want to really run the ttest. Java test class, we need to introduce the JUnit test package to this project.

Right-click the project (junit4) ---> Properties

Click "Next". celipse provides two test packages, junit3 and junit4. We select junit4 and click "finish" to introduce the test package.

 

4. Run the ttest. Java test code below.

The green scroll bar on the left shows that our code is correct.

You can try to modify the parameters of the following code:

Int z = new T (). Add (5, 3 );
Assertequals (8, Z); // you can modify numbers such as 5, 3, and 8 to view the color of the green scroll bar. What is the prompt?

 

 

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.