PhpStorm中使用PHPUnit报错:Fatal error: Class 'PHPUnit_Framework_TestCase' not found
Fixing "Fatal error: Class 'PHPUnit_Framework_TestCase' not found" in PhpStorm (PHP 7.1.12 + PHPUnit 7.1.5)
Hey there, I’ve dealt with this exact issue when transitioning to newer PHPUnit versions — let’s break down the fixes step by step:
1. Update Your Test Class to Use PHPUnit 7+'s Namespace
PHPUnit 7.0+ dropped the old global PHPUnit_Framework_TestCase class in favor of namespaced classes. This is the most common cause of this error.
Instead of this outdated code:
class MyTest extends PHPUnit_Framework_TestCase { // ... test methods ... }
Use either of these valid approaches:
- With a
usestatement (cleaner for multiple test classes):
use PHPUnit\Framework\TestCase; class MyTest extends TestCase { // ... test methods ... }
- Or use the full namespace directly:
class MyTest extends \PHPUnit\Framework\TestCase { // ... test methods ... }
2. Verify PhpStorm's PHPUnit Configuration
PhpStorm might still be pointing to an old setup or missing the autoloader:
- Open File > Settings > Languages & Frameworks > PHP > Test Frameworks
- If you installed PHPUnit via Composer (recommended), select Composer autoloader and browse to your project's
vendor/autoload.phpfile. - If using a global PHPUnit installation, confirm the path points to your 7.1.5 version, and the selected PHP interpreter is set to 7.1.12.
- If you installed PHPUnit via Composer (recommended), select Composer autoloader and browse to your project's
- Check your test run configurations: Go to Run > Edit Configurations — ensure each test configuration uses the correct PHPUnit setup (no leftover old configurations pointing to deprecated paths).
3. Refresh Composer Autoload & Dependencies
- First, confirm you have the correct PHPUnit version installed: Run
composer show phpunit/phpunitin your project root — it should display 7.1.5. - If the version is correct but the error persists, refresh the autoloader cache:
composer dump-autoload
4. Clear PhpStorm's Cache
PhpStorm’s cached metadata can sometimes cause false "class not found" errors:
- Go to File > Invalidate Caches...
- Select Invalidate and Restart — this clears the cache and restarts PhpStorm, which often resolves class detection issues.
5. Test via Command Line First
To rule out PhpStorm-specific issues, run your test directly from the command line:
phpunit path/to/your/test-file.php
- If the command line works but PhpStorm doesn’t, double-check your PhpStorm configuration steps above.
- If the command line also throws the error, go back to step 1 — your test code is still using the old class name.
内容的提问来源于stack exchange,提问作者rubyist
相关产品推荐
相关产品推荐

