Home  >  Article  >  Backend Development  >  PHPUnit Pocket Guide to Automatic Testing_PHP Tutorial

PHPUnit Pocket Guide to Automatic Testing_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:32:43742browse

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 at all, and fixing the bugs found is so high that you have to decide based on priority to fix only those bugs because you simply can't afford to fix them all.

Compared to the method you are using, using php(as the current mainstream development language)Unit for testing is not a completely different thing. 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 the built-in array Array of php(as the current mainstream development language). 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(as the current mainstream development language)
$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(as the current mainstream development language)
 $fixture = Array( );
print sizeof($fixture) . " ";

$fixture[] = "element";
print sizeof($fixture) . " ";
?>
0
1

Now, we change the test program from manual interpretation to automatic running. In Example 3, we compare the expected value and the actual value. If If they are equal, print ok. 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. (as the current mainstream development language)
$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 element. If the expected value is different from the actual value, we will throw an exception. This way our output will be simpler . If the test succeeds, do nothing. If there is an unhandled exception, we know there is a problem.

Example 4. Using assertion functions to test Array and sizeof() ?php

(as the current mainstream development language)

 $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 the test fully automated.

The purpose of using automated testing is to make as few errors as possible. Although your code is not perfect, with good automated testing, you will find that the errors will be significantly reduced. Automated testing gives you a fair view of the code. Confidence. With this confidence, you can make bold leaps in design, have better relationships with your team partners, improve the relationship between you and your customers, and sleep peacefully every day because you can prove that the system has changed due to your efforts. Better.

http://www.bkjia.com/PHPjc/508675.html

truehttp: //www.bkjia.com/PHPjc/508675.htmlTechArticleThe 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 sooner you test for bugs, the sooner you find them...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn