Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
Why is my custom TestCase class not being found when running tests through PhpUnit?

I added PHPUnit to my Symfony project as described in the documentation. Creating and running the first test which extend Symfony\Bundle\FrameworkBundle\Test\WebTestCase was no problem.

Now I would like to create a custom WebTestCase subclass which provides common features for other test classes:

// .../projectDir/tests/BaseTestCase.php
namespace My\Project\Tests;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class BaseTestCase extends WebTestCase {
    protected function doSomething() {}
}


// .../projectDir/tests/SomeTest.php
namespace My\Project\Tests;

use My\Project\Tests\BaseTestCase;

class SomeTest extends BaseTestCase {
    protected function testSomething() {
        $this->doSomething();
        ...
    }
}
 

When running php bin/phpunit I get an "Class not found" error:

$ php bin/phpunit
PHP Fatal error:  Uncaught Error: Class 'My\Project\Tests\BaseTestCase' not found in /path/to/projectDir/tests/SomeTest.php:6
Stack trace:
#0 /path/to/projectDir/vendor/phpunit/phpunit/src/Util/FileLoader.php(65): include_once()
#1 /path/to/projectDir/vendor/phpunit/phpunit/src/Util/FileLoader.php(49): PHPUnit\Util\FileLoader::load('/path/to/...')
#2 /path/to/projectDir/vendor/phpunit/phpunit/src/Framework/TestSuite.php(402): PHPUnit\Util\FileLoader::checkAndLoad('/path/to/...')
#3 /path/to/projectDir/vendor/phpunit/phpunit/src/Framework/TestSuite.php(530): PHPUnit\Framework\TestSuite->addTestFile('/path/to/...')
#4 /path/to/projectDir/vendor/phpunit/phpunit/src/TextUI/TestSuiteMapper.php(67): PHPUnit\Framework\TestSuite->addTestFiles(Array)
#5 /path/to/proje in /kunden/100170_47877/webpages/pockey/webpage/dev/vendor/phpunit/phpunit/src/TextUI/Command.php on line 98

I have double checked the class name, the namespace and use statement (which should not be necessary, since both classes are in the same folder/namespace).

Do I need to register BaseTestCase in PHPUnit somehow to be a valid superclass for my tests?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It seems you are missing the declaration of the class paths for your test directory.

Generally, for the test classes themselves it's not necessary, since PhpUnit will load the test files and read the classes directly. But any other class referenced in any of the tests will need to go through the autoloader. And if you haven't declared where to find the Test namespace, it will try to find it with your regular project files (e.g. in src/).

Add an autoload-dev key to your composer.json. E.g., something like this:

{
"autoload": {
    "psr-4": {
      "My\\Project\\": "src/"
    }
  },
  "autoload-dev": {
    "psr-4": {
      "My\\Project\\Tests\\": "tests/"
    }
  }
}

Dump the autloader after this (composer dump-autload), and try running your tests again.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!