corner imagecorner image
FeaturesPluginsDocs & SupportCommunityPartners

Testing with PHPUnit and Selenium

NetBeans IDE for PHP supports PHPUnit automated tests. Thanks to PHPUnit, NetBeans IDE provides code coverage for PHP, similar to the code coverage the IDE provides for Python and Ruby. Test output appears in the same feature-rich output window that the IDE's JUnit, Ruby, and Python test runners use.

Test runner output window showing Ruby test results

NetBeans IDE also supports the Selenium portable test framework, in combination with PHPUnit. A Selenium plug-in is available from the Update Center. Installing this plugin adds a Selenium server to the IDE's registered servers and adds Selenium test options to the PHP menus.

Contents

Content on this page applies to NetBeans IDE 6.7

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE PHP download bundle
A PHP engine, including PEAR Version 5.
A web server Apache HTTP Server 2.2 is recommended.
A PHP debugger XDebug 2.0 or later
PHPUnit Version 3.3.0 or later.

Installing PHPUnit

Use PEAR to install PHPUnit as described in the PHPUnit documentation. Install PHPUnit version 3.3.0 or later. No special setup is needed. After PHPUnit is installed, NetBeans can recognize it. Note that you need to have PEAR installed with your PHP engine. Also note that PHPUnit documentation says PHPUnit is usually installed to the local PEAR directory. They give a path of /usr/lib/php/PHPUnit, but on XAMPP for Windows it is XAMPP_HOME\php\PEAR\PHPUnit.

To check that NetBeans IDE recognizes your PHPUnit installation, open Tools > Options (On Mac, open NetBeans Prefererences) and look at the PHP window. The path to your PHPUnit script should appear in the PHPUnit Script field. If the script is not there, browse for it.

Options page showing path to PHPUnit script

Creating and Running PHPUnit Tests

NetBeans IDE can create and run PHPUnit tests on all PHP classes in a file. To be sure that the test generator will work, give the PHP file the same name as the first class in the file.

To create and run PHPUnit tests for a class:

  1. Create a PHP project named Calculator. In this project, create a file named calculator.php. In this file, type or paste the Calculator class from the Skeleton Generator chapter of the PHPUnit documentation.
    <?php
    class Calculator
    {
        public function add($a, $b)
        {
            return $a + $b;
        }
    }
    ?>
  2. Add a comment block with the @assert annotation and some sample input and output. Note that one incorrect assertion is included in this example.
    <?php
    class Calculator
    {
        /**
         * @assert (0, 0) == 0
         * @assert (0, 1) == 1
         * @assert (1, 0) == 1
         * @assert (1, 1) == 2
         * @assert (1, 2) == 4
         */
        public function add($a, $b)
        {
            return $a + $b;
        }
    }
    ?>
  3. In the Projects window, right-click the Calculator.php node and select Tools > Create PHPUnit Tests. Note that you can create tests for all files in a project in the context menu for the Source Files node.
    Context menu for Source Files node of PHP Project, showing Create PHPUnit Tests option
  4. The first time you create tests, a dialog opens asking you for the directory in which you want to store test files. In this example, the Browse function was used to create a tests directory.
    Test file directory dialog
  5. The IDE generates a skeleton test class in a file called CalculatorTest.php, which appears in your Projects window and opens in the editor.
    Projects window showing new test class

    Note that a test is created for each @assert annotation.

        /**
         * Generated from @assert (1, 1) == 2.
         */
        public function testAdd4()
        {
            $this->assertEquals(
              2,
              $this->object->add(1, 1)
            );
        }
  6. You can test either an individual file or the entire project. To test the project, right-click the project's parent node and select Test, or press Alt-F6. To test the Calculator.php file, right-clict the file's node and select Test, or press Ctrl-F6. This example has only one class in one file, so the results are the same. The IDE runs the tests and displays the results in the Test Results window.
    Test Results window

    A more verbose textual version of the results is displayed in the Output window.

    Output window showing test results

Test Results and IDE Output

The results of PHPUnit tests are displayed in two of the IDE's windows, Test Results and Output. The Test Results window has a graphic pane and a short text pane. The Output window gives a more verbose textual version of the output. In this section, you explore the Test Results and Output windows in detail.

In the Test Results window, you get information about failed tests from these locations:

  • Messages in the UI pane attached to the tree entry for the failed test
  • Text in the right-side pane, including links to the lines of test code that failed
  • Tooltip text that appears when you hover the cursor over a failed test in the UI pane
Test results window showing tooltip

The Test Results window includes the following buttons on the left side:

  • Rerun the test
  • Toggle between showing results for all tests or only for failed tests
  • Navigate between showing the next test result or the previous test result

The Output window shows the full output of the PHPUnit script. It can be useful when you cannot identify the cause of an error with the information in the Test Results window. Like Test Results, the Output window includes links to the test class line that failed. It also includes buttons on the left side for rerunning the test and for opening the PHP Options window. Button in Output window linking to Options

Output window showing full PHPUnit test results

Code Coverage

NetBeans IDE for PHP offers code coverage along with PHPUnit support. (The IDE also offers code coverage for Ruby and Python). Code coverage checks whether all your methods are covered by PHPUnit tests. In this section, you see how code coverage works with your existing Calculator class.

To use code coverage:

  1. Open Calculator.php and add a duplicate add function, called add2. The Calculator class now looks like the following:
    <?php
    class Calculator {
        /**
         * @assert (0, 0) == 0
         * @assert (0, 1) == 1
         * @assert (1, 0) == 1
         * @assert (1, 1) == 2
         * @assert (1, 2) == 4
         */
        public function add($a, $b) {
            return $a + $b;
        }
    
        public function add2($a, $b) {
            return $a + $b;
        }
    
    }    
    ?>
    
  2. Right-click the project node. From the context menu, select Code Coverage > Collect and Display Code Coverage. By default, Show Editor Bar is also selected.
    Turning on code coverage from the project node context menu
  3. The editor now has a code coverage editor bar across the bottom. Because code coverage has not been tested, the editor bar reports 0% coverage. (It also displays this after you click Clear to clear test results.)
    Editor bar for code coverage before tests are run
  4. Click Test to test the open file or All Tests to run all tests for the project. The Test Results display. In addition, the Code Coverage bar tells you what percentage of your code is covered by tests. In the editor window, covered code is highlighted in green and uncovered code is highlighted in red.
    Editor bar for code coverage after tests are run
  5. In the Editor Bar, click on Report... The Code Coverage report opens, showing the results of all tests run on your project. Buttons in the report let you clear the results, run all the tests again, or deactivate code coverage (click Done).
    code coverage report
  6. You can add another class to your project, delete and recreate the test files and look at the code coverage report again. Your new class is listed. In the following report, the Calculator class again has a function that is not included in the tests.
    code coverage report showing second class

Running Tests on the Selenium Framework

Selenium is a portable software testing framework for web applications. The tests can be written as HTML tables or coded in a number of popular programming languages and can be run directly in most modern web browsers. Selenium can be deployed on Windows, Linux, and Macintosh. For more details see the Selenium web site.

NetBeans IDE has a plugin that includes a Selenium server. With this plugin, you can run Selenium tests on PHP, Web Application, or Maven projects. To run Selenium tests on PHP, you need to install the Testing Selenium package to your PHP engine.

To run Selenium tests on PHP:

  1. Open a command prompt and run the command pear install Testing_Selenium-beta. You need PHP_HOME/php/PEAR on your Path. If the command is successful, the prompt will display install ok: channel://pear.php.net/Testing_Selenium-0.4.3.
  2. In the IDE, open Tools > Plugins and install the Selenium Module for PHP.
  3. In the Projects window, right-click the project node for your Calculator project. Select New > Other. The New File wizard opens. Select Selenium and click Next.
    New File wizard with Selenium file chosen
  4. The first time you create a Selenium test, a dialog opens asking you to set a directory for Selenium test files. This should be a separate directory from PHPUnit test files. Otherwise, the Selenium tests run every time you run unit tests. Running functional tests like Selenium usually takes more time than running unit tests, therefore you will probably not want to run these tests every time you run unit tests.
  5. Accept the defaults in the Name and Location page and click Finish. The new Selenium test file opens in the editor and appears in the Projects window.
    Projects window showing new Selenium test
  6. The Run Selenium Tests item is now added to the project's context menu. Click this item, and the Selenium test results display in the Test Results window, the same as PHPUnit tests.

More Exercises

Here are a few more ideas for you to explore:


To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE PHP development features, join the mailing list. This list is mirrored on the NetBeans IDE forums.

See Also

For more information about testing PHP in NetBeans IDE, see the following resources:

Back to the PHP Learning Trail