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 nota 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');
ClassRemoteconnecttestExtendsPhpunit_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 );
 &NBSP}
}
. ,
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.
Current 1/2 page
12 Next read the full text