PHPUnit pocket guide for proactive testing

Source: Internet
Author: User
The best programmers may also make mistakes. The difference between a good programmer and a poor programmer is that a good programmer can make as many mistakes as possible by testing. The sooner you test errors, the sooner you invent them, and the lower the cost of invention and correction. This shows why the best programmer is wrong. The difference between a good programmer and a poor programmer is that a good programmer can make as many mistakes as possible by testing. The sooner you test errors, the sooner you invent them, and the lower the cost of invention and correction. This explains why there are so many titles for testing only before the software announcement. Most mistakes are basically not invented, and the mistakes corrected are so high that you have to decide to correct only those mistakes based on the priority, because you basically cannot afford all the costs of correction.

Compared to the method you are applying, using PHPUnit for testing is not totally different. They only have different methods. The difference between the two lies in that it is done by a batch of code snippets that can be actively tested to check whether the program action is accurate. These code snippets are called unit tests. In this section, we first perform an active test based on the printed test code. Suppose we want to test the built-in Array of PHP. One of the tests is the sizeof () function. for any newly created array, the sizeof () function should return 0. When we participate in a new array member, sizeof () should return 1. Example 1 shows what we want to test.

Example 1. test the array and sizeof ()

<? Php
$ Fixture = Array ();
// $ Fixture should be empty.

$ Fixture [] = 'element ';
// $ Fixture should contain a group of several members.
?>
The simplest test method is to print the calculation result of sizeof () before and after the Array members. if 0 and 1 are returned, the Array and sizeof () are running normally.

Example 2: Use a print statement to test Array and sizeof ()

<? Php
$ Fixture = Array ();
Print sizeof ($ fixture). '\ n ';

$ Fixture [] = 'element ';
Print sizeof ($ fixture). '\ n ';
?>
0
1
Now let's change the test program from manual instructions to active running. In example 3, we compared the period value to the actual value. if the value is equal, the OK is printed. If the results we have invented are not OK, we will know that there is a title.

Example 3. compare the period value and actual value 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. if the value is different from the actual value, we will throw an exception. In this way, our output is simpler. If the test is successful and nothing is done, if there is an unhandled exception, we know that there is a title.

Example 4: Use the assertion 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 test is complete and active. Unlike our first version, this version makes the test complete and active.

The application actively tests to minimize errors. Although your code is not perfect, with excellent active testing, you will find that the error solution is significantly reduced. Proactive testing gives you confidence in code fairness. With this confidence, you can have a brave Flying Flight in design, better relationship with your team, improve the relationship between you and your customers, and go to bed with peace of mind every day, because you can confirm that the system has become better due to your efforts.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.