Codeception
PhpStorm provides support for running unit, functional, and acceptance tests with the Codeception test framework, versions 2.2.0 and later.
Make sure the PHP interpreter is configured in PhpStorm on the PHP page, as described in Configure local PHP interpreters and Configure remote PHP interpreters.
Before you start, make sure Composer is installed on your machine and initialized in the current project as described in Composer dependency manager.
Download codeception.phar at the Codeception Installation page and save it under the root of the project where Codeception will be later used.
You can also save codeception.phar in a different location and configure it as an include path. In either case, PhpStorm will include codeception.phar in indexing, so it will successfully resolve references to the Codeception classes and thus provide you with full coding assistance.
Inside composer.json, add the
codeception/codeception
dependency record to therequire
orrequire-dev
key. To get code completion for the package name and version, press CtrlSpace.Do one of the following:
Click the Install shortcut link on top of the editor panel.
If the Non-installed Composer packages inspection is enabled, PhpStorm will highlight the declared dependencies that are not currently installed. Press AltEnter and select whether you want to install a specific dependency or all dependencies at once.
Learn more about installing Codeception from Codeception Official website.
Clicking next to the package record in the composer.json editor gutter will take you to the corresponding Settings page where you can configure Codeception manually.
![Gutter icon for codeception settings in composer.json Gutter icon for codeception settings in composer.json](https://resources.jetbrains.com/help/img/idea/2024.3/ps_codeception_composer_json_gutter_icon.png)
If you use a local PHP interpreter, PhpStorm performs initial Codeception configuration automatically. In the case of remote PHP interpreters, manual Codeception configuration is required.
Having installed Codeception, you need to initialize it in your project by generating a codeception.yml configuration file.
Open the built-in PhpStorm Terminal AltF12 and at the command prompt type one of the following commands depending on the installation mode and your current operating system:
If you installed codeception.phar in your project, type
php codecept.phar bootstrap
for Windows and macOS orcodecept bootstrap
for Linux.If you installed Codeception globally through Composer, type
codecept bootstrap
for all platforms.
Store the codeception.yml or codeception.dist.yml configuration file under the project root.
PhpStorm detects the installed Codeception executable (or executables, if the project has Composer-managed subprojects with a specified Codeception dependency) and creates a test framework configuration per each installed Codeception executable on the Test Frameworks page.
tip
You can manually configure the used local or remote PHP interpreter separately for each of the Composer-managed test framework configurations.
![Codeception configurations Codeception configurations](https://resources.jetbrains.com/help/img/idea/2024.3/codeception_test_framework_configurations.png)
If the codeception.yml or codeception.dist.yml configuration file is detected under the project (or subproject) root automatically or specified explicitly during manual configuration, PhpStorm also creates a respective Codeception run/debug configuration.
![Codeception run/debug configurations Codeception run/debug configurations](https://resources.jetbrains.com/help/img/idea/2024.3/codeception_run_debug_configurations.png)
In the Settings dialog (CtrlAlt0S) , go to PHP | Test Frameworks.
On the Test Frameworks page that opens, click
in the central pane and choose the configuration type from the list:
In local configurations, the default project PHP interpreter is used. For more information, refer to Default project CLI interpreters.
To use Codeception with a remote PHP interpreter, choose one of the configurations in the dialog that opens:
In the Codeception Library area, specify the location of the Codeception executable file or codeception.phar archive in the target environment. For example, if you installed Codeception through Composer, the executable file is stored in vendor
/bin . Click/codecept next to the Path to Codeception directory or phar file field. PhpStorm detects the version of Codeception and displays it below the field.
In the Test Runner area, set the path to the configuration YML file to be used for launching and executing scenarios.
Clear the Default configuration file checkbox to have Codeception use the codeception.yml or codeception.dist.yml configuration 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 the path to the YML file to be used as the default configuration file in all Codeception run/debug configurations.
Open the Create New PHP Test dialog by doing any of the following:
Go to File | New. Then, choose either PHP Test | Codeception Unit Test or PHP Test | Codeception Functional Test from the context menu.
In the Project tool window, press AltInsert or right-click the PHP class to be tested and choose either New | PHP Test | Codeception Unit Test or New| PHP Test | Codeception Functional Test.
In the editor of the PHP class to be tested, place the caret at the definition of the class. Then, press AltEnter and select Create New Test from the popup menu. This way, you can generate a test for a PHP class defined among several classes within a single PHP file.
To create a test for a certain method, place the caret within the method declaration. The chosen method will be automatically selected from the list of methods in the Create New PHP Test dialog.
In the Create New PHP Test dialog that opens, provide the parameters of the generated test.
Test file template: the template based on which PhpStorm will generate the test class. Make sure that either Codeception Unit or Codeception Functional is selected from the list.
Name: the name of the test class. PhpStorm automatically composes the name from the production class name as <production class>Test.php (for Codeception Unit Test) or <production class>Cest.php (for Codeception Functional Test) .
Directory: the folder for the test class file, which is automatically suggested based on the containing directory and namespace of the production class, the configured test sources root and its PSR-4 package prefix, or the
tests
value specified in the codeception.yml configuration file.To specify a different folder, click
next to the Directory field and choose the relevant folder.
Namespace: the namespace the test class will belong to, which is automatically suggested based on the containing directory and namespace of the production class, the configured test sources root and its psr-4 package prefix, or the
namespace
value specified in the codeception.yml configuration file.Member: the list of production class methods to generate test method stubs for. Select the checkboxes next to the required production class methods. To include inherited methods from parent classes, select the Show inherited methods checkbox.
PhpStorm will automatically compose the test methods' names as
test<production method>
. You can customize the code templates used for generating test method stubs on the Code tab of the File and Code Templates settings page.
After the test is created, you can navigate back to the production class by choosing Navigate | Go to Test Subject. For more information, refer to Navigate between a test and its test subject.
note
If you rely on the Codeception configuration file for providing the containing folder and namespace for the test class, make sure that it is selected on the Test Frameworks page as described in Integrating Codeception with a PhpStorm project.
For more information about writing Codeception tests, refer to Unit Tests, Acceptance Tests, and Functional Tests.
In the Project tool window, select the file or folder to run your tests from and choose Run '<file or folder>' or Debug '<file or folder>' from the context menu of the selection:
PhpStorm generates a default run configuration and starts a run or debug test session with it.
After a test session is over, choose Save Configuration from the context menu of the respective run/debug configuration in the run widget.
Choose the required Codeception configuration from the list on the toolbar and click
or
.
In the Project tool window, select the file or folder with the tests to run and choose Create run configuration from the context menu. Alternatively, go to Run | Edit Configurations in the main menu, then click
and choose Codeception from the list.
In the Codeception dialog that opens, specify the scenarios to run, choose the PHP interpreter to use, and customize its behavior by specifying the options and arguments to be passed to the PHP executable.
PhpStorm shows the tests execution results in the Test Runner tab of the Run tool window.
![ps_test_result_codeception.png ps_test_result_codeception.png](https://resources.jetbrains.com/help/img/idea/2024.3/ps_test_result_codeception.png)
The tab is divided into 2 main areas:
The left-hand area lets you drill down through all unit tests to see the succeeded and failed ones. You can filter tests, export results, and use the context menu commands to run specific tests or navigate to the source code.
The right-hand area displays the raw Codeception output.
You can have PhpStorm re-run tests automatically when the affected code is changed. This option is configured per run/debug configuration and can be applied to a test, a test file, a folder, or a composite selection of tests, depending on the test scope specified in this run/debug configuration.
Run the tests.
On the Run toolbar, click
Rerun Automatically.
Optionally, set a time delay for launching the tests upon the changes in the code. To do so, on the Run toolbar, click
and select
Test Runner Settings | Set AutoTest Delay.
Thanks for your feedback!