Testing your Code Using PHPUnit
This tutorial demonstrates how to create and run PHPUnit tests on your code. You will learn how to create and run single unit test cases and test suites containing a number of test cases.
Unit testing is a procedure to test your code to ensure that individual units of source code are working properly and that the right output is being generated. Tests can be run on all or some functions within files, meaning that tests can be conducted before the file has been fully developed. Each test case should be independent of others to ensure that test results can pinpoint the location of the error.
Running unit tests can ensure that your code is stable and functioning correctly, and can help you to diagnose errors.
Tutorial Content
In this tutorial, you will learn:
- To create a PHPUnit Test Case
- To run a PHPUnit Test Case
- To analyze errors
- To create and run a PHPUnit Test Suite
- To generate a PHPUnit test report
Provides Items
- Throughout the tutorial, you will be provided with code snippets to insert in your project.
Prerequisites
- Zend Studio 10.5.0 or above which can be downloaded from the Zend Studio Downloads page. For information on installation, see the Zend Studio Installation Guide.
- An existing PHP project. For information on creating a PHP project, see Creating New PHP Projects.
Related Media
To better understand the procedures described in this tutorial, watch this video:
Zend Studio will automatically create test case files which can be run in order to check the functionality of your code. The first step of this tutorial is to to create a PHPUnit Test Case.
|
|
|
To create a PHPUnit Test Case:
|
Note: Test functions will have been created, but parameters need to be entered to ensure the test runs effectively. For more on this, see Running your Unit Test Case, below. |
Now that you've created your PHPUnit Test Case, you will now need to customize it by entering relevant parameters to be checked before being able to run your test.
|
|
|
To configure and run your test case:
The numbers 1 and 2 indicate that when the test case is run, the parameters 1 and 2 will be entered into the 'add' function in your Calculator file (i.e. the test will try to add 1 + 2). The last number (3) indicates that the expected result is 3. If the result is something other than 3, the test will report a failure for this function.
Select each required operation (subtract, divide or multiply), and enter variables where x and y are the two parameters which will be entered into the calculator, and z is the expected result.
|
|
Once a PHPUnit test has been run, the results can be viewed and analyzed in order to diagnose and correct problematic sections of code.
Now, say you have a few tests within a project which you would like to run at once. A number of different PHPUnit Test Cases can be unified into one Unit Test Suite file which will run all your unit tests at once.
|
|
|
To create a PHPUnit Test Suite:
|
|
Your final step is to share the results of your testing by generating a Test Report.
|
|
|
To generate a report on your Unit Test results:
|
For more information on the different types of reports, see PHPUnit Testing. |