PHP Unit testing tool PHPUNIT: page 1. 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. When you encounter these frustrating situations, unit testing will not only help you think about how to solve them? 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:
Php
ClassRemoteConnect
{
Public FunctionConnectToServer ($ 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 above code is actually used to connect to a specified server, so we can write the test code as follows:
Php
Require_once('RemoteConnect. php');
ClassRemoteConnectTestExtendsPHPUnit_Framework_TestCase
{
Public FunctionSetUp (){}
Public FunctionTearDown (){}
Public FunctionTestConnectionIsValid ()
{
//Test to ensure that the object from an fsockopen is valid
$ ConnObj = NewRemoteConnect ();
$ 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.
Why? Of course there are some solutions! This is the unit test. Unit tests can be performed not only in...