Creating a base test-class for PHPUnit and extending it for common functionality results in class not found error

Active3 hr before
Viewed126 times

8 Answers


I'm running PHPUnit using a bootstrap file for autoloading classes (generated by composer). ,I'm not sure if you still need a solution, but this worked for me: Non-testable base class extending PHPUnit_Framework_TestCase, Can a random forest learn time series correlations? , May I bring weapons and/or defend myself when trespassing for a legal reason?


   "autoload": {
      "psr-4": {
         "MyLibrary\\": "src/"
   "autoload-dev": {
      "psr-4": {
         "MyLibrary\\Tests\\": "tests/"
load more v

I'm running PHPUnit using a bootstrap file for autoloading classes (generated by composer). ,Fatal error: Class 'BaseTest' not found in BlahBlahTest.php on line 13,PHPunit does not use use the Composer autoloader to find any of your test classes. It just scans any directory you give it and operates on one file at a time.,Hence it does not know about any other class than the one in the file it is currently operating on. That is where the bootstrap file comes into play.

All my tests load up classes just fine, but for two of my tests, I made a "base" test class which extends PHPUnitFrameworkTestCase (similar to PHPUnit_Framework_TestCase before PHPUnit7), and then two test classes that extend the base class, similar structure to the following example code:

    abstract class BaseTest extends PHPUnitFrameworkTestCase {
       function setUp();

       function getCommonTestVariables() {
          // ...

       function runCommonTests() {
          // ...
    class BlahBlahTest extends BaseTest {
       function setUp() {
          $variables = $this - > getCommonTestVariables();

       function testThings() {
          $this - > runCommonTests();
load more v

10. Extending PHPUnit Subclass PHPUnit\Framework\TestCase Write custom assertions Extending the TestRunner Configuring extensions ,Sometimes you want to assert that the execution of a method, for instance, generates an expected output (via echo or print, for example). The PHPUnit\Framework\TestCase class uses PHP’s Output Buffering feature to provide the functionality that is necessary for this.,ClassTest inherits (most of the time) from PHPUnit\Framework\TestCase.,ClassTest inherits (most of the time) from PHPUnit\Framework\TestCase.

< ? php declare(strict_types = 1);
use PHPUnit\ Framework\ TestCase;

final class StackTest extends TestCase {
   function testPushAndPop(): void {
      $stack = [];
      $this - > assertSame(0, count($stack));

      array_push($stack, 'foo');
      $this - > assertSame('foo', $stack[count($stack) - 1]);
      $this - > assertSame(1, count($stack));

      $this - > assertSame('foo', array_pop($stack));
      $this - > assertSame(0, count($stack));
load more v

The classes containing tests should extend Cake\TestSuite\TestCase, Cake\TestSuite\IntegrationTestCase or \PHPUnit\Framework\TestCase.,PHP files containing tests should be in your tests/TestCase/[Type] directories.,Like other classnames, the test case classnames should match the filename. RouterTest.php should contain class RouterTest extends TestCase.,In addition to the above assertion methods, you can also use all of the assertions in TestSuite and those found in PHPUnit.

$ php composer.phar require--dev phpunit / phpunit: "^8.5"
load more v

To write a test for this in PHPUnit, we can do the following:,Now that we have created a base test, and are aware of the PHPUnit configuration, it is time to run our tests for the first time.,First, make a BetaTest class using the make:test artisan command:,This tells PHPUnit to run the tests it finds in the ./tests/Unit and ./tests/Feature directories, which, as we have previously learned, are the convention for storing tests.

If you have the Laravel installer set up, you can create a new test application by running:

$ laravel new phpunit - tests
load more v

An ExampleTest.php file is provided in both the Feature and Unit test directories. After installing a new Laravel application, execute the vendor/bin/phpunit or php artisan test commands to run your tests.,Once the test has been generated, you may define test methods as you normally would using PHPUnit. To run your tests, execute the vendor/bin/phpunit or php artisan test command from your terminal:,In addition to the phpunit command, you may use the test Artisan command to run your tests. The Artisan test runner provides verbose test reports in order to ease development and debugging:,If you would like to create a test within the tests/Unit directory, you may use the --unit option when executing the make:test command:

To create a new test case, use the make:test Artisan command. By default, tests will be placed in the tests/Feature directory:

php artisan make: test UserTest
load more v

Each suite has its own coding conventions. For example, all tests in the CRM suite extend the base class CiviUnitTestCase and execute on the headless database. They require a special environment variable (CIVICRM_UF)., Writing tests for core CiviUnitTestCase CiviEndToEndTestCase ,Executing tests based on CiviUnitTestCase requires setting an environment variable, CIVICRM_UF=UnitTests.,The change was implemented over the course of two versions. PHPUnit 5 allows for both the old class name and the new one, while PHPUnit 6 only allows for the new one. Meanwhile, PHPUnit 4 does not support PHP versions newer than 5.6.

$ cd / path / to / my / project
$ phpunit5. / tests / MyTest.php
load more v

Ensure Composer dependencies are installed,Setting up to run PHPUnit tests,PHPUnit runs all the tests in Drupal 8 and above. Earlier versions of Drupal used the Simpletest Module for testing.,Running PHPUnit tests

The result should look something like this:

<phpunit bootstrap="tests/bootstrap.php" colors="true" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" beStrictAboutChangesToGlobalState="true" printerClass="\Drupal\Tests\Listeners\HtmlOutputPrinter">

      <!-- Set error reporting to E_ALL. -->
      <ini name="error_reporting" value="32767" />
      <!-- Do not limit the amount of memory tests take to run. -->
      <ini name="memory_limit" value="-1" />
      <!-- Example SIMPLETEST_BASE_URL value: http://localhost -->
      <env name="SIMPLETEST_BASE_URL" value="http://drupal8.localhost" />
      <!-- Example SIMPLETEST_DB value: mysql://username:password@localhost/databasename#table_prefix -->
      <env name="SIMPLETEST_DB" value="mysql://drupal8:drupal8@localhost/drupal8" />
      <!-- Example BROWSERTEST_OUTPUT_DIRECTORY value: /path/to/webroot/sites/simpletest/browser_output -->
      <env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/var/www/sites/simpletest/browser_output" />
load more v

Other "error-creating" queries related to "Creating a base test-class for PHPUnit and extending it for common functionality results in class not found error"