Netbeans IDE for PHP supports automatic phpunit testing. With phpunit, netbeans IDE can provide PHPCodeCoverage, similar to the code coverage provided by IDE for python and Ruby. The test output is displayed in the function-rich output window, which is the same as the output window used by the IDE's JUnit, Ruby, and Python test runners.
Netbeans ide also supports combining selenium Portable Testing Framework with phpunit. The selenium plug-in can be obtained from the update center. Install this plug-in to add the selenium server to the registered server of the IDE, and add the selenium test option to the PHP menu.
Directory
- Install phpunit
- Create and run phpunit Test
- Test results and IDE output
- Code coverage
- Use project-specific configuration
- Run the test on selenium framework
To learn this tutorial, you must have the following software and resources.
Software or resources
Required version
Netbeans ide
PHP download package
PHP engine, including Pear
Version 5.
Web Server
Apache HTTP Server 2.2 is recommended.
PHP Debugger
Xdebug 2.0 or later
Phpunit
Version 3.3.0 or later.
Install phpunit
Follow
Use pear to install phpunit as described in phpunit. Phpunit version 3.3.0 or later should be installed. No special settings are required. After phpunit is installed, netbeans can recognize it. Note that you must install pear with the PHP engine. Note that phpunit is usually installed in the local pear directory. This document also provides the specific path:/Usr/lib/PHP/phpunitBut on Windows XAMPP, this path isXampp_home \ PHP \ pear \ phpunit.
To check whether netbeans ide recognizes your phpunit installation, open "Tools"> "options" (on Mac, open "netbeans Preferences") and view the "php" window. The phpunit script path should be displayed in the "phpunit script" field. If no script path is displayed here, browse to find the path.
Create and run phpunit Test
Netbeans IDE can create and run phpunit tests on all PHP classes in a file. To ensure that the test builder runs properly, specify the same name as the first class in the PHP file.
Create and run a phpunit test for a class:
- Create a PHP project named calculator. In this project, createCalculator. php. In this file, type or paste
The calculator class provided in the "skeleton generator" (Framework builder) Chapter of the phpunit document.
<? Phpclass calculator {public function add ($ A, $ B) {return $ A + $ B ;}}?>
Add a comment block that contains the @ assert annotation and some sample input and output. Note that this example contains an incorrect assertion.
<? Phpclass 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 ;}}?>
In the "project" window, right-click the "Calculator. php" node and choose "Tools"> "create phpunit test ". Note that you can create a test for all files in the project in the context menu of the "source file" node.
When you create a test for the first time, a dialog box is displayed asking you about the directory where you want to store the test file. In this example, the "Browse" function is used to createTestsDirectory.
IDE generates a framework test class in a file named calculatortest. php. The file is displayed in the project window and opened in the editor.
Note that@ AssertMark to create a test.
/*** Generated from @ assert (1, 1) = 2. */Public Function testadd4 () {$ this-> assertequals (2, $ this-> Object-> Add (1, 1 ));}
You can test a single file or the entire project. To test the project, right-click the parent node of the project and choose test or press the Alt-F6 key combination. To test the calculator. php file, right-click the node for the file and choose test or press the Ctrl-F6 keys. In this example, there is only one file and there is only one class in the file. Therefore, the two test results are the same. IDE runs the test and displays the result in the test result window.
The more detailed text version of the test result is displayed in the "output" window.
Test results and IDE output
The phpunit test results are displayed in the IDE's "Test Results" and "output" windows. The test results window contains a graphic pane and a short text pane. The "output" window provides more detailed text versions for output. In this section, you will learn more about the test results window and output window.
In the test results window, you can obtain information about failed tests from the following locations:
- Messages attached to tree entries for failed tests in the UI pane
- Text in the right pane, including links to failed test code lines
- Tooltip text displayed when hovering the cursor over a failed test in the UI pane
The test results window contains the following buttons on the left:
- Run the test again.
- The switch button between displaying all test results and displaying only failed test results
- Navigate to the next test result button, and navigate to the previous test result button
The "output" window shows the complete output of the phpunit script. This window is useful when you cannot identify the cause of the error through the information in the test results window. Similar to the test result window, the output window contains links to failed test rows. In addition, the window contains buttons on the left to re-run the test and to open the PHP option window.
Code coverage
Netbeans IDE for PHP provides code coverage and phpunit support. (IDE also provides code coverage for Ruby and Python ). Code coverage checks whether phpunit tests overwrite all methods. In this section, you will learn how to use code coverage through the existing calculator class.
Code coverage:
- open calculator. php and add the Add2 function with the same content as the Add function. The calculator class is as follows:
- right-click the project node. Select "code coverage"> "collect and display code coverage" from the context menu ". By default, the "show editor bar" is also selected.
- now, the editor displays the code coverage editor column at the bottom. Since code coverage has not been tested, the report coverage rate in the editor bar is 0%. (After you click "clear" to clear the test results, the editor Bar also displays 0% coverage .)
- click test to test the files opened, or click all tests to run all tests of the project. The "test result" is displayed ". In addition, the "code coverage" column informs you of the percentage of executable code statements covered by the test. In the editor window, the overwritten code is highlighted in green, and the unoverwritten code is highlighted in red.
- In the editor bar, click "report ...". The code coverage report is opened, showing the results of all tests running on the project. By using the buttons in the report, you can clear the results and run all tests again or deactivate code coverage (click Finish ").
- you can add other classes in the project. Delete the test file and recreate the test file. Next, view the code coverage report again. The new classes are listed. In the following report, another function of the calculator class is not included in the test.
Use project-specific configuration
In IDE, you can select the following custom configuration for the project:
- Boot file
- Xml configuration file
- Test Suite
To configure a specific project, right-click the project node or test file node of the project, and select Properties ". This opens the "properties" dialog box. Select the phpunit category. Open a dialog box where you can select Custom boot, xml configuration, or test suite files. If you are not familiar with the structure of these files, you can use netbeans ide to generate a framework.
Project requirements for using a custom Class LoaderBoot optionsFor example, implement the magic Function_ Autoload (). If you need to pre-include a file (such as a file that defines the global constants used by multiple classes in the project), you also need to use the pilot option.
You can use the xml configuration fileDefine the options used in the command line call.
The phpunit Manual provides a complete introduction. You can also use the xml configuration file to define test cases.PHP. iniSet and global variables. You can also set pilot options in the XML configuration file.
If you set up a custom test suiteThe suite is run every time you select "run"> "test project. This feature is especially useful if you want to run only a portion of the test, or use the recently added (manually added) phpunit feature (such as the data provider.
Note that you can define multiple Test suites as needed and run them separately by right-clicking the file in the project resource manager and selecting "run.
To prevent confusion, netbeans notifies you whether to use a custom test suite. You can find the notification in the "test result" and "output" windows.
Run the test on selenium framework
Selenium is a Web application for testing.ProgramThe portable software of the framework. These tests can be written as HTML tables orProgramming LanguageAnd can be directly run in most web browsers. Selenium can be deployed on Windows, Linux, and Macintosh. For more information, see
Selenium web site.
Netbeans ide has plug-ins that contain selenium servers. With this plug-in, you can run Selenium tests on PHP, web applications, or Maven projects. To run the selenium test on PHP, install the testing selenium package to the PHP engine.
Run selenium test on PHP:
- Open a command prompt and then runPear install Testing_Selenium-betaCommand. You must includePhp_home/PHP/Pear. If the command is successfully run, the following prompt is displayed:Install OK: Channel: // pear.php.net/testing_selenium-0.4.3.
- In IDE, open "Tools"> "ins" and install "Selenium module for PHP ".
- In the project window, right-click the node of the calculator project. Select "new"> "other ". Open the new file wizard. Select "Selenium" and click "Next ".
- When you create a selenium test for the first time, a dialog box is displayed asking you to set the directory of the selenium test file. This directory should be different from the phpunit test file directory. Otherwise, the selenium test runs every time you run the unit test. Functional tests such as selenium typically take longer to run than unit tests, so you may not want to run them each time you run unit tests.
- Accept the default settings on the "name and location" page, and click "finish ". The new selenium test file is opened in the editor and displayed in the project window.
- Now, the "run selenium test" item has been added to the context menu of the project. Click this item, and the selenium test result is displayed in the same "test result" window as the phpunit test.
More exercises
Here are more ideas for you to explore: