Testing with PHPSpec
With PhpStorm, you can practice behaviour-driven development by running specifications using the PHPSpec toolset.
Before you start
How do I install PHPSpec using Composer in PhpStorm?
- On the context menu of
composer.json, choose Composer | Manage Dependencies.
- In the Manage Composer Dependencies Dialog that opens, select the
phpspec/phpspecpackage from the Available Packages list, possibly using the search field. The list shows all the available packages, the packages that are already installed are marked with a tick.
Choose the relevant version from the Version to install list.
- If necessary, expand the Settings hidden area and specify the advanced installation options. In the Command line parameters text box, type the additional command line parameters. For example, to have the package added to the
require-devsection instead of the default
--dev. For more information about Composer command line options during installation, see https://getcomposer.org/doc/03-cli.md.
- Click Install.
Learn more about PHPSpec installation from PHPSpec Official website.
How do I integrate PHPSpec with PhpStorm in a project?
Step 1: Choose how to use Codeception
Open the Settings / Preferences dialog by pressing Ctrl+Alt+S, or alternatively choose on Windows and Linux or on macOS. Expand the node and select under .
On the Test Frameworks page that opens, click in the central pane and choose the configuration type from the list:
Step 2: Choose the PHP interpreter to use
To use PHPSpec with a remote PHP interpreter, choose one of the configurations from the dialog box that opens:
Step 3: Specify the PHPSpec library to use
In the Path to PHPSpec executable text box, specify the location of
phpspec. PHPSpec does not necessarily have to be installed under the current project root. Click next to the Path to PHPSpec directory or phar file text box. PhpStorm detects the version of PHPSpec and displays it below the text box.
Specify the PHPSpec configuration file to use
In the Test Runner area, appoint the configuration
.yml file to use for launching and executing specifications.
By default, PHPSpec looks for a
phpspec.yml or a
phpspec.yml.dist configuration file in the project root folder. You can appoint a custom configuration file.
- Clear the Default configuration file checkbox to have PHPSpec use the
phpspec.yml.distconfiguration file from the project root folder. If no such file is found, test execution fails, therefore it may be more reliable to specify the configuration file explicitly.
- Select the Default configuration file checkbox to specify your own
.ymlconfiguration file. This file will be later used as default in all PHPSpec run/debug configurations.
In the text box, specify the location of the configuration file to use. Type the path manually or click and choose the file in the dialog box that opens.
spec_prefixfrom the configuration file specified in the Default Configuration File field. The default value is
spec. See PHPSpec Configuration: PSR-4 and PHPSpec Configuration: Spec and Source Location for details.
How do I run and debug PHPSpec tests?
For information about writing PHPSpec specifications, see http://www.phpspec.net/en/stable/manual/getting-started.html#specifying-behaviour. To run or debug your tests, do one of the following:
Option 1: To run or debug PHPSpec tests
In the Project tool window, select the file or folder to run your tests from and choose Run <file_or_folder_name> or Debug <file_or_folder_name> on the context menu of the selection: PhpStorm generates a default run configuration and starts a run/debug test session with it.
Option 2: To save an automatically generated default configuration
After a test session is over, choose Save <default_test_configuration_name> on the context menu of the file or folder and choose Save <default_configuration_name> on the context menu.
Option 3: To run or debug tests through a previously saved run/debug configuration
Choose the required PHPSpec configuration from the list on the tool bar and click or .
Option 4: To create a custom run/debug configuration
- In the Project view, select the file or folder with the tests to run and choose Create run configuration on the context menu. Alternatively, choose Run | Edit Configurations on the main menu, then click and choose PHPSpec from the list.
- In the Run/Debug Configuration: PHPSpec dialog that opens, specify the the tests to run and customize the behavior of the current PHP interpreter by specifying the options and arguments to be passed to the PHP executable file.
How do I monitor test results?
PhpStorm shows the results of test execution in the Test Runner tab of the Run Tool Window. The tab is divided in 2 main areas. In the left-hand area you can drill down through all unit tests to see which ones succeeded and which ones failed. In this area you can also filter tests and export results.
The right-hand area shows us the raw PHPSpec output: Use the context menu in the left-hand area to run specific tests or navigate to the source code.