phpunit testing woes - Class 'CTestCase' not found

I’ve installed PHPUnit and trying to run a simple test to see if there is database connects


class DbTest extends CTestCase {


    public function testConnection() {

        $this->assertNotEquals(NULL, Yii::app()->db);

    }


}

when running


$ phpunit tests/unit



or


$ phpunit test



I get the following error:

PHP Fatal error: Class ‘CTestCase’ not found in tests/unit/DbTest.php on line 3

Not sure but in bootstrap.php when I provide wrong paths on purpose for yiit.php and config/test.php nothing happens (as in no errors come up?), is it possible that the boostrap.php in /protected/tests is not getting loaded somehow?

The importing of the CTestCase class file is certainly in the yiit file:


Yii::import('system.test.CTestCase');

Yii::import('system.test.CDbTestCase');

Yii::import('system.test.CWebTestCase');



so you must ensure the path to this file is correctly set in the bootstrap file. Also, make sure you have a phpunit.xml config file in your tests directory that specifes the proper bootstrap.php file for bootstrapping…something like:

[xml]<phpunit bootstrap="bootstrap.php"

	colors=&quot;false&quot;


	convertErrorsToExceptions=&quot;true&quot;


	convertNoticesToExceptions=&quot;true&quot;


	convertWarningsToExceptions=&quot;true&quot;


	stopOnFailure=&quot;false&quot;&gt;





&lt;selenium&gt;


	&lt;browser name=&quot;Firefox&quot; browser=&quot;*firefox&quot; /&gt;


&lt;/selenium&gt;

</phpunit>[/xml]

in this case, bootstrap.php and phpunit.xml should be in the same directory.

You can always put a little echo of something out in the bootstrap file and then run your test suite to ensure it is indeed being executed as you expect.

both the bootstrap.php and phpunit.xml files are in the ‘tests’ directory. I’ve commented out bootstrap.php and just added an echo to see if it loads, the echo doesn’t come up, only the same old “CTestCase” not found message. So I guess the bootstrap isn’t being loaded by phpunit. Could phpunit configuration be wrong somehow?

PHPUnit looks for phpunit.xml in the current working directory by default. It means that you should either launch phpunit from protected/tests or use --configuration (or --bootstrap) option.

Thank you! That’s exactly what it was, i need to be in the test/protected directory and


$ phpunit unit

works!