PHPUnit Setup and First Test
Install PHPUnit via Composer and write your first test class.
PHPUnit Setup and First Test is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is PHPUnit?
PHPUnit is PHPs standard unit testing framework. It lets you write automated tests that verify individual units of code in isolation.
Installing PHPUnit
Install via Composer as a dev dependency.
$ composer require --dev phpunit/phpunit "^11.0"phpunit.xml Configuration
Create a phpunit.xml file to configure the test suite.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Unit">
<directory>tests/</directory>
</testsuite>
</testsuites>
</phpunit>Writing a Test Class
Extend PHPUnit\Framework\TestCase. Each public method prefixed with test is a test.
<?php
use PHPUnit\Framework\TestCase;
class CalculatorTest extends TestCase
{
public function test_addition(): void
{
$calc = new Calculator();
$this->assertEquals(5, $calc->add(2, 3));
}
}The Class Under Test
The Calculator class being tested:
<?php
class Calculator
{
public function add(int $a, int $b): int
{
return $a + $b;
}
}Running Tests
Run PHPUnit from the project root.
$ vendor/bin/phpunit
...
OK (1 test, 1 assertion)Test Naming Conventions
Two styles: test_method_name() (snake_case with test prefix) or use the @test annotation without the prefix.
<?php
/** @test */
public function addition_returns_correct_sum(): void
{
$this->assertEquals(5, (new Calculator())->add(2, 3));
}Directory Structure
Common layout: tests mirror the src structure.
src/Calculator.php → Calculator tests/CalculatorTest.php → CalculatorTest
setUp() and tearDown()
Run code before/after each test method. Use setUp() to create the object under test.
<?php
protected function setUp(): void
{
$this->calc = new Calculator();
}
public function test_add(): void
{
$this->assertEquals(7, $this->calc->add(3, 4));
}setUpBeforeClass() and tearDownAfterClass()
Run code once before/after all tests in the class — useful for expensive setup like database connections.
Code Coverage
Generate a coverage report (requires Xdebug or PCOV):
$ vendor/bin/phpunit --coverage-html coverage/Summary
Install PHPUnit via Composer dev. Create a TestCase subclass with test_*() methods. Run with vendor/bin/phpunit. Use setUp() for shared object creation.
Quick Check
Which base class must PHPUnit test classes extend?
Frequently asked questions
Is the “PHPUnit Setup and First Test” lesson free?
Yes — the full text of “PHPUnit Setup and First Test” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “PHPUnit Setup and First Test”?
Install PHPUnit via Composer and write your first test class. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “PHPUnit Setup and First Test” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- PHPUnit Setup and First Test
- Assertions and Test Methods
- Data Providers for Parameterized Tests
- Mocking Dependencies with PHPUnit