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. Test output appears in the same feature-rich output window that the IDE's JUnit and Python test runners use.

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 7.1

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.
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. Open the Unit Testing tab. 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/⌘-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

Using Test Groups

Starting in NetBeans IDE version 7.1, you can use PHPUnit test groups. You can select which groups of tests to execute when the test suite is run. For example, you could have some tests you want to run only in a production environment and other tests that you want to run in both production and development environments. You would place the former tests in a production group and the latter tests in both production and development groups. When you run the test suite in your development environment, you select only the development test group to execute.

You must enable test groups for a PHP project before you use test groups on any file in that project.

To mark a test as part of a test group, annotate the test method with @group [group name].

To create and run test groups:

  1. In the Projects window, right-click the Calculator node and select Properties. The Project Properties open.
  2. In the Project Properties, select the PhpUnit category. Select Ask for Test Groups Before Running Tests. Click OK.
    Project properties, PhpUnit category, showing test groups enabled
  3. Open CalculatorTest.php in the editor.
  4. For the methods testAdd, testAdd3 and testAdd5, add the annotation @group production.
    Code showing test group annotation
  5. For the methods testAdd2 and testAdd4, add the annotations @group production and @group development. Code showing test group annotations
  6. Right-click the Calculator.php node and select Test. A dialog opens, asking you which test groups to run. Select "development" and click OK. The IDE only runs the tests that are annotated with @group development.
    Select test group dialog

For more information about PhpUnit test groups in NetBeans IDE, see the NetBeans IDE for PHP blog post Using PHP Unit Test Groups.

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 rerun button
  • Show failed tests Show failed tests button
  • Show passed tests Show passed tests button
  • Show tests that passed but with errors Show tests with errors button
  • Navigate between showing the next test result next test button or the previous test result previous test button

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 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 executable code statements is covered by tests. In the editor window, covered code is highlighted in green and uncovered code is highlighted in red.

    Warning: If you re-generate the test files AFTER adding the add2 function, the PHPUnit tests will not run. This is because PHPUnit creates two conflicting testAdd2 functions. Do not differentiate functions by appending numbers at the end if you plan to use PHPUnit on more than one such function. See the the PHPUnit documentation.

    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

Using Project-Specific Configurations

In the IDE, you can select the following custom configurations for your project:

  • A bootstrap file
  • An XML configuration file
  • A test suite

To set a project-specific configuration, right-click the project's node or the project's Test Files node and select Properties. This opens the Properties dialog. Then select the PHPUnit category. A dialog opens in which you can select a custom bootstrap, XML configuration, or test suite file. If you are not familiar with the structure of these files, you can use NetBeans IDE to generate a skeleton for you.

Selecting PHPUnit custom options in a project's properties dialog

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 multiple classes in your project.

The XML configuration file allows you to define options that you use in a command line call. There is a complete introduction in the PHPUnit manual. You can also use the XML configuration file to define php.ini settings and global vars for your test cases. You can set the bootstrap option in the XML configuration file too.

If you set a custom test suite, you run that suite whenever you select Run >Test Project. This is particularly useful when you wish to run only a subset of your tests, or if you want to use recently added features of PHPUnit that you have to add manually, such as Data Providers.

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 are using a custom Test Suite. The notification can be found in the Test Results and in the Output window.

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:

  • Add a second class to Calculator.php, such as a Calculator2 class that multiplies $a and $b. Delete and regenerate the tests.
  • If you try the multi-part Creating a CRUD Application tutorial, create a Selenium test for the final project.

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

get support for the NetBeans

Support


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo