The best programmers also make mistakes. The difference between a good programmer and a bad programmer is that a good programmer can test the error as much as possible. The faster you test for errors, the quicker you will find them, and the lower the cost of finding and correcting them. This explains why it's so much more problematic to test only before the software is released. Most errors are not found at all, and the bugs found are so high that you have to prioritize to fix only those errors, because you simply can't afford to fix them all.
Using PHP (as the current mainstream development language) unit for testing is not a completely different thing than the method you are using. They are just different methods. The difference between the two is that checking whether the program behaves correctly is done through a batch of code snippets that can be automatically tested. These code snippets are called unit tests. In this section, we first Test automatically based on the printed test code. Suppose we want to test the built-in array of PHP (which is now the mainstream development language). One of the required tests is the function sizeof (), 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 array and sizeof ()
$fixture = Array ();
The $fixture should be empty.
$fixture [] = "element";
$fixture should contain an array member.
? >
The simplest test method is to print the results of sizeof () before and after joining the array members, and if 0 and 1 are returned, the array and sizeof () are working correctly.
Example 2. Test array and sizeof () with a print statement
$fixture = Array ();
Print sizeof ($fixture). "";
$fixture [] = "element";
Print sizeof ($fixture). "";
? >
0
1
Now, let's get the test program to run automatically from the manual explanation. In Example 3, we compare the expected and actual values, and if they are equal, print OK. If we find that the results are not OK, we know there is a problem.
Example 3. Compare the expected and actual values of array and sizeof ()
$fixture = Array ();
Print sizeof ($fixture) = = 0? "OK": "Not OK";
$fixture [] = "element";
Print sizeof ($fixture) = = 1? "OK": "Not OK";
? >
Ok
Ok
We now introduce a new feature that throws an exception if the expected and actual values are different. This makes our output much simpler. If the test succeeds and does nothing, if there is an unhandled exception, we know there is a problem.
Example 4. Use the Assert function to test array and sizeof ()
$fixture = Array ();
Asserttrue (sizeof ($fixture) = = 0);
$fixture [] = "element";
Asserttrue (sizeof ($fixture) = = 1);
function Asserttrue ($condition) {
if (! $condition) {
throw new Exception ("Assertion failed.");
}
}
? >
The test is now fully automated. Unlike our first version, this version makes testing completely automated.
The purpose of using automated tests is to make as few mistakes as possible. Although your code is not perfect, with good automated testing, you will find that errors can be significantly reduced. Automated testing gives you confidence in the fairness of your code. With this confidence, you can have a bold leap in design, better relationships with your team, improved relationships between you and your customers, and a daily peace of mind as you can prove that the system has become better because of your efforts.
http://www.bkjia.com/PHPjc/508675.html www.bkjia.com true http://www.bkjia.com/PHPjc/508675.html techarticle The best programmers also make mistakes. The difference between a good programmer and a bad programmer is that a good programmer can test the error as much as possible. The faster you test your mistakes, the quicker you'll find them ...