How to include unit tests in Yii extensions?

None of the Yii extensions I used included any unit test.

I can hardly blame them because:

[list=3]

[*]the official guide does not mention tests on the page dedicated to "Creating extensions",

[*]the unit test system provided by Yii is meant to test an application. I could not find an "official" way to have tests within a Yii extension.

[*]The only forum post I found on this subject is unanswered.

[/list]

I think the documentation should be enhanced on this subject. It could increase the quality of the extensions.

Is there an official way to put unit tests in an extension? Or at least does anyone do it, and how?

François

Some extensions in YiiExt are tested but tests are not included in release zip-s.

If you’ll find a good way to store tests in extension directory, leave a note here.

To load Yii I had to use this ugly line:

In extensions/XXXX/tests/XXXTest.php:




// Expect yii/ to be next to "index.php"'s parent directory. Please adapt.

require_once dirname(dirname(__FILE__)) . '/../../../../yii/framework/yii.php';



AFAIK, the path to “yii.php” is only in “/index.php” and “/index-test.php”. But I can’t include these files because they would launch an application. Parsing them would be uglier.

To make things portable, Yii could have somewhere (e.g. in "/protected/extensions/") a file that would load "yii.php". Unfortunately, if the path to Yii was to change, the user would modify "/index.php", but probably not this new file. So, IMHO, the path to "yii.php" (or an include of it) should be in a separate file used by all ("/index.php" et al.).

Should I open an issue ticket on this?

It’s not a framework issue, I think.

I do really think that its a framework issue.

Maybe best option is that CWebTestCase looks if extensions/*/test exists, and then run Unit Tests on that folder.

Its that possible?