Author: keld H. Hansen Translation: cmanlh original address
I. Introduction
2. Enter JUnit's paradise
Iii. Round class
4. Analyze and obtain test cases
Testing is a very easy task. Testing Programs Written by others is even more exaggerated. If you are also a developer, it may be difficult to upgrade. As developers, we are naturally keen on creation. But think about it, why not create a program to test other programs or even itself? This idea was born after automated testing. It is the topic of this article.
Automated unit testing (generally, a class is a unit) is nothing new. Some of the testing methods we applied to the mainframe many years ago are still effective from the perspective of Automated unit testing. Automated unit testing differs from random testing in the following ways:
1. This makes it very easy to retest only the modified parts (often called regression testing) after the program is modified.
2. Have a powerful test architecture. When you compile code or deploy applications, you can perform actual and effective tests automatically.
Correct use of unit tests can help us improve program writing efficiency and code quality. Only when you believe that unit testing is an important part of the programming process will it be promoted to design code that can withstand testing. In fact, it has become a trend to focus on the actions to be implemented by interfaces and classes, first writing test code and then program code. However, this article is a simple demonstration of unit testing, so the code used for testing actually exists.
Golf
I want to describe the whole process of automated testing with a small example in my life. So, let's play golf for a while! Case: we need to write a program to record a golf score. You are so smart that you soon know that you need the following two classes:
1. Course has the following attributes: name of the stadium; number of standard poles for each hole
2. A ball (round) (score of each player)-has the following attributes: name of the player (the player can actually be an object, but for simplicity, so I didn't do that); Stadium; number of Poles in each hole
We must all want to clearly understand the performance of every player on the court, so we define a score: A bar value above or below the standard bar. Therefore, if the first two holes of the standard rod are 4 and 5, and the actual ball uses 4 and 6, the score is "1" (that is, higher than the standard rod 1 ).
Stadium
The stadium name and the number of standard bars are implemented using a string and an int array respectively. In addition to the setter-and getter-methods, you also need a method to obtain the number of standard bars corresponding to each hole. We declare it as "paruptohole (INT n )". The following is the class code I provided:
Package Hansen. playground;
Import java. util .*;
Public class cource ...{
Private string name; // name Of cource
Private int [] par; // The par for each hole
/**//*
* Set the name of the course
*/
Public void setname (string name )...{
This. Name = Name;
}
/**//*
* Set the par for the cource
*/
Public void setpar (INT [] Par )...{
This. Par = par;
}
/**//*
* Get the number of holes for the course
*/
Public int getnumberofholes ()...{
Return par. length;
}
/**//*
* Return the par for a given hole
*/
Public int parforhole (INT hole )...{
Return par [hole-1];
}
/**//*
* Return the par from hole 1 and up to a given hole
*/
Public int paruptohole (INT hole )...{
Int sum = 0;
For (INT I = 0; I Sum + = par [I];
}
Return sum;
}
For simplicity, method call error check is not introduced in the example (that is, "setpar" must be called before the "getnumberofholes" method is called ").
Test the course class
By calling the course class method, you can write a program to test whether the "paruptoholes" method can obtain the expected results. We assume that the stadium is England's beautiful and famous "st. Andrews ":
Course c = New Course ();
C. setname ("st. Andrews ");
Int [] par =... {4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
C. setpar (PAR );
The sum of the 1st holes and the 2nd holes is obviously 4 + 4 = 8, so you can write the following code for testing:
If (C. paruptohole (2 )! = 8 )...{
System. Out. println ("*** error in course. paruptohole: par for 2 holes not 8 ");
}
If you run the above Code to obtain an error message, we have a bug to fix. If she only runs silently, haha, everything will be normal.
You can write a piece of code to test the 18 holes, but most programmers know that they only need to test the boundary values, that is, 0 and 18 holes, and the values in the middle can meet the requirements. For example:
If (C. paruptohole (0 )! = 0 )...{
System. Out. println ("*** error in course. paruptohole: par for 0 holes not 0 ");
}
If (C. paruptohole (2 )! = 8 )...{
System. Out. println ("*** error in course. paruptohole: par for 2 holes not 8 ");
}
If (C. paruptohole (8 )! = 72 )...{
System. Out. println ("*** error in course. paruptohole: par for 8 holes not 72 ");
}
The above code compares some values and then processes them based on the comparison results. Although it is actually effective, it lacks the code artistry. Through this program, we can perform a unit test on "paruptohole". After running the test, we will acknowledge with appreciation that this method works as scheduled.
In fact, we can make it more beautiful ......