Recent improvements in PHPUnit-support

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 PHPUnit

First 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.

PHPUnit Properties

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.

PHPUnit Project Properties

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,

Custom Test Suite in Test Results

as well as in the Output window.

Custom Test Suite in Output

Of course, if any of these files is missing, a warning is displayed so you can quickly fix the problem.

Missing Files Warning

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.

Improved Code Coverage

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.

Displaying Coverage of Source File

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.

That's all for today, as always, please test it and report all the issues or enhancements you find in NetBeans IssueZilla (component php, subcomponent phpunit).

Comments:

Impressed with the style of your screen shots.

Did you use any programs?
Did you use an image program like Photoshop?

Thought it might be helpful to other users like myself especially considering the latest post on Community Docs.

Posted by Glenn Welker on July 31, 2009 at 02:17 PM CEST #

I used Bug-Shooting (bugshooting.com) to take screen-shots and FireShot (screenshot-program.com/fireshot) to add annotations.

Bug-Shooting is free software (Windows only).
FireShot is a free Firefox-Extension and great for browser-screens. There is a pro-version that allows you to open and annotate existing screens as well.

As a side-note: I've seen DataProviders now work in the current version of PHPUnit. Some people reported bugs for previous versions though.

PHPUnit 3.4 is well on it's way introducing a whole bunch of new annotations and features. Judging from the changes that I already know of: it will be worth looking at as soon as it's done.

Posted by Tom on August 04, 2009 at 02:10 AM CEST #

One side effect of new features is, now we should also add to global
include path directory with unit tests itself. Otherwise IDE cannot
find them (for example shortcut "Go to test file" does not work). It
is not mentioned in documentation. In older versions it has working
without it

Posted by Pet on August 06, 2009 at 05:25 AM CEST #

To Pet:
Sorry, this is _NOT_ true. The only change was in code coverage area, other things are new features.
(The 1st image is only a hint where to add PHPUnit classes to have them in code completion.)

Tomas

Posted by Tomas Mysik on August 06, 2009 at 08:18 AM CEST #

It is really great to see good PHPUnit support arriving in Netbeans.

But there is one cruical feature I am missing. In my team, we are working on a pretty complex application that is too difficult to install and maintain on each developer's machine. So we are using a development server - on which the app is executed - and export the project directory as a local drive to the dev machines for editing. So far, so good - we can use code intelligence and of course xdebug support as sessions are initiated through HTTP.

But we cannot use PHPUnit, because it makes the assumption that the app is executed locally. I wrote a shell script that I entered as the "phpunit" in the configuration, and I can see from the output panel that my shell script is executed. It also correctly outputs the results of the unit test and takes care of putting the XML file with PHPUnit's results into the directory that was specified by Netbeans' call to my script.

My hope was that this ugly workaround would make it possible to have PHPUnit IDE support and remote execution. But not so: Netbeans tells me that my phpunit is out of date, for no good reason, and refuses to parse the XML. In fact, the output window says:

"PHPUnit 3.3.17 by Sebastian Bergmann.

.

Time: 0 seconds

OK (1 test, 0 assertions)

==================================================================================================================================================
PHPUnit (version ?.?.?) is too old, upgrade it if possible (the minimum version is 3.3.0).
Creating and running individual test is still possible.
=================================================================================================================================================="

What I am trying to get at: please improve the config dialog to let the user specify where Netbeans should look for PHPUnit's XML file and then just parse this file for the IDE if it exists after the command executed. This is hopefully a small change to your code base, and will drastically improve the usefulnes of this cool feature!

Thanks,

Batrick

Posted by Batrick on August 16, 2009 at 05:29 PM CEST #

Hiya,
Thanks for the fantastic post.

I am using netbeans 6.7.1 and done all the things you mentioned but I cannot see PHPUnit menu when I right click on test-folder. I am trying to use PHPunit with Zend Framework and I need to specify the xml configuration file.

Thanks
Bill

Posted by Bill on September 05, 2009 at 07:41 AM CEST #

@Batrick, It works on mine by the time I use include global path, shown on the first instruction.

Posted by Wendy Novianto on October 08, 2009 at 05:40 AM CEST #

I'm a noob at both Netbeans and PHPUnit, so please be gentle. ;-)

Two questions:

1. You specify that the include path should be set to point to the PHPUnit subfolder of the PEAR folder. But shouldn't it be set to the PEAR folder itself? After all, the PHPUnit classes have require_once calls of the form:

require_once require_once 'PHPUnit/Util/Filter.php';

that already have the PHPUnit folder specified.

See what I mean?

2. When I right-click > Properties on the folder containing my tests, I do not get the dialog displayed. Rather, I get a very thin dialog that lists some folder properties and some sorting modes. Perhaps my NB (v6.7.1) is misconfigured in some way?

Any ideas greatly appreciated. Many thanks in advance.

Posted by David Weinraub on October 08, 2009 at 09:54 PM CEST #

I have the same version has Bill and the same problem, is there a work around for this?

Posted by Andre Ferreira on December 06, 2009 at 08:14 AM CET #

When I right-click > Properties on the folder containing my tests, I do not get the dialog displayed. Rather, I get a very thin dialog that lists some folder properties and some sorting modes. Perhaps my NB (v6.7.1) is misconfigured in some way?

Any ideas greatly appreciated. Many thanks in advance.

Posted by ED Hardy Suit on February 07, 2010 at 12:29 AM CET #

I used this post to get my unit tests working in NB 6.8 on Windows even though the interface is not exactly the same as your screen shots.
This morning I installed some recommended IDE updates (NetBeans IDE 6.8 Build 200912041610) and now all I get in the Test Results window when I run my tests is "No tests executed. Perhaps an error occured, verify in Output window."
The Output window doesn't have any messages.
Any help would be greatly appreciated.

Posted by Jannie Theunissen on February 10, 2010 at 08:26 PM CET #

Oops! Things working perfect (after I killed a line of buggy code in my project).

Please ignore my previous post.

Absolute brilliant IDE and PHP support!

Posted by Jannie Theunissen on February 10, 2010 at 09:54 PM CET #

Same error message as Jannie:
"No tests executed. Perhaps an error occured, verify in Output window."
Does someone knows what is the problem?

Posted by ianagui on February 24, 2010 at 07:59 PM CET #

I'm getting the same issue as ianagui and Jannie. I've entered the global path to PEAR's PHPUnit and followed the blog post here.

Still, when I try to run my tests, I get "Perhaps an error occured, verify in Output window." What's funny, is that this error message IS displaying in the Output window.

I can get around this by turning off the Code Coverage: Right-click on the project->Code Coverage-> and un-check the "Collect and Display Code Coverage" checkbox.

So I can still run tests, but no code coverage. Any ideas?

//----------------------------------------
Product Version: NetBeans IDE 6.8 (Build 200912041610)
Java: 1.6.0_17; Java HotSpot(TM) Client VM 14.3-b01
System: Linux version 2.6.31-19-generic running on i386; UTF-8; en_US (nb)

Posted by Josh on February 26, 2010 at 06:08 PM CET #

I would never have worked this out without this blog post, so thanks very much!

Posted by Phil on March 31, 2010 at 10:02 AM CEST #

I followed your post closely with Xampp 1.7.3(bundled with php 5.3.1); PHPUnit 3.4.13; Xdebug 2.1.04; NetBeans 6.8 on Windows XP.

Both Xdebug and PHPUnit are working fine. I see test results for a single unit test or all tests but I never get code coverage in the report. The message is always "No data -- have you run your code yet?"

What could I have missed in the setup?
Any help will be appreciated.

Posted by peter on June 16, 2010 at 01:24 AM CEST #

Batrick gave me an idea. I also needed to run tests on remote machine.
Here is some prototype of the script that one may use for this (that's example from Windows machine - put the content in a remotephpunit.bat file and point PHPUnit script from NetBeans to this file):

@echo phpunit --log-junit ~/nb-phpunit-log.xml run YoutTestFileFullPath.php > c:\\cmd
@"C:\\Program Files (x86)\\PuTTY\\plink" -ssh -l YOUR_UNIX_NAME -pw YOUR_UNIX_PASSWORD UNIX_MACHINE_IP -m c:\\cmd
@"C:\\Program Files (x86)\\PuTTY\\pscp" -q -l YOUR_UNIX_NAME -pw YOUR_UNIX_PASSWORD UNIX_MACHINE_IP:~/nb-phpunit-log.xml C:\\Users\\YOUR_WINDOWS_NAME\\AppData\\Local\\Temp\\nb-phpunit-log.xml

Obviously it's not nice to have those things hardcoded in the script, but it's the way to go.
Just need to create a script (say php command line script) that parses input arguments to get the desired location of xml file and the test file to run to make it generic. And optionally handle security in putty to avoid clear text passwords.
Please note, that for test file you would need to specify the full path, so some mapping (from local machine to remote machine) would be required.

Posted by Sergey on June 27, 2010 at 02:50 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

This blogs is written by NetBeans developers who contribute to the PHP support mainly.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today