Use unit tests to check PHP code on each layer

Source: Internet
Author: User
Use unit tests to check the PHP code on each layer <P> test-driven development and unit tests ensure that the code can still work as we do after correction and major adjustments. In this article, you will learn how

Test-driven development and unit testing are the newest way to ensure that the code can still work as we will see after correction and major adjustments. In this article, you will learn how to perform unit tests on your PHP code at the module, database, and UI layers.

It is am. How can we know that our code is still working?

The Web application is running 24x7 without interruption, so whether my program is still running this title will keep bothering me at night. Unit testing has helped me build enough confidence in my code-so that I can have a good night's sleep.

Unit testing is a framework for writing test cases for code and actively running these tests. Test-driven development is a unit test method. The idea is that you should first compile the test program and verify that these tests can create errors before you start to write the code that needs to pass these tests. When all tests expire, the features we developed will be completed. The value of these unit tests is that we can run them at any time-either after a major correction or after a running system is executed before the code is signed.

PHP Unit test

For PHP, the unit test framework is phpunit2. You can use the PEAR command line as a PEAR module to install the system: % pear install PHPUnit2.

After installing this framework, you can compile unit tests by creating a test class derived from PHPUnit2_Framework_TestCase.

Module unit test

I found that the best place to start unit testing is to use the business logic module of the program. I applied a simple example: this is a function that sums two numbers. To start the test, we first compile the test case, as shown below.

Listing 1. TestAdd. php

Require_once 'add. php ';
Require_once 'phpunit2/Framework/TestCase. php ';

Class TestAdd extends PHPUnit2_Framework_TestCase
{
Function test1 () {$ this-> assertTrue (add (1, 2) = 3 );}
Function test2 () {$ this-> assertTrue (add (1, 1) = 2 );}
}
?>

This TestAdd class has two methods, both of which apply the test prefix. Each method defines a test, which can be as simple and complex as listing 1. In this example, in the first Test, we simply judge that 1 plus 2 is 3, and in the second Test, 1 plus 1 is 2.

The PHPUnit2 system defines the assertTrue () method, which is used to test whether the condition value in the parameter is true. Then, we wrote the Add. php module, which initially produced a wrong result.


Listing 2. Add. php

Function add ($ a, $ B) {return 0 ;}
?>

When you run the unit test, both tests will fail.

Listing 3. test failed

% Phpunit TestAdd. php
PHPUnit 2.2.1 by Sebastian Bergmann.

FF

Time: 0.0031270980834961
There were 2 failures:
1) test1 (TestAdd)

2) test2 (TestAdd)

FAILURES !!!
Tests run: 2, Failures: 2, Errors: 0, Incomplete Tests: 0.

Now I know that both tests can work normally. Therefore, you can correct the add () function to actually do the actual thing.

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.