PHP TDD Visual Studio Code Extension
This is a Visual Studio Code extension to assist with TDD in PHP. It includes tools that help to create, run and edit unit tests in your PHP application. By default, this extension uses PHPUnit, but it can be configured to run other unit test frameworks.
- Run a method or class unit test by positioning cursor and running the Run Unit Test command (default hotkey is Ctrl+Alt+T)
- Results are displayed in the status bar, unit test failures display as warnings in the Problems list
- Automatically creates a unit test (and class) for you if one is not already created
- Automatically creates PSR-4 namespaces in your composer.json so your unit tests are easier to write
- PHP (for tokenizing source code)
- Composer installed globaly (to install PHPUnit and to update namespace directories)
Note: As of version 0.1.0, if your PHP does not have JSON and tokenization built into it (i.e. they get loaded as extensions), they will get properly loaded if the
enablePHPExtensions configuration property is set to true.
Make sure you have PHP and Composer installed, and that you can run
composer from a command prompt / shell. If not, use the links in the Requirements section to install.
To initialize the php-tdd, open a Workspace and run the command "PHP TDD: Initialize PHP Unit Test Project", which will do the following (by default configuration). The extension will copy files required for unit tests to the workspace folder. It will then ensure that Composer and PHPUnit are installed in the
Run or Edit a Single Unit Test
Once php-tdd is initialized, you can open a PHP file and move your cursor to a function or class (outside of a function) and run the command "PHP TDD: Run PHP Unit Test". If there is a unit test defined as a "@testFunction" comment line, it will be run, and the results shown in the status line (pass or fail). If there is not a unit test defined, a unit test will be created and brought up to edit. You can later edit the unit test by moving your cursor to the function or class, and run the command "PHP TDD: Edit PHP Unit Test".
All PHP-TDD Commands
- Initialize PHP Unit Test Project: Initialize, or re-initialize, files required for php-tdd unit test project files to the current workspace folder. If there is more than one workspace folder, and no file is open, you will be prompted to select the workspace folder.
- Edit PHP Unit Test: Create or edit a unit test for the currently selected function or class. If a function or class is not selected at the current cursor position, a warning will be displayed.
- Run PHP Unit Test: Run a unit test for the currently selected function or class. If no test function is defined, one will be created and activated for editing. If a function or class is not selected at the current cursor position, a warning will be displayed.
- Run PHP All Unit Tests: Run all unit tests
- Run PHP All Unit Tests With Coverage: Run all unit tests, including code coverage, and then display the code coverage report
- Clear All Unit Test Warnings & Errors: Clear any warnings for failed unit tests and the PHP-TDD output window