Installing PHPUnit
Installing PHPUnit
Before we write any tests, we need to get PHPUnit installed and ready to run.
You'll need Composer installed on your machine to follow along. If you haven’t already, visit getcomposer.org/download for installation instructions.
Step 1: Create a project folder
Start by creating a new directory for your project and navigating into it:
mkdir testing-php cd testing-php
Step 2: Install PHPUnit with Composer
Install PHPUnit as a development dependency:
composer require --dev phpunit/phpunit
Composer will automatically generate a composer.json
file and download PHPUnit into the vendor/
directory.
You can now run PHPUnit using:
./vendor/bin/phpunit --version
If you see a version number, your setup is working.
Why install locally instead of globally?
Installing PHPUnit locally (per project) is the best practice because:
- Each project can use its own PHPUnit version
- It keeps your setup portable and reproducible
- It avoids conflicts with other global tools
You can install it globally if needed:
composer global require phpunit/phpunit
…but we won’t use that approach in this course.
Step 3: Run PHPUnit
Try running PHPUnit now:
./vendor/bin/phpunit
You won’t have any tests yet, but PHPUnit should run and tell you so. That means your setup is complete.
In the next lesson, you’ll create your first test file and see it in action.
0 comments