Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RaphaelTokyo84's avatar

Phpunit: class not found error

Hi everyone, Currently doing the Katas tutorial and I ran into a problem. When executing phpunit I got the following error:

PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 39 ms, Memory: 4.00 MB

There was 1 error:

1) RomanNumeralsTest::it_generates_the_roman_numeral_for_1
Error: Class 'App\RomanNumerals' not found

/Users/exoskill/kata/tests/RomanNumeralsTest.php:11

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

Here are my class and settings files:

tests/RomanNumeralsTest.php

<?php
        
    use App\RomanNumerals;
    use PHPUnit\Framework\TestCase;
  
    class RomanNumeralsTest extends TestCase
    {
        /** @test */
        function it_generates_the_roman_numeral_for_1()
        {
            $this->assertEquals('I', RomanNumerals::generate(1));
        }
    }

src/RomanNumerals.php

<?php
    namespace App;

    class RomanNumerals
    {
        public static function generate($number)
        {
            return 'I';
        }
    }

composer.json

{
    "require": {
        "phpunit/phpunit": "^7.5"
    },
    "autoload": {
        "psr-4": {
            "App\":"src/"
        }
    }
}

I executed composer dump-autoload several times. And can't see anything wrong with my class naming or paths.

Does anyone have a clue of what the problem is?

0 likes
3 replies
Lke's avatar

i think the problem is that you have in your machine a global phpunit installation.

when you do phpunit it references that installation and phpunit "global" does not know where to find your vendor/autoload.php, so it can't find the class.

Here you have 3 solution that you can try:

  1. use your global installation giving it the --bootstrap flag like so, it might work, depending on the local phpunit version:
phpunit --bootstrap="vendor/autoload.php"
  1. use the project phpunit composer installation:
./vendor/bin/phpunit 
  1. add a phpunit.xml config file in the root directory of your project, try this one that i use. adding the config file both wile work phpunit and ./vendor/bin/phpunit

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

for the solutions, there no good or bad one, try whatever fits your worflow.

you can verify the 2 installations of phpunit by running.

  1. check the global. phpunit --version
  2. check the local project version ./vendor/bin/phpunit --version

hope it helps.

Tray2's avatar

It's most likely a namespace issue.

Try changing

 $this->assertEquals('I', RomanNumerals::generate(1));

To

 $this->assertEquals('I', \RomanNumerals::generate(1));

Or

 $this->assertEquals('I', App\RomanNumerals::generate(1));

Or maybe even

 $this->assertEquals('I', \App\RomanNumerals::generate(1));
dignat's avatar

Hi I recently had a similar problem with namespaces and phpunit. Make sure the name case of your subfolder matches the case name you set as a namespace in the class. example :

folder name : testdome namespace: \App\Testdome this would not find the class in the phpunit test.

1 like

Please or to participate in this conversation.