X

An Oracle blog about NetBeans for PHP

PHPUnit support added

Guest Author

Today I would like to inform you that we have added PHPUnit support to NetBeans. It is not finished yet (UI for test results is still missing) but I would like to show you how one can test his/her PHP sources in NetBeans today. If you are new to PHPUnit, be sure that you visit PHPUnit web site first to get all the required information.

First, be sure that PHPUnit is installed on your computer and PHPUnit script is known for NetBeans; to verify that, just open IDE Options (Tools > Options) and check whether the script has been found for you. If the script is not found, please let us know, preferably using our IssueZilla (component php, subcomponent project) so we can try to fix your case. The IDE Options should look like this:

NetBeans Options (PHP part)

Now it's time to create our first PHPUnit test - to do so, open any existing PHP project (or create a new one) and create a new PHP class (be sure that the name of the file is in the form of <the_name_of_PHP_class_defined_in_the_file>.php - for NetBeans, it's enough to invoke New > PHP Class...). Add some methods and right click the file in Projects view, select Tools > Create PHPUnit test... Please note that this action can be invoked for folders as well.

Create PHPUnit test

If it's the first time, IDE has to ask for a folder for tests:

Select a folder for PHPUnit tests

 Once this folder is set, it can be found in Project Properties dialog as well as in Projects view, of course.

PHP Project properties

After the tests are created, one can easily run them using Test action on project's node:

Run Test action on project's node

For now, only Output window is opened and the test results are displayed; but stay tuned, we plan to add UI for these results, similarly as for Ruby or Python support.

PHP Test results

Of course, one is able to run or even debug only one particular test (labels are not final yet, Run/Debug are used for now, maybe we should change them to Run test/Debug test - what do you think?):

Run/debug PHP test

The last thing for today is that it is very easy to navigate between source (tested) class and test class - just right click anywhere in the editor and invoke Navigate action:

Navigate to tested class

We hope that you will find NetBeans PHPUnit support useful!

PS: Currently we are discussing the relation between Test sources and Remote Web configuration of a PHP project - should tests be uploaded to a remote (S)FTP server? If yes, then where? What about if Tests are underneath Sources? And what if they are not? As you can see, there are quite many questions to solve. Any feedback is welcome, of course.


Join the discussion

Comments ( 48 )
  • coccyx Monday, January 19, 2009

    Is it posible to run unit tests defined in PHPUnit_Framework_TestSuite?


  • Tomas Mysik Monday, January 19, 2009

    Can you please provide more information or an example? I'm not sure what you mean. Thanks.


  • coccyx Monday, January 19, 2009

    Ok, I will try to describe it more detailed (sorry my english).

    I am doing some integration tests, on my DAO classes (CRUD,...). But to test the database layer I need to drop all tables and create tables and fill them with basic data and this must be done before the phunit testing. And after database setup, I can run unit tests, but i need run them in special order and this is why the unit tests are defined in PHPUnit_Framework_TestSuite.

    Description of class PHPUnit_Framework_TestSuite says:

    "A TestSuite is a composite of Tests. It runs a collection of test cases."

    PS: "Podla mena vyzeras ze si slovak/cech, tak dufam ze som si tu nelamal jazyk zbytocne" :)


  • hantsy Monday, January 19, 2009

    Dose it support runing single file test and single method test ?

    This gui is not reused from the JUnit gui? It is so different.

    By the way , I want to kown it support SimpleTest(http://www.simpletest.org/)?


  • hantsy Monday, January 19, 2009

    I think other features like Code Coverage/Unit test report(which provided in Ruby/PYthon support now) are expected by other PHP guys.


  • Jussi Monday, January 19, 2009

    Great job!

    (More features and even better job.)

    In my opinion:

    If the tests are under sources then they should be uploaded as in that case they already have a place for them. If not, should there be a checkbox and an input box for choosing if and where to upload test classes?


  • ljarochowski Tuesday, January 20, 2009

    What should i do, to enable this, and other features (return types, implement all abstract methods and other)? I have NB 6.5 with PHP 1.2.2


  • Tomas Mysik Tuesday, January 20, 2009

    To coccyx:

    I need to investigate more your use case - in such case I would say that such suite should be run using 'ordinary' PHP interpreter and not PHPUnit - I will try to look at it (of course, any suggestions are welcome).

    Tomas

    PS: Tak tak, jsem Cech ;)


  • Jozef Tuesday, January 20, 2009

    @ljarochowski: you must download the development nightly build zip from http://bits.netbeans.org/download/trunk/nightly/latest/


  • Tomas Mysik Tuesday, January 20, 2009

    To hantsy:

    > Does it support runing single file test and single method test ?

    Single file - yes (written in the blog already)

    Single method - not yet

    > This gui is not reused from the JUnit gui? It is so different.

    In fact, no GUI right now; the one from Ruby/Python will be reused.

    > By the way , I want to kown it support SimpleTest?

    No plans for this support right now.

    Tomas


  • Tomas Mysik Tuesday, January 20, 2009

    To hantsy:

    > I think other features like Code Coverage/Unit test report(which provided in Ruby/PYthon support now) are expected by other PHP guys.

    These are in the current TODO list.


  • Tomas Mysik Tuesday, January 20, 2009

    To Jussi:

    > Great job!

    Thanks a lot!

    > In my opinion:

    > If the tests are under sources then they should be uploaded as in that case they already have a place for them. If not, should there be a checkbox and an input box for choosing if and where to upload test classes?

    The "problem" here is that if Tests are underneath Sources, they are _not_ visible in Projects view - wouldn't it be confusing if they will be uploaded together with Sources? Maybe not, I don't know.

    Specify another upload directory for Tests is one possibility - but seems to me a bit complicated, and again, hard to say whether people will be happy with it.

    Thanks a lot for your opinion,

    Tomas


  • Tomas Mysik Tuesday, January 20, 2009

    To ljarochowski and Jozef:

    Or one can use NB development builds from:

    http://deadlock.netbeans.org/hudson/job/PHP-build/lastSuccessfulBuild/artifact/nbbuild/

    Tomas


  • coccyx Tuesday, January 20, 2009

    Ok, I uploaded source file to see how looks inside.

    [IMG]http://i39.tinypic.com/4t6ag3.png[/IMG]

    I run unit tests from command line: phpunit AllTests.php

    And you can see some results here:

    [IMG]http://i42.tinypic.com/o597vk.png[/IMG]


  • Tomas Mysik Tuesday, January 20, 2009

    To coccyx:

    Could you please create and issue for it in our IssueZilla? Describe your use case there, attach screenshots; let's discuss it there, to keep history etc.

    (How do you run your suite? Using php or phpunit script?)

    Thanks a lot,

    Tomas


  • coccyx Tuesday, January 20, 2009

    I am running my tests with 'phpunit AllTests.php' and i can also use other parameters for code coverage, pmd, ...

    You can also look at Zend Framework to see how PHPUnit_Framework_TestSuite is used.

    I never worked with IssueZilla but I give it a try :)


  • Rahul Tuesday, January 20, 2009

    Hi,

    Is there a plugin which I can install separately instead of downloading & installing the complete package again over my previous 6.5 installation?

    btw Good Job on this. Netbeans FTW!!


  • Tomas Mysik Wednesday, January 21, 2009

    To Rahul:

    It's not possible because PHP support depends on other IDE features that are only in trunk, not in NB 6.5 FCS - that's why you have to download the PHP build. If the ZIP file is too big (cca. 45MB) for you, you can use NetBeans installer, it can be found here:

    http://bits.netbeans.org/download/trunk/nightly/latest/

    (only about 23 MB)

    Tomas


  • Rahul Wednesday, January 21, 2009

    I understand. I'll give the NB installer a shot.

    Thanks for your time.


  • Rahul Wednesday, January 21, 2009

    oh and btw I use Java so I've installed the complete package (220 MB) thats my problem ;)

    I'll give my php team the "nice & small" 23 Mb build.


  • Olksy Wednesday, January 21, 2009

    Thanks for your efforts!

    BTW, it would be nice to highlight file:line as links in test run output if test produces fail/error.

    The same way as "Running Tutorial Examples in NetBeans IDE" states: "If the build output concludes with the statement BUILD FAILED... Errors are reported in the Output window as hyperlinked text. You double-click such a hyperlink to navigate to the source of an error."


  • Tomas Mysik Wednesday, January 21, 2009

    To Olksy:

    This should be possible from test results GUI. But I agree, it would be useful in Output window as well. We will try to look at it.

    Thanks,

    Tomas


  • Tomas Prochazka Wednesday, January 21, 2009

    This is great news. But problem is, that I use Zend Framework like class organization. For example in folder ap/ui I have file Login.php which contain class with name ap_ui_Login. And Netbeans write me only "Test were not generated... ...DO they contain PHP classes? Are the names of the classes same as names of the files?" No :-( It's problem create test with the same file name and class name inside it?


  • Tomas Mysik Thursday, January 22, 2009

    To Tomas Prochazka:

    Well, the problem here is that phpunit is run only with a file name as a parameter because PHP project doesn't know anything about the content of a file. And right now it's not possible to get these information. But please, report a bug and I will try to fix it (together with editor guys).

    Thanks a lot,

    Tomas


  • Valentin Jacquemin Friday, January 23, 2009

    Thanks a lot for this great addition!

    (I think Run/Debug Test would be more meaningful)


  • Mon Z. Tuesday, January 27, 2009

    Hi,

    I upgraded to the nightly build from 6.5 a couple of days ago to use this PHPUnit scaffolding and the inline var type docblock. They work very nicely. However, I can't seem to use the rename (Ctrl-R) feature that I used heavily in 6.5. Is there a way to enable it in the nightlies?


  • Mon Z. Tuesday, January 27, 2009

    My bad on that one. Ctrl-R is not disabled, it's just limited to local vars now and doesn't work on class members and method names. I don't think I can live without this feature, I hope it can be addressed in the near future


  • Jacob Christophersen Tuesday, January 27, 2009

    Great new feature.

    Netbeans didn't find the scripts for me, running Vista and WAMP. (issue submitted)


  • Tomas Mysik Friday, January 30, 2009

    To Mon Z.:

    This behavior is intentional (we are just trying to do it safely) but the final behavior is not decided yet. Please, file and issue (against PHP Editor) so it can be discussed directly in NetBeans IssueZilla. Thanks.

    To Jacob Christophersen:

    Thanks a lot for filing an issue, I will look at it soon.


  • VagabondVince Tuesday, June 23, 2009

    How do you setup xdebug and netbeans to debug your phpunit tests? I'm not having any luck, netbeans keeps waiting for a connection (netbeans-xdebug).

    And thanks for adding this to netbeans, I've been wanting it for a while.


  • Thomas Meyer Thursday, June 25, 2009

    I filed an issue, that PHPUnit support doesn't work for projects that use either the __autoload() feature of PHP 5 or a centralized class-loader.

    In both cases you will not be able to create new or even run existing Unit-Tests and will not be able to show code-coverage.

    This is due to the fact, that PHPUnit's "bootstrap" command line argument is not supported. For now you can work around this problem by writing your own batch file and adding the argument there.


  • Melor Wednesday, July 1, 2009

    Looks like there is a problem with filenames with '_'.

    Example:

    C_MyClass.php

    --------------

    class C_MyClass(){...}

    Then I am getting output:

    PHPUnit 3.3.17 by Sebastian Bergmann.

    File "C\\MyClass.php" does not exist.


  • web development in php Friday, July 17, 2009

    great keep the good work on


  • Alexander Wednesday, July 22, 2009

    i just cant make it work, it say that it didnt find any PHPUnit script

    windows xp sp3

    WampServer2.0i (Apache 2.2.11, MySQL 5.1.36, PHP 5.3.0)

    NetBeans 6.7

    the path for phpunit im trying to insert on php options at options = C:\\wamp\\bin\\php\\php5.3.0\\PHPUnit\\Framework.php

    Any help is welcome =)

    Thanks


  • Alexander Wednesday, July 22, 2009

    ok i just found this http://www.netbeans.org/kb/docs/php/phpunit.html#installing-phpunit

    and everything went better than expected, it works! thanks anyway :D


  • mirc Saturday, August 22, 2009

    Thank you by Admin cok sagolun ya harıkasın


  • Niall dawson Thursday, September 17, 2009

    single tests can be run by changing the tools - options - php - phpunit script setting from

    /usr/bin/phpunit

    TO

    /usr/bin/phpunit --filter nameoftest


  • Andreas Wenk Friday, September 18, 2009

    Hi there,

    thanks for PHP in Netbeans! I love it.

    I have a problem with writing tests for a class extending another class. NB will not create it with the message:

    "Test were not generated for the following files:

    CLASSNAME.php

    Review the log in Output window"

    There:

    PHP Fatal error: Class 'CLASSNAME_OF_EXTENDED_CLASS' not found in /PATH/CLASSNAME.php on line 14

    PHPUnit 3.4.0 by Sebastian Bergmann.

    PHP Stack trace:

    Fatal error: Class 'CLASSNAME_OF_EXTENDED_CLASS' not found in /SAME_PATH_AS_ABOVE/CLASSNAME.php on line 14

    If I do creating a test from a class without extending another class there's no problem.

    Any ideas what's going wrong? A PHP include_path issue?

    Thanx for any replys

    Andy (@awenkhh)


  • Rodolphe Tuesday, September 29, 2009

    Support for SimpleTest would be awesome (since it's the test lib I'm using now, of course!).

    It seems that both test libs (PHPUnit and SimpleTest) are widely used in the PHP world.

    Anyway, thanks for NetBeans, it's an awesome tool I'm going to use more and more...


  • Karl Purkhardt Tuesday, October 6, 2009

    Hi. I have imported an existing project and I just can't seem to get my tests to run in batch. I can run them individually, but when I invoke Test on the main project no tests are actually executed. I setup the tests myself and they are probably not setup in the way that NetBeans expects. What does netbeans expects from the test setup?

    My setup is as follows. I have a folder structure (like this www.grabup.com/uploads/4d5760389c16.png)

    where each test contains a require_once that points to the Bootstrap.php file (www.grabup.com/uploads/9da18016f77a.png), which sets up the testing environment. An example test case is: www.grabup.com/uploads/dd912dc40ca1.png

    Thanks,

    Karl


  • collect bag Friday, November 13, 2009

    I filed an issue, that PHPUnit support doesn't work for projects that use either the __autoload() feature of PHP 5 or a centralized class-loader.


  • Ralph Meier Friday, January 1, 2010

    I have installed PHPUnit 3.4.5 for Netbeans 6.8 and my unit tests are running good.

    If i activate code coverage and run the same test/tests i get no test results

    Test Results: no tests are executed

    Code Coverage: 0.0%

    It seems that an activated code coverage plugin doesn't find my tests.

    Does anyone know what the problem could be?


  • ED Hardy Suit Saturday, February 6, 2010

    I filed an issue, that PHPUnit support doesn't work for projects that use either the __autoload() feature of PHP 5 or a centralized class-loader.


  • Ikeda Monday, March 29, 2010

    > VagabondVince

    Write xdebug setting to php.ini for cli-php.

    ex).

    [xdebug]

    zend_extension=/usr/lib/php5/20090626/xdebug.so

    xdebug.remote_enable=on

    xdebug.remote_handler=dbgp

    xdebug.remote_host=localhost

    xdebug.remote_port=9000

    I had the same problem in debian squeeze.

    Xdebug work well in browse debugging, but does'nt work in pupunit test.

    php.ini for cli and that for httpd is another.

    To use xdebug for phpunit, write setting to /etc/php5/cli/php.ini


  • plasticsticker Tuesday, May 25, 2010

    Hello NetBeans,

    I have installed PHPUnit 3.4.13 for Netbeans 6.8 and my first unit test is causing the following error to be reported by Netbeans: "Selected PHPUnit (version ?.?.?) is too old ..." Does anyone know how to correct this?


  • Ahmad Friday, August 13, 2010

    Hello plasticsticker,

    you haven't configured netbeans correctly to work with phpunit.

    go to tools->options->php->unit testing and give the path to "phpunit.bat" (like C:\\xampp\\php\\phpunit.bat )


  • guest Friday, September 30, 2011

    please send some sample login script for the phpunit testing


  • guest Monday, March 18, 2013

    Help me, please! I have error afte click "Tools->Create PHPUnit tests":

    '".\php.exe"' is not recognized as an internal or external command, operation program or batch file.

    But I correctly identified the options: PHP->General->PHP 5 Interpreter, PHP->PHPUnit script. And defined system variable PATH = C:\php


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.