Testing with Codeception
IntelliJ IDEA provides support for running unit, functional, and aceptance tests with the Codeception test framework, versions 2.2.0 and higher.
Before you start
- Make sure the PHP and Codeception plugins are installed and enabled. The plugins are not bundled with IntelliJ IDEA, but they can be installed from the JetBrains plugin repository as described in Installing, Updating and Uninstalling Repository Plugins and Enabling and Disabling Plugins. Once enabled, the plugins are available at the IDE level, that is, you can use them in all your IntelliJ IDEA projects.
- Make sure the PHP interpreter is configured in IntelliJ IDEA on the PHP page, as described in Configuring Local PHP Interpreters and Configuring Remote PHP Interpreters.
Where do I get Codeception from?
codeception.pharat the Codeception Installation page.
codeception.pharunder the root of the project where Codeception will be later used. You can also save
codeception.pharanywhere else and configure it as an include path. In either case, IntelliJ IDEA will include
codeception.pharin indexing, so IntelliJ IDEA will successfully resolve references to Codeception classes and thus provide you with full coding assistance.
- On the context menu of
composer.json, choose Composer | Manage Dependencies.
- In the Add Composer Dependency dialog that opens, select the
codeception/codeceptionpackage 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.
- 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.
How do I initialize Codeception in a project?
To generate a
codeception.yml configuration file, open the built-in IntelliJ IDEA Terminal (press Alt+F12 or choose on the main menu) 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.pharin your project, type
php codecept.phar bootstrapfor Windows and macOS or
codecept bootstrapfor Linux.
- If you installed Codeception through Composer, type
codecept bootstrapfor all platforms.
How do I integrate Codeception with IntelliJ IDEA in a project?
Step 1: Choose how to use Codeception
Open the Test Frameworks page (File | Settings | Languages and Frameworks | PHP | Test Frameworks for Windows and Linux or IntelliJ IDEA | Preferences | Languages and Frameworks | PHP | Test Frameworks for macOS). Click in the central pane and choose the configuration type from the list:
Step 3: Specify the Codeception library to use
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/codecept. Click next to the Path to Codeception directory or phar file text box. IntelliJ IDEA detects the version of Codeception and displays it below the text box.
By default, Codeception looks for a
codeception.yml configuration file in the project root folder. You can appoint a custom configuration file.
- Clear the Default configuration file check box to have Codeception use the
codeception.ymlconfiguration 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 check box to specify your own
.ymlconfiguration file. This file will be later used as default in all Codeception 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.
How do I run and debug Codeception tests?
Option 1: To run or debug Codeception 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: IntelliJ IDEA 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.
- 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 Codeception from the list.
- In the Run/Debug Configuration: Codeception 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?
IntelliJ IDEA 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 Codeception output: Use the context menu in the left-hand area to run specific tests or navigate to the source code.