Codeception PHP: Complete Guide with Examples
1. What Is Codeception and Why Use It
Codeception is a full-featured testing framework for PHP applications that combines the best practices of unit, functional, acceptance, and integration testing. Unlike standard PHPUnit, Codeception provides a single, intuitive API for all test types, along with powerful tools for working with browsers (via WebDriver or PhpBrowser), databases, REST APIs, and even JavaScript.
The main goal of Codeception is to make writing tests as simple and readable as possible. The framework uses a BDD-like syntax (Behavior-Driven Development), where tests are described in natural language using action methods: $I->amOnPage('/login'), $I->fillField('email', 'user@example.com'), $I->click('Login'). This allows not only developers but also testers, analysts, and managers to easily understand what is being tested.
Codeception is indispensable in projects requiring comprehensive testing — from checking individual classes (unit tests) to simulating real user actions in a browser (acceptance tests). It supports popular frameworks (Laravel, Symfony, Yii2, Zend), automatically manages database transactions, takes screenshots on errors, and generates detailed reports. If you want to improve code quality and reduce regression testing time, Codeception will be your reliable companion.
2. Installation
Codeception is distributed via Composer, the standard package manager for PHP. Make sure you have PHP 7.4 or higher and Composer installed.
Run the following command in your project root:
composer require --dev codeception/codeception
After installation, initialize Codeception in your project:
vendor/bin/codecept init
This command creates the basic directory structure: tests/ with subfolders unit, functional, acceptance, and a configuration file codeception.yml.
For browser testing, install the WebDriver module (if you plan to use Selenium or ChromeDriver):
composer require --dev codeception/module-webdriver
For REST API testing, install the REST module:
composer require --dev codeception/module-rest
3. Quick Start — Minimal Working Example
Let's create a simple unit test to check a math function. After initialization (codecept init), you'll already have a tests/unit.suite.yml file. Edit it to enable the Asserts module:
# tests/unit.suite.yml
actor: UnitTester
modules:
enabled:
- Asserts
Now create a test file tests/unit/MathTest.php:
<?php
use Codeception\Test\Unit;
class MathTest extends Unit
{
protected UnitTester $tester;
public function testAddition()
{
$result = 2 + 3;
$this->tester->assertEquals(5, $result, '2 + 3 should equal 5');
}
public function testSubtraction()
{
$result = 10 - 4;
$this->tester->assertGreaterThan(5, $result, '10 - 4 should be greater than 5');
}
}
Run the test:
vendor/bin/codecept run unit
You should see a green
Also in library
Logging for Swift (iOS/macOS): Best Libraries and Solutions
Juniper: An Elegant GraphQL Library in Rust for Modern Web Applications
Task Queues in Kotlin: Coroutines, KJob and Advanced Patterns
Machine Learning in Java: Overview of Key Libraries for Development