Using Xdebug Debugging and Unit test coverage analysis _php skills

Source: Internet
Author: User
Tags pear phpinfo zend
Today, I'm on my own. XdebugUse some of the experience to do a little bit of sharing.XdebugAlso because the need is used to generate coverage analysis files installed, just contact soon, usually used is not very frequent, but this is a good tool, if you want to rely on it to analyze the performance of the program or need to try their own hands. How good it is, please listen to me one by one.

<!--[if!supportlists]--> ,<!--[endif]--> installation article (XDebug and PHPUnit)

A : Install XDebug:

Xdebug Network download Xdebug dll file, stored in PHP loaded ext directory (you can choose to rename, such as Php_xdebug.dll, and then open php.ini files, adding configuration

[Xdebug]

Zend_extension_ts = Php_xdebug.dll

Xdebug.profiler_enable = On

Xdebug.trace_output_dir = D:\PHPAPP\XDebug

Xdebug.profiler_output_dir=d:\phpapp\xdebug

xdebug requires zend Engine load, so use zend_extension_ts or zend_extension,   TS is thread_safety, currently 5.3 more than support zend_ Extension load, <5.3 zend_extension_ts load (or you can view phpinfo whether zts ) is enabled, and then restart apache, view phpinfo, the finds xdebug option.


You can see xdebug.profiler_enable,xdebug.profiler_output_dir,xdebug.trace_. Output_dir These three are the items that we just configured, we can install their own needs to configure the remaining items,xdebug.profiler_enable: Open performance analysis,

Once this is turned on , the Cachegrind.out.XXX analysis file is generated in the Xdebug.profiler_output_dir directory , if xdebug.profiler_ is specified Output_name This, the output file will be the specified name . XXX Format (this method is not suitable for analysis, explained later),

xdebug.trace_options : This is the open tracking item,

After the item is opened, the trace file is produced under Xdebug.trace_output_dir, noting that it affects the speed at which it is run because he needs to record a series of events during the tracking process.

There is one more thing to note about XDebug :XDebug is debug, that is to say, configure the XDebug That program is running a debug state, so this time Zend_optimizer can not be used, always can not let the program that is debugging state and in the optimization State ha.

     more about xdebug , let's go online to discover it ~~ and then discuss it together.

Friendly reminder: for students using integrated environments, such as Wamp, it has two php.ini profiles, one under Apache , one under PHP ,Apache Under the influence of the Web , and PHP is affected by the command line, so (Thanks for June Brother's reminder )

B : Install PHPUnit:

About the installation process of PHPUnit, the internet has been a lot, before their own in order to finally install the success of the small text of the installation article (http://user.qzone.qq.com/414073277/blog/1292122868 , so here is no longer to repeat, some of the online installation tutorials can complete the installation. The process is simple:

PHPUnit installation is the Pear installation method, so if the machine has not been installed Pear package needs to be installed first, generally in the installation of PHP when the directory has a go-pear.bat files, directly executed to complete the installation, "here the installation needs to pay attention to expand Exif, this extension will be used in the mbstring extension of the method, so mbstring loading sequence needs to be Exif above. " after you finish installing pear, you can execute pear, see the command parameters of pear ,


Then execute sequentially :

Pear channel-discover pear.phpunit.de

Pear install–alldeps Phpunit/phpunit

Note that the current version of PHPUnit is 3.5 But this only pear version is 1.6 above and php5.2.x will be installed to get the latest version of the installation is otherwise installed is the first version (but does not affect the use).

Once installed, you can perform phpunit to View related information :


You can see that there are-coverage-html–coverage-clover–coverage-source three options that are available only after the XDebug installation is successful. These three are important options for exporting coverage files.

<!--[if!supportlists]--> II<!--[endif]-->PHPUnit Unit test Coverage Analysis

Unit test, one of the most important indicators is coverage. This point although Zend Studio can be very clear to see, but to export into a document is not very good, so this time XDebug can help busy, the use of a very simple, call the command can :

phpunit–coverage-html D:\PHPAPP\XDebug\ youtest

This will put the resulting overlay file (HTML ) in the D:\PHPAPP\XDebug\ directory,


Then you can find a bunch of HTML files in the directory below and run the next index.html


You can see such a picture, the English alphabet is very simple · The meaning is clear and does not explain the point:

The test results show that there are 5 kinds, the example above shows two kinds.

. : Represents the correct

F : Represents an assertion error

E : Represents a PHP program error or exception

I : Represents a method that is not implemented

S : represents a skipped method

Description of the production coverage document:

Classes: When all the methods in a class are overwritten, this class is tested

Functions/methods : Only one method or function of all valid statement code is executed to the method or the function is tested

Lines: The total number of rows will be filled with comments, blank lines,<?php?> labels, and declarations of classes and methods.

Using phpunit–coverage-clover D:\PHPAPP\XDebug\first.xml youtest

Production will be an XML format file, this format of the file effect is not obvious, there is no good HTML format, this side of the effect was I deleted, so there is the effect of the picture can be seen, the third overwrite the parameters of the file I have not tested, we are interested to try.

<!--[if!supportlists]--> III<!--[endif]--> program Performance Analysis

In addition to the effective analysis of unit test coverage, XDEBUG can help group analyze the bottleneck of program execution, and open the function of analysis:

Xdebug.profiler_enable = On

Xdebug.profiler_output_dir=d:\phpapp\xdebug

Set the above two, and then execute the program you want to analyze, you will see in the above directory Cachegrind.out.XXX file, this file directly open is some records, need to use the third party's cachegrind view tool, A handy tool is wincachegrind, look easy to find.


You can see the total time of execution, and the time each function executes.

Because this tool can only open cachegrind.out.* files when the file is opened, so the file to be analyzed must be the name, so in order to omit renaming, this is why I said that it is not recommended to modify the Xdebug.profiler_ Output_name The reason for this option.

<!--[if!supportlists]--> IV<!--[endif]--> XDebug Library functions

XDebug in addition to the above methods, there are some of their own function library to provide, can be in the Code section of your program to execute anywhere, specific please see http://xdebug.org/docs/all_functions

  xdebug features, and not only this Some, because his options are a great one page, so still go to http://xdebug.org/docs/ here to see the official instructions. Unreliable

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.