PHPUnit pocket guide-automated testing-read the PHPUnit pocket guide for automated testing, and the best programmers will make mistakes. The difference between a good programmer and a poor programmer is that a good programmer can discover errors as much as possible through testing. The faster you test errors, the faster you discover them and the lower the cost of discovery and correction. This explains why <LINKhref = "http: // www. php100
The best programmers also make mistakes. The difference between a good programmer and a poor programmer is that a good programmer can discover errors as much as possible through testing. The faster you test errors, the faster you discover them and the lower the cost of discovery and correction. This explains why there are so many problems with the practice of testing only before the software is released. Most errors are not found at all, and the errors found by correction are so high that you have to determine the priority to only correct those errors, because you cannot afford the total cost of correction.
Compared to the method you are using, using PHPUnit for testing is not totally different. They only have different methods. The difference between the two lies in that it is done by checking whether the program behavior is correct through a batch of code snippets that can be automatically tested. These code snippets are called unit tests. In this section, we perform automatic testing based on the printed test code. Suppose we want to test the built-in Array of PHP. One of the tests is the sizeof () function. for any newly created array, the sizeof () function should return 0. When we add a new array member, sizeof () should return 1. Example 1 shows what we want to test.
Example 1. test the array and sizeof ()
<? Php
$ Fixture = Array ();
// $ Fixture should be empty.
$ Fixture [] = "element ";
// $ Fixture should contain a group of several members.
?>
The simplest test method is to print the sizeof () calculation result before and after adding the Array members. if 0 and 1 are returned, Array and sizeof () are running normally.
Example 2: Use a print statement to test Array and sizeof ()
<? Php
$ Fixture = Array ();
Print sizeof ($ fixture). "\ n ";
$ Fixture [] = "element ";
Print sizeof ($ fixture). "\ n ";
?>
0
1
Now let's change the testing program from manual interpretation to automatic operation. In example 3, we compared the expected value and the actual value. if the values are equal, print OK. If we find that some results are not OK, we will know that there is a problem.
Example 3. compare the expected values and actual values of Array and sizeof ()
<? Php
$ Fixture = Array ();
Print sizeof ($ fixture) = 0? "OK \ n": "not OK \ n ";
$ Fixture [] = "element ";
Print sizeof ($ fixture) = 1? "OK \ n": "not OK \ n ";
?>
OK
OK
Now we introduce a new element. if the expected value is different from the actual value, we will throw an exception. In this way, our output is simpler. If the test is successful, nothing will be done. if there is an unhandled exception, we know there is a problem.
Example 4. use the assertion function to test Array and sizeof ()
<? Php
$ Fixture = Array ();
AssertTrue (sizeof ($ fixture) = 0 );
$ Fixture [] = "element ";
AssertTrue (sizeof ($ fixture) = 1 );
Function assertTrue ($ condition ){
If (! $ Condition ){
Throw new Exception ("Assertion failed .");
}
}
?>
Now the test is completely automated. Unlike our first version, this version makes the test completely automated.
The purpose of using automated testing is to make as few mistakes as possible. Although your code is not perfect, with excellent automatic testing, you will find errors significantly reduced. Automatic testing gives you confidence in code fairness. With this confidence, you can have a bold flying flight in the design, better relationship with your team, improve the relationship between you and your customers, and fall asleep with peace of mind every day, because you can prove that the system has become better due to your efforts.