Testing with PHPUnit and Selenium

Source: Internet
Author: User
Tags pear php download netbeans

The NetBeans IDE for PHP supports PHPUnit automated testing. The Phpunit,netbeans IDE provides code coverage for PHP, similar to the code coverage provided by the IDE for Python. The test output is displayed in the feature-rich Output window, which is the same as the output window used by the IDE's JUnit and Python test Runner.

NetBeans IDE also supports the use of the Selenium Portable test framework in conjunction with PHPUnit. The Selenium plugin can be obtained from the Update Center. Installing this plugin adds the Selenium server to the IDE's registered server and adds the Selenium test option to the PHP menu.

Directory

    • Installing PHPUnit
    • Create and run the PHPUnit test
    • Working with test groups
    • Test results and IDE output
    • Code Coverage
    • Using Project-specific configurations
    • Running tests on the Selenium framework

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

Software or Resources Required Version
NetBeans IDE PHP Download Package
PHP engine, including PEAR Version 5.
Web Server It is recommended to use Apache HTTP Server 2.2.
PHPUnit Version 3.4.0 or later.
Framework Builder for PHPUnit As a version of PHPUnit.
Installing PHPUnit

Use PEAR to install PHPUnit (as described in the PHPUnit documentation) and PHPUnit framework Builder (as described in the Framework Builder documentation). You should install PHPUnit version 3.4.0 or later. No special settings are required. After installing PHPUnit, NetBeans can identify it. Please note that you will need to install PEAR with the PHP engine. Also note that PHPUnit is typically installed in the local PEAR directory as indicated in the PHPUnit documentation. The document also gives the exact path:/usr/lib/php/phpunit, but on XAMPP for Windows, this path is xampp_home\php\pear\phpunit.

To check if the NetBeans IDE can recognize your PHPUnit installation, open Tools > Options (on your Mac, open NetBeans Preferences (NetBeans preferences)) and check the Look at the "PHP" window. Open the Unit Testing tab. The path to your PHPUnit and framework builder scripts should be displayed. If the script is not displayed, click Search next to the empty field. The IDE will search for the script in your local system. Alternatively, click Browse to explore the find script.

Create and run the PHPUnit test

NetBeans IDE can create and run PHPUnit tests on all PHP classes in a file. To ensure that the Test builder works correctly, specify the same name as the first class in the file for your PHP file.

To create and run a PHPUnit test 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 that is provided in the "Skeleton Generator" (framework Builder) Chapter of the PHPUnit document.
    <?phpclass calculator{public    function Add ($a, $b)    {        return $a + $b;    }}? >
  2. Add a comment block that contains @assert labels and some sample inputs and outputs. Note that this example includes an incorrect assertion.
    <?phpclass calculator{    /**     * @assert (0, 0) = = 0     * @assert (0, 1) = = 1     * @assert (1, 0) = = 1     * @ass ERT (1, 1) = = 2     * @assert (1, 2) = = 4 */public    function Add ($a, $b)    {        return $a + $b;    }}? >

    Note: You can use callout code completion to add @assert annotations. Use the Tab key to navigate between parameters, or click Enter after filling in the parameter values.

  3. In the Projects window, right-click the calculator.php node, and then choose Tools > Create PHPUnit Tests (creating PHPUnit test). Note that you can create tests for all files in a project in the context menu of the source Files node.
  4. The first time you create a test, a dialog box opens asking for the directory where you want to store the test files. In this example, the tests directory was created using the Browse feature.

    Note: You can manually write multiple tests for a project. If you write multiple tests, you can categorize them into subfolders of the test file directory, such as "important" or "quick". You can then run the tests in that folder by right-clicking a subfolder and selecting Run Tests.

  5. The IDE generates the framework test class in a file named Calculatortest.php, which is displayed in the Projects window and opens in the editor.

    Note that a test will be created for each @assert callout.

        /**     * Generated from @assert (1, 1) = = 2.     */Public    function TestAdd4 ()    {        $this->assertequals (          2,          $this->object->add (1, 1)        );    }
  6. You can test a single file, or you can test the entire project. To test the project, right-click the parent node of the project and choose test or press ALT-F6 to combine the keys. To test the calculator.php file, right-click the file's node and select Test, or press CTRL-F6/?-F6 to combine the keys. This example has only one file, and there is only one class in the file, so the two test results are the same. The IDE runs the test and displays the results in the Test Results window.

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

Working with test groups

You can select the test group that you want to execute during the test suite run. For example, there may be some tests that you want to run only in a production environment, while others run concurrently in the production and development environment. You can place the previous test in the production group, while the latter test is placed in both the production and development groups. When you run a test suite in the development environment, you can choose to execute only the development test Group.

Before you can use a test group for all the files in the project, you must first enable the test group for the PHP project.

To mark a test as part of a test group, use @group [group name] to label the test method.

To create and run a test group:

    1. In the Projects window, right-click the Calculator node, and then select Properties. Project Properties opens.
    2. In Project properties, select the PhpUnit category. Select ask for Test Groups before Running Tests (ask the test group before running the test). Click OK.
    3. Open calculatortest.phpin the editor.
    4. Add labels @group productionfor the testadd,testAdd3 , and testAdd5 methods.
    5. For methods testAdd2 and testAdd4, add callouts @group production and @group development.
    6. Right-click the calculator.php node and select Test. A dialog box opens asking which test groups you want to run. Select "Development" and click "OK". The IDE only runs tests that use @group development annotations.

For more information about the PhpUnit test group in the NetBeans IDE, see the NetBeans IDE for PHP blog post: Using the PHP Unit Test Groups (with the PHP units testing group).

Test results and IDE output

The results of the PHPUnit test are displayed in the IDE's "Test Results" and "Output" two windows. The Test Results window contains a graphics pane and a short Text pane. The Output window provides a more verbose text version of the output. In this section, you will learn more about the test Results window and the Output window.

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

    • Messages that are included with the tree entry for failed tests in the UI pane
    • The text in the right pane, which includes a link to the failed test code line
    • The ToolTip text that appears when you hover the cursor over a failed test on the UI pane

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

    • Rerun Test button
    • Show failed Tests
    • Show the passed test
    • Show tests that passed but failed
    • buttons to navigate to the next test result, and buttons to navigate to the first test result

The Output window displays the full output of the PHPUnit script. This window is useful when you cannot find the cause of the error through the information in the Test Results window. Similar to the Test Results window, the Output window includes a link to the failed test class row. It also includes buttons on the left side for re-running tests and for opening the PHP Options window.

Code Coverage

The NetBeans IDE for PHP provides code coverage and PHPUnit support. (The IDE also provides code coverage for Python). Code coverage checks whether all methods are covered by the PHPUnit test. In this section, you will learn how to use code coverage with existing Calculator classes.

Use code Coverage:

  1. Open calculator.php, and then add the add2 function with the same content as the add function. Now, theCalculator class will look like this:
    <?phpclass Calculator {    /**     * @assert (0, 0) = = 0     * @assert (0, 1) = = 1     * @assert (1, 0) = = 1     * @as SERT (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, which collects and displays the codes cover. The show editor bar is also selected by default, and is displayed.
  3. The editor now displays the Code coverage editor bar at the bottom. Because code coverage has not been tested, the editor bar reports coverage of 0%. (When you click Clear to clear the test results, the editor bar also shows coverage of 0%.) )
  4. Click Test to test the open file, or click All Tests to run all tests for the project. The "Test Results" is displayed (testing results). In addition, the Code coverage column tells you the percentage of executable code statements covered by the test. In the editor window, the overridden code is highlighted in green, and the non-overridden code is highlighted in red.

    Warning: If you regenerate the test file after adding the ADD2 function, the PHPUnit test will not run. This is because PHPUnit created two conflicting testAdd2 functions. If you plan to use PHPUnit for more than one such function, do not differentiate the function by appending numbers at the end. See the PHPUnit documentation.

  5. In the editor bar, click "Report ..." ... )。 The Code Coverage report opens, showing the results of all tests that were run on the project. Using the buttons in the report, you can clear the results, run all tests again, or deactivate code coverage (click Done).
  6. You can add other classes to the project, delete the test file, and recreate the test file, and then review the code coverage report again. The new class is listed. In the following report, another function of theCalculator class is not included in the test.
Using Project-specific configurations

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

    • Boot file
    • XML configuration file
    • Test suite
    • customizing PHPUnit Scripts

To set up your project-specific configuration, do the following:

    1. Right-click the project node or project's Test files node, and select Properties. This opens the Properties dialog box.
    2. Select the PHPUnit category. A dialog box opens where you can select a custom bootstrap, XML configuration, PHPUnit script, or test suite file.
    3. If you are unfamiliar with the structure of a bootstrap or XML configuration file, you can use the NetBeans IDE to generate the framework. You can also find instructions for using the dialog box by clicking Help.

Projects that use the custom ClassLoader require boot options , for example, by implementing the Magic function __autoload (). If you need to include a file beforehand, such as a file that defines the global constants used by multiple classes in your project, you also need to use the boot option.

The options used in command-line invocation can be defined through an XML configuration file . A complete introduction to the PHPUnit manual. You can also use an XML configuration file to define php.ini settings and global variables for test cases. You can also set boot options in the XML configuration file.

If you set up a custom test suite , the suite runs every time you select Run > Test Project. This is especially useful if you want to run only a subset of tests, or if you want to use the PHPUnit features (such as data providers) that you recently added, which you must manually add. Note that you can define multiple test suites as needed and run them individually by right-clicking the file in Project Explorer and choosing Run. To prevent confusion, NetBeans informs you whether to use a custom test suite. You can find the notification in the test Results and Output window.

You can use custom PHPUnit scripts instead of the default scripts selected in Tools > Options (option) for your project. Custom PHPUnit scripts can contain any command-line switches described in the PHPUnit manual.

Running tests on the Selenium framework

Selenium is a portable software that tests the framework of a WEB application. These tests can be written as HTML tables, encoded in a variety of common programming languages, and can be run directly in most of the current Web browsers. Selenium can be deployed on Windows, Linux, and the Macintosh. For more detailed information, see Selenium Web site.

NetBeans IDE has plug-ins that contain Selenium servers. With this plugin, you can run Selenium tests on PHP, Web applications, or Maven projects. To run the Selenium test on PHP, you need to install the testing Selenium package to the PHP engine.

Run Selenium test on PHP:

    1. Open a command prompt, and then run the pear install testing_selenium-beta command. You need to include php_home/php/pearin the Path variable. If the command is run successfully, the following prompt is displayed:install ok:channel://pear.php.net/testing_selenium-0.4.3.
    2. In the IDE, open Tools > Plugins (Plug-ins), and then install "Selenium Module for PHP".
    3. In the Projects window, right-click the node for the Calculator project. Select "New" > "Other". This opens the New File wizard. Select "Selenium" and click "Next".
    4. The first time you create a Selenium test, a dialog box opens that asks you to set the directory for the Selenium test file. This directory should be different from the PHPUnit test file directory. Otherwise, the Selenium test runs each time the unit test is run. Functional tests such as running Selenium are typically longer than running unit tests, so you might not want to run them every time you run unit tests.
    5. Accept the default settings in the name and Location page, and then click Finish. The new Selenium test file opens in the editor and is displayed in the Projects window.
    6. The Run Selenium Tests (running Selenium test) item is now added to the project's context menu. Click to Selenium the test results in the same test Results window as the PHPUnit test.
More Exercises

Here are 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 test.
    • If you are trying to learn a tutorial on creating a CRUD application with multiple parts, create a Selenium test for your final project.
Send feedback about this tutorial

To send comments and suggestions, get support, and stay up to date with the latest developments in NetBeans IDE PHP development features, please join the [email protected] mailing list. A mirror of this list is available on the NetBeans IDE forum.

Testing with PHPUnit and Selenium

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.