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 that these bugs are associated, but you can't find the problem. 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 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, refer to the phpunit manual for installation (https://www.php1.cn /).

  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:

 

The above code is actually used to connect to a specified server, so we can write the test code as follows:

 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:

 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.

 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.

Summary: In this article, we will give a preliminary introduction to some basic knowledge and common mistakes in unit testing, and give a simple example of how to use phpunit and some basic usage in php.

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.