PHP Coverage Test Tool Summary

Source: Internet
Author: User
Tags php framework

First, write in the top

This document is my previous period of work to summarize and share, oneself is also the first time to dabble in this knowledge, there must be omissions and deviations, and even "low-level error", so think of the role of the play, we share with each other, common progress. Can give a little inspiration to the work of the students, I am very satisfied.

Second, what is coverage? What does it reflect?

In short, coverage typically refers to the execution of a program (that is, a case), the ratio of executed code to the executable code (or the ratio of the total number of lines of code). It can reflect the quality of the case from one side, that is, whether the case does a full overwrite of the code.

Third, PHP How is coverage tested?

1. Xdebug

Xdebug is an extension of PHP, understand PHP students will not be unfamiliar to it, very powerful debugging assistant, the default does not open, need to install additional, but in most cases only need to open in the php.ini configuration file. After successfully opening Xdebug, we can use the following functions in the program:

Xdebug_start_code_coverage ()    //  function to start statistical coverage xdebug_get_code_coverage ()      //  function for getting current statistics xdebug_stop_code_coverage ()     //  function for end coverage statistics

Here's a simple example:

<? php  xdebug_start_code_coverage ();   // comments, blank lines do not count  Echo ' Hello world! ' ;   if false {      echo "Here is wrong\n";   Else {      echo "Here is rigth\n";  }   $data = xdebug_get_code_coverage ();  Xdebug_stop_code_coverage ();   // no statistics after stop

The coverage information is saved in the $data and the result is:

1 (2     Array 3         (4             [4] = 15             [6] = 16             [9] = 1 7             [1] + (8        )9 )

As you can see, the information about the file is saved in the $data, and the 4th, 6, 9, and 12 lines of code in the file are executed at this time (status code 1 indicates execution). Essentially, based on this information, we can count the coverage of the code. Various frameworks are also mostly based on the information obtained by XDEBUG for further processing.

But Xdebug's function is far from this, and then briefly introduces several other common functions of xdebug:

1.1 Get the currently running file, line number, and method

<? PHP     function fix_string () {        echo "        called @" ... ":".        . "from".         Xdebug_call_function ();    } Fix_string ();

out:called @/users/liufuxin/www/mars/index.php:8 from {main}

1.2 Gets the program's current elapsed time

<? PHP     echo xdebug_time_index (), "\ n"     ;  for ($i$i$i+ +)        {//  do nothing    }      Echo Xdebug_time_index ();? >


0.0014429092407227
0.015676975250244

1.3 Tracking Code Execution path

You can set Xdebug.auto_trace to on in Xdebug configuration, or Use function groups Xdebug_start_trace () and Xdebug_stop_trace () in your program to specify the code snippet for tracing. Automatically generate trace files in the configured output directory, more configuration

1.4 Xdebug Output File parsing

When Xdebug is turned on, the specified directory is generated with "cachegrind.out." Start the program run information file. It's roughly what it looks like:

Version:1Creator: xdebug 2.2.3cmd:/users/liufuxin/www/mars/index. phppart: 1positions: lineeventstimefl=php:internalfn=php:: Xdebug_time_index2 2fl=php:internalfn=php::xdebug_time_index6 0

At this time we need some parsing tools, recommend the use of PHP implementation of the parser Webgrind, it is easy to install, from the browser will automatically detect the Xdebug output directory, and then output the parse result:

You can clearly see the procedures involved in the execution of the function and its execution times, time-consuming and other basic information.

2. PHPUnit

Belong to the Xunit family series, for unit testing of PHP code, based on Xdebug can quickly and easily test the code coverage, and generate intuitive reports. Take a look at one of the following simple examples

Prepare two class files, bankaccount with the corresponding test class BankAccountTest, the code is as follows:

<?PHPclassBankAccount {protected $balance= 0;  Public functionGetBalance () {return $this-balance; }        protected functionSetbalance ($balance) {            if($balance>= 0) {                $this->balance =$balance; } Else {                Throw Newbankaccountexception; }        }    }?>
<?PHPrequire_once' Bankaccount.php '; classBankAccountTestextendsPhpunit_framework_testcase {protected $ba; protected functionsetUp () {$this->ba =NewBankAccount; }         Public functionTestbalanceisinitiallyzero () {$this->assertequals (0,$this->ba->getbalance ()); }    }?>

Then the terminal uses the PHPUnit command to execute the test file and specify the report as the HTML output format and output file.

PHPUnit--coverage-html./report bankaccounttest.php

Once executed, the report file can be viewed in a browser to see the code execution clearly:

More information

3. Codespy

Unlike the tools described earlier, Codespy is a lightweight coverage statistics tool for pure PHP development and does not rely on xdebug. By simply introducing the library file before the code being tested, the test report is automatically generated after the script has finished executing. This tool is an open source tool hosted on GitHub (GitHub Dafa is good!). )。 This time we take the common PHP framework thinkphp as an example, introduce a library file in the portal file, and make an API visit at the same time to view the generated test report. The entry file is as follows:

<?PHPinclude' Codespy.php '; \codespy\analyzer::$outputdir= '/users/liufuxin/www/codespy '; \codespy\analyzer::$outputformat= ' html '; if(Version_compare(php_version, ' 5.3.0 ', ' < ')) { die(' Require PHP > 5.3.0! ')); }Define(' App_debug ',true);
Define(' No_cache_runtime ',True);require'./protected/thinkphp/thinkphp.php ';?>

Codespy is easy to use, just include the library file, format the report, and output the directory. We then visit the API to view the report. Is the entire file covered by this visit, most of which is the framework code:

We are only interested in accessing the API, click on the specific action to see the details:

As you can see, the report highlights the code that was executed, as well as the coverage of the two dimensions, which is 6.78% of the statement coverage (statement coverage) and 2% of the row coverage (line coverage).

More information

4. Phpcoverage

The tools on the river map are used primarily to meet page-level automation tests, to count coverage information and generate reports, details. Unfortunately, no documentation has been provided, the interface has left, and the code is never functioning properly, but by looking at its source code, you can see that the tool is based on the two development of Phpcoverage, an open source project. Need to rely on Xdebug and xml_parser two extensions.

5. Pika

Also a tool on the river map, featuring support for manual testing and life cycle control, details. The general principle is to install and run the Pikagent program on the test machine, which can interact with the server, and QA can control the entire test flow through the server's Web interface, as shown in:

Detailed reading of the project from the initial research, to the late various conference documents, the project's earliest goal is to achieve the Baidu PHP code coverage testing tools, hope to be able to combine manual and automatic two working methods, while the internship test cycle controllable, that the report can contain multiple cases of the operation.

But the core of the central control machine has been unable to access, the project also announced the cessation of maintenance, it is a pity, but from his documents can also absorb a lot of reference, for example, my factory and the domestic PHP coverage of the Survey (2009), about the report generation, performance optimization, interactive design, etc. have a certain discussion and record.

Iv. comparison of Major programmes

1 , Overall scenario comparison

Scheme

Disadvantages

Advantages

Xdebug

1. Additional Installation extensions required

2. Complex output parsing

1. Best Performance

2. Flexible Code Insertion

3. Feature-rich

PHPUnit

1. Additional Installation extensions required

2. The case must be rewritten using the PHPUnit framework

3. Only unit tests are possible

1. Normalization

2. Result Analysis Optimal

Codespy

1. The slowest running speed

2. Single function

1. Easy Code Insertion

2. Open source, easy to expand

2 , performance comparison

More often we do not want to rewrite the case and additional code, the test scenario is mainly focused on API testing, PHPUnit more suitable for rd unit testing, so this article will be a small experiment compared to the performance of Xdebug and Codespy.

First, we take thinkphp as an example to calculate the time-consuming of both in a coverage statistic. Access an empty interface and count the average time-consuming.

Scheme

Number of experiments

Average Time (seconds)

Xdebug

100

0.05

Codespy

100

0.91

As you can see from the table, Xdebug does not cause much performance loss at all, while Codespy is nearly 20 times times more time-consuming!

Is it really so different? In doubt, another contrast test, this time we take a more time-consuming operation in the API, from the remote interface to obtain data (hundreds of orders of magnitude) to parse and write to the database, the results are as follows:

scheme

experiment times

API time consuming (seconds)

average Time (seconds)

Xdebug

1 XX

4.42

4.48

codespy

100

4.51

5.41

Unexpectedly, the performance loss of the two is not linear growth, xdebug time is basically kept in about 0.05 seconds, while the codespy is basically 1 seconds or so. But codespy than Xdebug performance is can be determined, and codespy in the actual production environment in the end, will be in the actual use after the sharing.

In conclusion, different schemes have different scenarios for which they are suitable. Xdebug is suitable for large-scale projects with complex test requirements, such as function overrides, class overrides, and it is also easy to interact with third-party tools; PHPUnit is primarily used for unit testing of modules, while its canonical case management is also suitable for large projects; Codespy is lightweight and easy to scale, Ability to test the coverage requirements of most small projects.

V. Last

The above is my research and study a little harvest, I hope to be able to play a certain role, welcome to shoot bricks. In addition, it is worth mentioning that the beginning or the thought of Hetu search, the results are more disappointing, the only few tools are not "years of disrepair", is no documentation, difficulty in getting started, I hope Baidu people can better summarize sharing, maintain our knowledge reserves.

VI. The last

The following posted some related resources, easy to build the environment, consult the documentation.

PHPUnit installation, Mac Please see here, the main introduction phpunit how to install.

PHPUnit instance, phpunit a more detailed introduction to the log.

PHPUnit online documentation, Chinese online documentation, very detailed.

Xdebug configuration, Introduction to detailed configuration of Xdebug.

Codespy Fork,codespy's github address.

PHP Coverage Test Tool Summary

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.