PHP Fatal error: Class not found - PHPUnit

Call phpunit from the root folder:

$ cd C:\xampp\htdocs\PHPUnitTestProject\
$ vendor\bin\phpunit tests/PagesTest.php

Adding bootstrap="vendor/autoload.php" in phpunit.xml.dist solved the issue for me.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"> <!-- in here -->
    <php>
        <!-- ... -->
    </php>
    <testsuites>
       <!-- ... -->
    </testsuites>
</phpunit>

You need to point to the tested class, so in pagesTest.php add a require:

require __DIR__ . "/../controllers/Pages.php";

Or if you are using autoloading, then you can bootstrap the autoload in your command line

phpunit --bootstrap src/autoload.php

Or you can set up a phpunit.xml configuration file like this example (from the PHPUnit page I linked to above):

<phpunit bootstrap="src/autoload.php">
  <testsuites>
    <testsuite name="money">
      <directory>tests</directory>
    </testsuite>
  </testsuites>
</phpunit>

Which you then use with the --configuration option.


Try composer dump-autoload -o command

Tags:

Php

Phpunit