Accueil / Calculating test coverage

Calculating test coverage

Here's how you can determine test coverage of your tests by generating a report like this one:

Step 1: Install xdebug php extension. On ubuntu I run
apt-get install php5-xdebug

Step 2: Checkout simpletest trunk anywhere on your disk. In this example I will choose /usr/local/simpletest
svn co \ \ /usr/local/simpletest

Step 3: Add simpletest to the php.ini loaded by apache.
On ubuntu the file is /etc/php5/apache2/php.ini. You'll want to preserve the default paths as well. You'll also need to restart apache after you make your edit.
include_path = ".:/usr/share/php:/usr/local/simpletest/extensions/coverage"

Step 4: Log into your system and navigate your web browser to admin page where you run unit tests, but don't run the tests just yet and never run unit tests on a production system!

Step 5: As a system user with permissions that can create files that your web server user can modify, initialize test coverage. Pass parameters to include and exclude coverage on files you care and don't care about.
cd {your web docroot} php /usr/local/simpletest/extensions/coverage/bin/php-coverage-open.php  -- \  '--include=modules/.*\.php$' \  '--include=modules/.*\.inc$' \  '--include=modules/.*\.module$' \  '--include=includes/.*\.inc$' \  '--exclude=/net/.*' \  '--exclude=/usr/.*'

Step 6: Add hook to Drupal to calculate coverage by editing settings.php and adding
require 'autocoverage.php';

Step 7: Run your tests from Drupal's web UI

Step 8: Close your coverage
php /usr/local/simpletest/extensions/coverage/bin/php-coverage-close.php

Step 9: Generate the report
php /usr/local/simpletest/extensions/coverage/bin/php-coverage-report.php

Step 10: Open your report. It's located in coverage-report/index.html

* Generating the coverage report for D7 takes 64MB+ of ram so edit the php.ini for CLI appropriately
* If you do not like the coverage report look, templates for coverage can be modified in simpletest/extensions/coverage/templates/*.php
* Coverage is independent of any unit test environment, including the 'simpletest' module in core. For example you can turn this on while running a usabilty test and see area of the code that are excersized and not excersized.
* You can save out the sqlite coverage database coverage.sqlite and rerun reports as you modify the templates, so that you don't have to rerun tests just to change to report formatting.
* You can only specify the include/exclude patterns as you open coverage so you may have to experiment coming up with the right regex's before running a long test run.
* If you run your tests from drupal's CLI script ./script/ then to calcualte full coverage you should be able to use php's auto_prepend ini setting to include 'autocoverage.php' in CLI environment.


Posted on by wulff (non vérifié).

I had two issues when I tried to get this running on my Ubuntu machine at work:

1) I had to grant the webserver write access to the Drupal root dir (because sqlite needs to create an extra file while running).

2) I had to make a couple of changes to simpletest to make it run without errors:

Index: extensions/coverage/coverage_utils.php
--- extensions/coverage/coverage_utils.php (revision 1872)
+++ extensions/coverage/coverage_utils.php (working copy)
@@ -12,10 +12,10 @@

static function requireSqlite() {
- if (!self::isPackageClassAvailable('DB/sqlite.php', 'SQLiteDatabase')) {
- echo "sqlite library is required to be installed and available in include_path";
- exit(1);
- }
+// if (!self::isPackageClassAvailable('DB/sqlite.php', 'SQLiteDatabase')) {
+// echo "sqlite library is required to be installed and available in include_path";
+// exit(1);
+// }

static function isPackageClassAvailable($includeFile, $class) {
Index: extensions/coverage/coverage_data_handler.php
--- extensions/coverage/coverage_data_handler.php (revision 1872)
+++ extensions/coverage/coverage_data_handler.php (working copy)
@@ -1,6 +1,6 @@

-require_once 'DB/sqlite.php';
+//require_once 'DB/sqlite.php';

* Persists code coverage data into SQLite database and aggregate data for convienent