The best programmers make mistakes. The difference between good programmers and bad programmers is that good programmers can find as many errors as possible through testing. The quicker you test for bugs, the quicker you find them and the cheaper they are to find and fix. This explains why testing software just before it's released is so problematic. Most bugs are never found, and fixing the bugs found is so high that you have to prioritize fixing only those bugs because you simply can't afford to fix them all.
Testing with PHPUnit is not a completely different thing than the approach you are using. They just have 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 part, we first perform automatic testing based on the printed test code. Suppose we want to test PHP's built-in array Array. One of the tests that needs to be done is the function sizeof(). The sizeof() function should return 0 for any newly created array. When we add a new array member, sizeof() should return 1. Example 1 shows what we want to test.
Example 1. Test array and sizeof()
<?php
$fixture = Array( );
// $fixture should be empty.
$fixture[] = "element";
// $fixture should contain an array member.
?>
The simplest test method is to print the operation results of sizeof() before and after adding array members. If 0 and 1 are returned, it means that Array and sizeof() are operating normally.
Example 2. Use print statements to test Array and sizeof()
<?php
$fixture = Array( );
print sizeof($fixture) . "n";
$fixture[] = "element";
print sizeof($fixture) . "n";
?>
0
1
Now, we move the test program from requiring manual interpretation to running it automatically. In Example 3, we compare the expected value with the actual value and print ok if they are equal. If we find that some results are not ok, we know there is a problem.
Example 3. Compare the expected and actual values of Array and sizeof()
<?php
$fixture = Array( );
print sizeof($fixture) == 0 ? "okn" : "not okn";
$fixture[] = "element";
print sizeof($fixture) == 1 ? "okn" : "not okn";
?>
OK
ok
we now introduce a new element, if the expected value is different from the actual value, we throw an exception. This way our output is simpler. If the test succeeds, nothing is done, if there is an unhandled exception, we know something is wrong.
Example 4. Using assertion functions 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 fully automated. Unlike our first version, this version makes testing completely automated.
The goal of using automated testing is to make as few mistakes as possible. Even though your code isn't perfect yet, with good automated testing you'll find that errors will be significantly reduced. Automated testing gives you confidence that your code is fair. With this confidence, you can make bold leaps in design, partner better with your team, improve relationships between you and your customers, and sleep peacefully every day because you can prove that the system is better because of your efforts. .