Skip to content
Latchkey

PHPUnit "Class \"PHPUnit\Framework\TestCase\" not found" in CI

PHP could not find PHPUnit’s TestCase base class. Almost always PHPUnit itself is not installed in CI - typically because composer install --no-dev skipped dev dependencies - or the test bootstrap never loaded Composer’s autoloader.

What this error means

The test run fails immediately with "Class \"PHPUnit\\Framework\\TestCase\" not found" when loading a test class. PHPUnit, a require-dev dependency, is absent from vendor/.

PHP error
PHP Fatal error:  Uncaught Error: Class "PHPUnit\Framework\TestCase" not found
in /app/tests/UserTest.php:7

Common causes

Dev dependencies were not installed

composer install --no-dev (common for production builds) skips require-dev, so PHPUnit is never installed. Running tests then fails to find TestCase.

The Composer autoloader was not loaded

A custom bootstrap that does not require vendor/autoload.php, or a missing/empty vendor/, means no class - including PHPUnit’s - can be autoloaded.

How to fix it

Install dev dependencies in the test job

The test stage must install require-dev, even if the deploy stage uses --no-dev.

Terminal
composer install --no-interaction --prefer-dist
# NOT: composer install --no-dev   (skips PHPUnit)

Ensure the autoloader is loaded

Point PHPUnit’s bootstrap at Composer’s autoloader.

phpunit.xml
<!-- phpunit.xml -->
<phpunit bootstrap="vendor/autoload.php">

Confirm PHPUnit is present and runnable

Terminal
composer show phpunit/phpunit
vendor/bin/phpunit --version

How to prevent it

  • Install dev dependencies (no --no-dev) in any job that runs tests.
  • Set bootstrap="vendor/autoload.php" in phpunit.xml.
  • Run vendor/bin/phpunit --version early to assert PHPUnit is installed.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →