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 ();
The $fixture should be empty.
$fixture [] = "element";
$fixture should contain an array member.
? >
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
$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 ()
$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 ()
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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.