The best programmers also make mistakes. The difference between a good programmer and a poor programmer is that a good programmer can detect errors as much as possible through testing. The quicker you test the bugs, the quicker you'll find them, and the lower the cost of discovering and correcting them. This explains why there are so many problems with testing only before the software is released. Most of the bugs are not found at all, and the fixes are so high that you have to prioritize to fix them, because you can't afford to pay for all the fixes.
Using PHPUnit to test is not a completely different thing than the method you're using. They're just different ways. The difference between the two is that checking whether the program behaves correctly is done through a batch of snippets that can be automatically tested. These pieces of code are called unit tests. In this section, we first automate the test based on the printed test code. Let's say we're testing PHP's built-in array of arrays. One of the required tests is the function sizeof (), and for any newly created array, the sizeof () function should return 0. When we join a new array member, sizeof () should return 1. Example 1 shows what we want to test.
Example 1. Test arrays and sizeof ()
<?php
$fixture = Array( );
// $fixture应该为空。
$fixture[] = "element";
// $fixture应该包含一个数组成员。
?>
The simplest test method is to print the results of the sizeof () before and after adding the array members, and if 0 and 1 are returned, the array and sizeof () are functioning properly.
Example 2. Test array and sizeof () with print statements
<?php
$fixture = Array( );
print sizeof($fixture) . "\n";
$fixture[] = "element";
print sizeof($fixture) . "\n";
?>
0
1
Now we let the test program change from manual interpretation to automatic operation. In Example 3, we compare the expected and actual values, and then print OK if equal. If we find that some of the results are not OK, we will know that there is a problem.
Example 3. Compare the expected 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
We now introduce a new element, and if the expected value is different from the actual, we throw an exception. So our output is much simpler. If the test succeeds, nothing is done, and if there is an unhandled exception, we know there is a problem.
Example 4. Use the Assert 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 tests are completely automated. Unlike our first version, this version makes the tests completely automated.
The purpose of using automated tests is to make as few mistakes as possible. Although your code is not perfect, with excellent automated testing, you will see a noticeable reduction in errors. Automatic testing gives you confidence in the integrity of your code. With this confidence, you can have a bold leap in design, be better with your team, improve your relationship with your customers, and go to sleep every day because you can prove that the system is getting better because of your efforts.