Do you encounter the following situations during the process of program development: After you have spent a long time developing a PHP application, you think it should be a success. Unfortunately, when debugging, bugs are constantly discovered, and the most terrible thing is that these bugs are repeated. you may find these bugs... "> <LINKhref =" http://www.php100.com//statics/style/h
Do you encounter the following situations during the process of program development: After you have spent a long time developing a PHP application, you think it should be a success. Unfortunately, when debugging, bugs are constantly discovered, and the most terrible thing is that these bugs are repeated. you may find that these bugs are associated, but you can't find the problem.
When you encounter these situations that make you frustrated, you will definitely think about a better solution? Of course there are some solutions! This is the unit test. Unit testing not only solves the above headaches to a certain extent, but also makes the code easy to maintain and allows you to refactor the code more.
Once you write unit test cases, when you need to modify your code, you have to re-run your unit test cases and observe whether these unit test cases can pass, if the code passes, the code is okay.
People often say: Since unit tests are so good, why are so many people reluctant to write unit tests? There are several misunderstandings:
1. it is too time-consuming to write unit tests. Although many IDE tools have established a framework for writing unit tests, developers still need to write some unit test code. Like many best practices in development, doing the right thing in the right way will save a lot of time for development. When new features are added, you may access your web page and click manually to test the unit test cases, which is faster than manually testing the unit test cases.
2. since the code can run, you do not need to write unit tests. However, if there are new members in the team and there are no good unit test cases, it is very likely that the new members will code at will without considering the consequences. If you have a well-written unit test and perform various tests while running the program, you can avoid bugs to the maximum extent possible.
3. think that unit test code writing is boring. Programmers solve the problem by nature. many programmers think that unit test code writing is boring during intense coding. However, if you can discover as many errors as possible in the code by writing unit tests at an early stage, it will save time and reduce the number of errors. why not?
Start to install PHPUnit
This article will introduce PHP's unit testing tool PHPUnit (http://phpunit.de/), and explain how to use PHPUnit in practical work through practical examples. First, you can install PHPUnit using pear in PHP:
Pear channel-discover pear.phpunit.de
Pear channel-discover components. ez. no
Pear channel-discover pear.symfony-project.com
Pear install phpunit/PHPUnit
If you want to install it manually, you can refer to the PHPUnit manual to install (http://www.phpunit.de/manual/3.0/en/installation.html ).
Write the first unit test case
Next we will write the first unit test case. When writing test cases, follow the following PHPUnit rules:
1 generally, you can extend the PHPUnit_Framework_TestCase class in the test case, so that you can use methods such as setUp () and tearDown.
2. the name of the Test case should be in a conventional format, that is, add "Test" after the tested class. for example, if the class to be tested is RemoteConnect, the name of the Test case is RemoteConnectTest.
3. all test methods in a test case should be named with the test + test method name, such as testDoesLikeWaffles (), note that the method must be declared as public. Of course, you can include private methods in your test cases, but they cannot be called by phpunit.
4. parameters cannot be received in the test method.
The following is a simple example. the code is as follows:
Class RemoteConnect
{
Public function connectToServer ($ serverName = null)
{
If ($ serverName = null ){
Throw new Exception ("That's not a server name !");
}
$ Fp = fsockopen ($ serverName, 80 );
Return ($ fp )? True: false;
}
Public function returnSampleObject ()
{
Return $ this;
}
}
?>
The following code is used to connect to a specified server:
Require_once ('remoteconnect. php ');
Class RemoteConnectTest extends PHPUnit_Framework_TestCase
{
Public function setUp (){}
Public function tearDown (){}
Public function testConnectionIsValid ()
{
// Test to ensure that the object from an fsockopen is valid
$ ConnObj = new RemoteConnect ();
$ ServerName = 'www .google.com ';
$ This-> assertTrue ($ connObj-> connectToServer ($ serverName )! = False );
}
}
?>
In the above code, because it inherits the PHPUnit_Framework_TestCase class, you do not need to write any code in the setUp and tearDown methods. The SetUp method initializes each test case before running, while tearDown performs some operations such as resource release after running each test case. In the test method, assertTrue of PHPUnit is used to determine whether the returned Boolean value is true. In this example, you can call the connectToServe method in RemoteConnect. php to determine whether the returned Boolean value can be connected to the server.
Next, run the unit test and enter the code in the command line:
Phpunit/path/to/tests/RemoteConnectTest. php:
PHP Unit 3.4 by Sebastian Bergmann
.
Time: 1 second
Tests: 1, Assertions: 1, Failures 0
As you can see, the test is passed. By default, PHPUnit runs all the test methods in the test case. Next we will introduce several assertions related to PHPUnit:
AssertTrue/AssertFalse whether the asserted is true or false
AssertEquals determines whether the output is equal to the expected
AssertGreaterThan asserted whether the result is greater than a certain value. Similarly, LessThan (less than), GreaterThanOrEqual (greater than or equal ),
LessThanOrEqual (less than or equal ).
AssertContains determines whether the input contains the specified value
AssertType determines whether it belongs to the specified type
AssertNull determines whether it is null
AssertFileExists
AssertRegExp based on regular expression
For example, to illustrate the use of AssertType, we can use AssertType to determine whether the object instance returned by returnSampleObject is remoteConnect. the code is as follows:
Function testIsRightObject (){
$ ConnObj = new RemoteConnect ();
$ ReturnedObject = $ connObj-> returnSampleObject ();
$ This-> assertType ('remoteconnect ', $ returnedObject );
}
?>
Currently, the PHP framework supports unit testing.
Currently, many excellent PHP frameworks (such as Zend Framework and Symfony) provide excellent support for unit testing. Take Zend Framework as an example to describe how to run a unit test.
Class CommentControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
Public function setUp ()
{
Parent: setUp ();
}
Public function tearDown ()
{
Parent: tearDown ();
}
Public function appBootstrap ()
{
$ This-> frontController-> registerPlugin (new Initializer ('test '));
}
Public function testGoHome ()
{
$ This-> dispatch ('/home ');
$ This-> assertController ('Home ');
}
}
?>
The above code is actually a unit test on the Zend Framework. we can see that in Zend, Zend_Test_PHPUnit_ControllerTestCase inherits Zend_Test_PHPUnit_ControllerTestCase to perform unit tests on Zend controllers, we can see that the unit test in zend is similar to that in PHPUnit, but some new assertions are added, such as the assertController above. for details, refer to the Zend reference manual.
PHPUnit is a lightweight PHP testing framework. It is a full migration of the JUnit3 series under PHP5 and a member of the xUnit Testing framework family (both of them are based on the pattern pioneer Kent Beck's design ).
Unit testing is the basis of several modern Agile development methods, making PHPUnit a key tool for many large PHP projects. This tool can also be used by the Xdebug extension to generate code coverage reports, and can be integrated with phing for automatic testing. Finally, it can be integrated with Selenium to complete large-scale automated integration testing.