tam5
tam5

Reputation: 3227

PHPUNIT_Framework_TestCase not found when running single test

I am trying to use PHPUnit testing with Laravel 5.2.10. I have PHPUnit 5.1.4 installed locally within my project.

When I run:

phpunit

PHPUnit runs all tests in my test directory perfectly.

However, when I run:

phpunit tests/unit/testThatFails.php

I get:

PHP Fatal error:  Class 'PHPUNIT_Framework_TestCase' not found in ...

The strange thing is that when I run phpunit it is succesfully running every test in the test directory, including this testThatFails.php just fine.

Why does it break when I try to run just one test?

Update:
Here is my phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory suffix=".php">app/</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>
</phpunit>

Update:
For now, since I am using Laravel, I am circumventing this issue by extending TestCase rather than PHPUNIT_Framework_TestCase, and my tests are able to run as expected. However, this is just a circumvention and not a solution. (It is also strange to me that this works given the fact that TestCase itself does ultimately extend from PHPUNIT_Framework_TestCase.

Upvotes: 0

Views: 14962

Answers (2)

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

The message says PHPUNIT_Framework_TestCase, but the class is actually called PHPUnit_Framework_TestCase. Now here is the tricky thing

  • Class names in PHP are case insensitive, that's why it works when you run the whole test suite and the class is already loaded. BUT
  • The autoloader is case sensitive, because your file system is case sensitive and it cannot guess the correct case.

So when you run the test separately, the class PHPUNIT_Framework_TestCase is not loaded before your test case file is included and the autoloader will look for a file PHPUNIT/Framework/TestCase.php instead of PHPUnit/Framework/TestCase.php. This file does not exist and you receive the "class not found" error.

Solution

Replace

class TestThatFails extends PHPUNIT_Framework_TestCase

with

class TestThatFails extends PHPUnit_Framework_TestCase

Upvotes: 9

sitilge
sitilge

Reputation: 3737

PHPUnit loads the configuration file phpunit.xml (see here) that actually holds the info about the bootstrap file

...
bootstrap="tests/bootstrap.php"
...

It is the bootstrap.php that loads the autoloader thus making all of those classes available in your app.

require __DIR__.'/../vendor/autoload.php';

The autoload.php, probably, is not included in the testThatFails.php. The autoload php loads the PHPUNIT_Framework_TestCase along with other files/classes that may be relevant to this case (some other unit test classes, etc.). IMHO the best way, other than including it in the phpunit.xml, is to include the bootstrap file as an argument in command

phpunit --bootstrap bootstrap/autoload.php testThatFails.php

Upvotes: 2

Related Questions