Test Practice: Junit of Eclipse (1)

zhaozj2021-02-17  57

Test Practice: Junit of Eclipse (1)

(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 we need to consider here.

l Unit Test: These are serviced for checking individual modules (such as Classes Class). 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 find 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. Running test

6. 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 At the time, when we developed the way in using TDD, 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.

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 string. 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 consisting of a set of associated TestCase, but we are not introduced in this article. (Untrained).

Test Practice: Junit of Eclipse (2)

http://www.9cbs.net/develop/read_article.asp?id=24223

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

New Post(0)