Test Practice: Junit (Using Junit with Eclipse IDE) of Eclipse
This article will introduce you to JUnit - a tool for engineering test. After introducing the test driver development theory, we continue to introduce "How to create you with Eclipse, JUnit Test". We will expose JUnit Case to you as a simple example as "Hello Word".
Automated Testing is introduced in many books, but rarely pays that how to organize these tests. When the test is written, it is difficult to know where to put these tests or use to call them. This has a big problem in the era of extreme programming - EXTreme Programming (XP), Test Drive Development Test-Driven Development (TDD). You can think of test-driven development (TDD) is "developing through testing" is tested.
The main terms of TDD:
Before any code snippet, you must first write a program that automatically detect this code function. Since the code does not exist, the test fails at the beginning.
After the test passes, the copied code must be deleted.
Each programmer in this way can be applied without a specific methodology. But before we start writing Test, it is worth noting that first considering how to organize automation tests.
There are several tests we need to consider here.
Unit test: These are serviced to check individual modules (such as Classes class). If the object needs to access the external data source, such as Database, you need to simulate Database by some simulated objects (but this is only different when the data is different from the test environment.
For example, there is no real datebase in the test environment, you need MOCK OBJECT)
User Test: This is a functional, systematic and approved test. All behaviors inspections in the system are made as a whole. In the XP theory, these tests are written by the user, give the test case outline.
Item Tests: These test images are crossroads between user testing and unit tests. Integrated Test Help Programs Test Several Level Interaction. Mock Object does not appear in the intersection test, he will increase the test time. Similarly, integrated tests often require a specific test environment, such as putting some test data from the database. Integrated tests may use external libs. CACTUS is the J2EE integrated lib. Interpreting these tests have exceeded the scope of this article, and also need detailed theoretical narrative, so you only need to know that this test is available.
Developer's Test: This test is those developers to check the entire code, new code, and new function functions. For each development, it is important to generate new tests at any time. Organize these tests and organize these code with the same importance.
As for other places in this article, as long as the "test" is special, it is specialized development test.
During development, a programmer may sometimes ask yourself: Is this behavior in the system? This Test exists, where can I find this test? Every time you find an error, it is a typical example of modifying bugs by the most basic. This is a typical example. In this case, the progress may be: go to the test of this function (may test it, but there are some small errors) If this test is not yet, or the test cannot cover this error, we write A new test to cover this error. Now we are convinced that the program will not pass in new tests. The bug in the repair program. Run the test determine that the program is passed in the test.
Of course, there may be a variety of treatments, but the ideas must be clear: You just need to correct those mistakes that are tested.
Now let's tell you how a developer solves this situation. Testing with the existence of functionality
I use some integrated development environment (IDE) to find where to fix those classes and methods.
Manufacturing a known error environment to find that those code judgment exist errors.
Finally, not least, write a good test and put it in an existing test class. If you accidentally mistaken, you can notice that you can pay attention to your copy and correct it.
All is ready, start building a test, so I need to take a name to the test. You may say, "This is not a problem: adding a TEST in front of each class!" But not that simple, let me tell you that if possible:
At that time, when we developed the use of TDD, the Class or Method that required tests may not exist.
It may also be a Test to cover several methods, even several classes.
These are just the most common problems, there are more below.
Suggestions on Test Name: Test classes should first express this class is a Test class, and it can be exactly what he wants to check, there is this original Class name. In fact, this is easy, please don't worry that this name will become very long or ugly, and how you can take it.
Below we will build our first test using the JUnit tool in Eclipse, assuming that you have downloaded the current version of this product, if not, you can freely download it from its official website (www.eclipse.org). We need JUnit, you can also download from its official website (www.junit.org), download and decompress the place where Java Libaries stores in your hard drive.
Open Eclipse. We will create a new workplace point-> New -> Project, select Java all the way next. Enter the project name (Project Name), such as ProjectWithjunit. Click Finish. This creates a new project, let us configure our Eclipse, so we add JUnit Library to build path. Click Project -> Properties, select Java Build Path Libraries, click Add Exteranal Jars to select JUnit.jar. You will see that JUnit will appear on the screen of the libraries list. Point Okay, Eclipse will enforce all Build Paths.
We are ready to start writing our "Hello World". Let us follow the TDD specification: establish a test before encoding. To, we will leave the name of the class name that we will write is HelloWorld's method for returning a string. Say (). To create such a Test, right-click on the ProjectWithjunit title, select New-> Other, expand "Java" On the right side of the dialog, choose TestCase, then click Next. See Figure 1.
figure 1. Established JUnit Test in Eclipse
In Test Class: Enter the Class - HelloWorld we need to test. And give Test Case, such as TesttttWegetHelloWorldPrompt (yes, this looks too long, but it can clearly express its meaning) point finish completion.
Below is the code of TesttHatwegetHelloWorldprompt.java:
Public Class TestttathWegetHelloWorldPrompt
Extends testcase {
Public TesttathWegetHelloWorldPrompt (
String name) {
Super (Name);
}
Public void testsay () {
HelloWorld Hi = New HelloWorld ();
Assertequals ("Hello World!", Hi.SAY ());
}
Public static void main (String [] args) {
Junit.textui.teStrunner.run
TesttathWegetHelloWorldprompt.class;
}
}
This code is not complicated at all, just a little special. Anyway, let us check it in detail. We inherited the TestCase of Junit. (Testcase defined in Junit's Javadoc is "fixed device used to run multiple TESTs"). Junit also defines TestSuite because of a set of associated TestCase..
Establish our simple Test Case by following two steps;
Establish an instance of junit.framework.testcase. Define some test functions starting with "TEST" and return an empty value. (Such as TestWastranscationsUccessful (), TestShow (), etc.).
TestttWegetHelloWorldPrompt.java simultaneously follows these standards: These TestCase subclasses contain a TestSay () method. This method is called by the assertequals () method for verifying the return value of SAY () (according to the way it is returned, because At the beginning, the HelloWorld we let Say () return to NULL).
The main () master function is the result of running TEST and displays the output. JUnit's Testrunnex performs our TEST and feedback information in a (Swing.u) graphic and this article (Text.ui). We use text (Text.ui), this Eclipse is affirmed. (Translation: There may be no translation here, the so-called text and graphic, refers to the time when establishing Testcase, there is a option, which method stubs would you like To create, choose Text.ui || Swing.ui || AWT.UI, usually selection text.ui because Eclipse is definitely supported, in accordance with these text information, Eclipse simultaneously generates graphic display. (I will see multiple junit on the Tab of Package Exploer, and I will see it :). Another thing, according to the practice of testing driven now, once we ran our Test, we should see the information returned to some errors. Point Run-> Run As -> JUnit Test (note, this test.java should be in Package Explorer, in the left of the Window), you point to the JUnit Window (that is the TAB bar below, Note that it is not Package Exploer, so you will see JUnit Window, he will display a red strip, indicating a failure Test. (If you press running it does not automatically go to this window, you can click on the JUnit label on the TAB bar.)
One is running, it is great, and it is wrong. Ok, now officially start building the HelloWorld code used to work, --- Point New-> Class, possibly with the original repetition, then delete it. code show as below:
Hellowolrd.java
Public class helloworld {
Public string say () {
Return ("Hello World!");
}
}
This is simply, it is not covered. Test now to see the results. Just use the above method, click Run-> Run as jnit. A green strip appears in the JUnit window on the left. See Figure 3. The green strip appears indicates the test.
Now, change the condition and let the test pass. This will help us understand how the JUnit Test covers and reports different errors. Edit the assertequals () method, turn its return value from "Hello World!" To another value such as "Hello Me!". This way, when you run this JUnit Test, the display strip becomes red, and It seems that it has caused a mistake in Failuer Trace. Figure:
to sum up. I want to say some of my own ideas (here is still the original text is not translated). I didn't think that the test code was a very important part of the development process. However, in recent years, it has developed very quickly, thanks to those methodology (such as exception development "Exceptions-Based Development", they have promoted testing and testing tools.
If you are interested in this article, you can spend some time to learn the test theory and apply it to your work. .
Learn to test some resources:
Juint Home
Support Struts framework JUnit Components: newhttp: //strutstestcase.sourceforge.net/
About XP and website newhttp://www.chianxp.org
"Refactoring" Chinese "Refactoring" Hou Jie translated. .
In short, I think this is a very simple entry article, it is easy to understand, so it is translated by the face.
TotoDo translation, limited level, error, please indicate in time, thank you very much.