Recent improvements in PHPUnit-support
By Tomas Mysik on Jul 30, 2009
Recent improvements in PHPUnit-support
This blog post has been contributed by Thomas Meyer who has helped a lot with these improvements. Thanks a lot, Thomas!
Today I want to introduce some new features and fixes, that will greatly improve the support for - and usability of - PHPUnit in NetBeans.
Enabling PHPUnitFirst allow me to explain, how to enable PHPUnit in NetBeans. Click the menu "Tools" > "Options" and select the "PHP" tab. The screen below shows the two options that you need to change.
First: Enter the path to the command line script you wish to run in NetBeans. This requires that you have a local copy of PEAR and PHPUnit 3.3 already installed on your system.
Second: To enable code-completion for your test cases and test suites, please specify the path to PHPUnit's class-files.
New options in project-properties
You will now find options to configure PHPUnit for you project within the project properties menu. Just right-click on your test-folder and choose "properties" to bring up the dialog.
You may choose a custom configuration XML-file, bootstrap-file and a custom test suite. If you are not familiar with the structure of these files, you may let NetBeans generate a skeleton for you.
The bootstrap option is required for projects that use a custom class loader, for example by implementing the __autoload() magic function. You also use the bootstrap option if you need to include a file in advance, such as a file that defines global constants used by several classes of your project.
The XML configuration file allows you to define any of the options you could use in a command line call and more. You will find a complete introduction in the PHPUnit manual. You may find the ability to define php.ini settings and global vars for your test cases quite useful. Note that you may alternatively set the bootstrap option in the XML configuration file too.
Last but not least you may choose a custom test suite that will be run whenever you select "run" > "test project" in NetBeans. This is particularly useful when you wish to run just a subset of your tests, or if you want to use some recently added features of PHPUnit like Data Providers. Data Providers are reported to require that you manually add them using PHPUnit_Framework_TestSuite::createTest() and thus are one example for when you will definitely need a custom test suite.
Note that you may of course define as many test suites as you want and run them separately by right-clicking the file in your project explorer and choosing "run".
To prevent confusion, NetBeans notifies you if you use your own Test Suite. The notification can be found in the Test Results,
as well as in the Output window.
Of course, if any of these files is missing, a warning is displayed so you can quickly fix the problem.
Improved code coverage report
You may already know how to test your project - but how do you test your tests? The solution is to generate a code coverage report. It will not only tell you how many percent of your project is already covered by unit tests, it will also tell which lines in your code are executed during testing and (more importantly) which are not.
Note that you may need to raise PHP's memory limit in php.ini before you are able to create your first report. I suggest setting it to at least 500MB. I have seen a PHP process using a peak of 300MB on my machine while generating the report for a small project with only 30 test cases.
If you are already used to code coverage in NetBeans 6.7, you may have noticed that the presented statistics were based on lines of code. It was counting empty lines and comments alike as tested code. We decided to change that behavior, as it showed a tendency to report far too high coverage percentages.
The improved code coverage report in NetBeans now is based on the real number of statements which are actually executed, giving you much more precise results, as the screen above shows.
You will also witness these changes when you display the coverage of a single file. It now corresponds with the results you get by creating a PHPUnit coverage report via it's command line interface.
The screen shows that only executable statements are colored. White-space and comments are ignored. Covered statements are marked green, while statements that haven't been executed during the test are marked red.