When a programmer writes a complete program, does he or she ensure that the program is correct? Of course, to test, programmers usually write a program to test the correctness of the target program. Such tests are programmer tests, known as white-box testing, because programmers know how the software being tested accomplishes functions and what (What) functions are done. This programmer-based test is called unit testing.
Unit Test principle
The principle of unit testing is actually very simple. Is the consistency of the test expectation and the actual results under certain conditions. For example, we write Java classes as shown in Listing 2.1 of the program, which has a method sum.
Package com.kingbegin;
public class A {public
int sum () {return
8;
}
}
We test the sum method of this class, we can do a test class, instantiate a class in the test class and call its sum method, if it actually returns the value and we expect the value (8) Consistent with the words that this class is written without problems, otherwise there will be errors. The test program is shown in Listing 2.2.
Package ch01;
Package com.kingbegin;
public class TestA1 {public
static void Main (string[] args) {
A A = new A ();
if (a.sum () = = 8) {
System.out.println ("Test results are correct!") ");
} else {
System.out.println ("The test result is incorrect!") ");
}
}
}
So we run the test program, we can test whether the test program is correct or not.