Testing with PHPUnit and SeleniumNetBeans 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 ![]()
To follow this tutorial, you need the following software and resources.
Installing PHPUnitUse 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. ![]() Creating and Running PHPUnit TestsNetBeans 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:
Using Test GroupsStarting 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:
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 OutputThe 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:
![]() The Test Results window includes the following buttons on the left side:
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. ![]() Code CoverageNetBeans 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:
Using Project-Specific ConfigurationsIn the IDE, you can select the following custom configurations for your project:
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. ![]() 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 FrameworkSelenium 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:
More ExercisesHere 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 AlsoFor more information about testing PHP in NetBeans IDE, see the following resources: |
Documentation |