Do you encounter the following situations in the process of program development: When you have spent a lot of time developing an application, you think it should be done, unfortunately, when debugging, always constantly find bugs, and the most frightening is that these bugs are recurring, you may find that there are links between these bugs, But the problem is always not found.
When you encounter all of these frustrating situations, you will think that there is a better way to solve it? Of course it is! This is the use of unit tests. Unit test can not only solve the above headache problem to some extent, but also make the code easy to maintain, and can allow you to refactor the code more.
Once you've written a unit test case, when you need to modify your code, all you have to do is rerun your unit test case and see if the unit test case passes, and if it does, the proof code is OK.
People tend to say, "Since unit testing is so good, why are so many people still reluctant to write unit tests?" There are several false interpretations on the following:
1. Consider it a waste of time to write unit tests. Although many IDE tools now have a framework for writing unit tests, developers are asked to write code for unit tests. As with many of the best practices in development, doing the right thing in the right way can save a lot of time for development. Every time you add new features, you may be able to click on manual tests by visiting your Web pages, while running the built unit test cases faster than testing them manually.
2, that since the code can run, do not need to write unit tests. But assuming there are new members in the team, if there are no good unit test cases, the new members are likely to arbitrarily code without considering the consequences. If you have well-written unit tests, you can minimize bugs by performing various tests while the program is running.
3, think that writing unit test code is boring. The programmer's instinct is to solve the problem, and many programmers think it would be tedious to write unit test code when it comes to tight coding. But you know, if you can write unit tests at a very early stage to find as many errors in the code as possible, then save time and reduce errors, why not?
Start to install PHPUnit
In this article, we will introduce the Unit Test tool PHPUnit (http://phpunit.de/) in PHP and explain how to use phpunit in practical work through practical examples. The first way to install PHPUnit is to install it through the pear under 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 manually, refer to the PHPUnit Manual for installation (http://www.phpunit.de/manual/3.0/en/installation.html).
Writing the first unit test case
Here we start writing the first unit test case. When writing test cases, follow the following phpunit rules:
1 generally, in test cases, you can extend the Phpunit_framework_testcase class so that you can use such methods as Setup (), teardown ().
2 The name of a test case is best used in a conventional format, that is, after the test class is added "test", such as the class to be tested is remoteconnect, then the test case is named Remoteconnecttest.
3 All test methods in a test case should be named with the test+ test method name, such as Testdoeslikewaffles (), and note that the method must be declared to be of public type. Of course you can include private methods in your test cases, but they cannot be invoked by PHPUnit.
4 The test method cannot receive parameters.
Let's start with a simple example, the code is as follows:
<?php
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 above code is actually the function of connecting to a specified server, then we can write the test code as follows:
<?php
Require_once ("remoteconnect.php");
Class Remoteconnecttest extends Phpunit_framework_testcase
{
Public Function SetUp () {}
Public Function teardown () {}
Public Function Testconnectionisvalid ()
{
Test to ensure this object from a fsockopen is valid
$CONNOBJ = new Remoteconnect ();
$serverName = "www.google.com";
$this->asserttrue ($connObj->connecttoserver ($serverName)!== false);
}
}
?>
In the above code, because the Phpunit_framework_testcase class is inherited, no code needs to be written in the setup and Teardown methods. The setup approach is to perform some initialization work before each test case runs, while Teardown will perform some tasks such as releasing resources after each test case runs. In the test method, by using the PHPUnit assertion asserttrue to determine whether the returned Boolean value is True, this is done by calling the Connecttoserve method in remoteconnect.php to determine whether the server can be connected.
Next we run the unit test and enter the code at the command line:
phpunit/path/to/tests/remoteconnecttest.php, you can see the test passed, will output the following results:
PHPUnit 3.4 by Sebastian Bergmann
.
Time:1 Second
Tests:1, assertions:1, failures 0
As you can see, the above is passed the test. By default, PHPUnit is the one that will run all the test methods in the test case. Here are a few of the relevant assertions in the next phpunit:
Asserttrue/assertfalse whether the assertion is truth or false
Assertequals determine if output is equal to expected
Assertgreaterthan asserts whether the result is greater than a value, the same is LessThan (less than), greaterthanorequal (greater than or equal),
Lessthanorequal (less than or equal).
Assertcontains determines whether the input contains the specified value
Asserttype whether it belongs to the specified type
Assertnull to determine if null value
Assertfileexists determine if a file exists
Assertregexp based on the regular expression of judgment
For example to illustrate the use of such as Asserttype, still in the above example, you can use Asserttype to determine whether the Returnsampleobject returned object instance is Remoteconnect, the code is as follows:
<?php
function Testisrightobject () {
$CONNOBJ = new Remoteconnect ();
$returnedObject = $CONNOBJ->returnsampleobject ();
$this->asserttype ("Remoteconnect", $returnedObject);
}
?>
The current PHP framework for unit testing support
At present, many excellent PHP frameworks (such as Zend Framework,symfony, etc.) provide good support for unit testing. Take the Zend framework as an example, and explain how the unit tests are run.
<?php
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 of the framework of Zend itself, you can see, in the Zend, is through the inheritance Zend_test_phpunit_controllertestcase to Zend Controller to carry out unit test, As you can see, the unit tests in Zend are similar to those in PHPUnit, but add some new assertions, such as the assertcontroller above, and refer to the Zend Reference manual.
Summarize
In this paper, some basic knowledge of unit test and common misunderstandings are introduced, and some simple examples are given to illustrate how to use PHPUnit and some basic usage in PHP, in the next article, we will introduce more usage of phpunit.