Test Practice: Eclipse JUnit

xiaoxiao2021-03-06  44

Test Practice: Eclipse JUnit

Author: totodo Testing Practice: Eclipse of JUnit (a)

(Using Junit with eclipse IDE)

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. In extreme programming --- Extreme Programming (XP), Test-Driven Development (Test-Driven Development (TDD)] is a big problem. You can think of test-driven development (TDD) is "developing through testing" is tested.

The main terms of TDD: l 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. l After the test passes, the copy 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 that we need to consider (Unit Test): These are serving individual modules (such as Classes class) services. If the object needs to access the external data source, such as Database, you need to simulate Database through some analog objects (but this is only when the data in the real environment is different from the test environment. -, such as testing There is no real datebase in the environment, you need Mock Object)

l User Test: Here 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. l Integration Test: These test images are crossroads between user testing and unit testing. 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. l Developer's Test: This test is the developer checking the entire code, the new code, and the new function function. 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, things may be:

1. Go to this function of this function (possibly testing, but there are some small errors) 2. If such a test has not yet, or the test cannot cover this error, we write a new test to cover this error. 3. Now we are convinced that the program will not pass in new tests. 4. The bug in the repair program. 5. Run the test 6 again. Determine the program 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

l I use some integrated development environment (IDE) to find where to fix those classes and methods. l Manufacturing a known error environment to find errors in the code judgment. l The last but not least least improper, 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: l When we used TDD to develop The Class or Method that needs to be tested may not exist. l may also have 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 all of REBUILD. We are ready to start writing our "Hello World". Let us follow the TDD specification: Establish tests before encoding. To, we will leave the class name that we will write is HelloWorld's method SAY ().

To establish such a Test, right-click on the ProjectWithjunit title, select New-> Other, expand "Java", select JUnit. 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 TestThatWeGetHelloWorldPrompt extends TestCase {public TestThatWeGetHelloWorldPrompt (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 (TestttWegetHelloWorldprompt.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 consisting of a set of associated TestCase, but we are not introduced in this article.

Test Practice: Junit of Eclipse (2)

(Using Junit with eclipse IDE)

(carry on)

Establish our simple Test Case by following two steps;

1. Establish an instance of junit.framework.testcase.

2. Define some test functions starting with "TEST" and return an empty value. (Such as TestWastranscationsuccessful (), testshow (), etc.) .TesttHatwegetHelloWorldPrompt.java followed 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 difference here), it should be inconsistent, because the HelloWorld, which is established, let Say () returned by NULL).

The main () pig function is used to run TEST and display the result of the output. JUnit's Testrunnery performs our TEST and feedback information in the way (Swing.u) graphics 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 :).

So, according to the development of the drive-driven development, 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, failing Test. If you press Run it without automatic transfer to this window, you can click on the JUnit label on the TAB bar.

It's too good to run, and it's 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 a simple, you don't need a comment. 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 indicates the test.

Now, we need to let him fail again, but change this reason. 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 See what is seen in Failuer TRACE leads to errors.

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. .

The author introduces Alexander Prohorenko Advanced UNIX System Administrator, network security administrator.

OLEXIY PROHORENKO JAVA developers he lived in Ukraine, Roves (found in the football team :)

(Full text is finally translated)

Learn to test some resources:

JUINT Home: http://www.junit.org/index.htm

Support Struts Framework's JUnit component: http://strutstestcase.sourceforge.net/

About XP and website http://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.

Here I expressed apology to the authors of the original text, and my English teacher, I gave you old.

If there is a place where you have a child, please correct it in time, and I hope to test the expert with my rookie.

E-mail zhangli@telecomjs.com 2004-2-11

转载请注明原文地址:https://www.9cbs.com/read-76008.html

New Post(0)