PHP Unit testing tool PHPUNIT

Source: Internet
Author: User
Do you encounter the following situations during the process of program development: After you have spent a long time developing an 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 when you encounter these frustrating situations, 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 above code is actually used to connect to a specified server, so we can write the test code as follows:

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.

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.