Use of the JUnit framework in Eclipse-unit testing

Source: Internet
Author: User

"Turn" junit a dangerous note one

JUnit is a regression testing framework (regression testing framework). JUnit testing is a programmer's test, the so-called white-box test, because the programmer knows how the software being tested (how) accomplishes the function and what it does. JUnit is a set of frameworks that inherit the TestCase class and can be automatically tested with JUnit.

Although most of the situation he is the work of the programmer, but as a tester still need to understand, after all, is also testing work, improve their own technology, expansion of the idea is the main, while learning to do notes, so learned things will not forget, Hee ~!

======== Study Environment =================

Java Editor: Eclipse SDK 3.5.6

JUnit Test Package: Eclipse has its own JUnit 4 test package

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

1. We open eclipse to create an engineering name: JUNIT4,

Create a package named: com.junit4.cc,

The package below creates a class T

The code is as follows:

 Package com.junit4.cc;
 Public class T {
Public int Add (int x,int y) {
return x + y;
}

}

How do we do the test, of course, the above code at a glance can see the results, need to test, we just want to explain the problem, if we do not have to test the framework, we will write a main method to verify the above code. The entire code is as follows.

 Package com.junit4.cc;
 PublicclassT {
PublicintAdd (intx,inty) {
returnx+y;
}
PublicStaticvoidMain (String args[]) {
intZ=NewT (). Add (3,5); Pass two parameters 3 and 5 to the T class
System.out.println (z); Print Z to see if 3 and 5 did an addition operation
}
}

The code above the cloud line determines whether the Add method is correct by looking at the results.

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

The above unit tests have many drawbacks:

@ It takes a lot of time to write the test code

The @ Test code is not reusable, and a test code corresponds to a section of the code being measured. The code changed, the test code is useless.

@ Cannot run multiple test code at the same time. If there are 100 tested code, one to run the test code, with the naked eye to observe the results, inefficient and error prone.

This is a few of the shortcomings that I summarize briefly.

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

2. Then we will use the unit test framework to deliver efficiency.

Let's use Eclipse to create a test. or test the above code for an example.

To see more clearly, we typically put the test code under a different package than the code being tested.

We re-created a package under the JUnit project named: com.junit4.cc.test

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

Create a test class: TTest

Note : Here to create a test class: Right-click on the com.junit4.cc.test package-->new-->junit test Case

Click "Next" will let you choose the method to test, select and click "Finish" to complete the creation.

Create complete such as:

The Ttest.java code is as follows:

 Packagecom.junit4.cc.test;

ImportStaticOrg.junit.Assert.*;

Importorg.junit.Test;
Importcom.junit4.cc.*;

PublicclassTTest {

@Test
PublicvoidTestadd () {
intZ=NewT (). Add (5,3);
Assertequals (8, z);
}

}

3. If we want to actually run the Ttest.java test class, we also need to introduce junit test packages to this project.

Right-click Project (JUNIT4)--->properties

Click "Next" button, Celipse provide JUNIT3 and JUNIT4 two test package, we choose JUNIT4 click "Finish" to introduce the test package.

4. Run our Ttest.java test code below.

See our Green scroll bar on the left, stating that our code is no problem.

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

int z=new T (). Add (5,3);
Assertequals (8,Z); Change the number 5, 3, 8 and so on to see the color of the green scroll bar, what is the hint?

(RPM) Use of the JUnit framework in Eclipse-unit testing

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.