Yii 1.1: unittestyiis

Unit Test Generator For Basic Model Crud Operations.

UnitTestYiis auto-generates unit tests for basic Create, Read, Update and Delete actions on a given Model.


Yii 1.4 or above. You must have a fixture for the given Model you wish to generate a test on.


  1. Place the `CrudUnit' directory into your extensions directory
  2. Place the file `CreateUnitTestCommand.php' into your 'commands/shell' directory.
  3. In your `config/test.php' add the import statement bellow


  1. Open your terminal and enter yiic shell
  2. Type createunittest
  3. You will be prompted to enter your Model name (example: `User').
  4. Enter the model name and hit enter.
  5. You will be prompted to enter the name of your fixture file without the .php' (example:users').
  6. Enter the fixture file name and hit enter.
  7. You will be prompted to enter your fixture key prefix (Example `user_' as in ('user_1'=>array('id'=>1, etc..), user_2=>array('id'=>2, ...), ...).
  8. Enter the fixture key prefix and hit enter
  9. Yo will be asked "Are there attributes of '$modelName' that are contained in fixture '$fixtureName' modified by beforeSave or other action(s)? [Yes|No]:"
  10. Enter 'yes' or 'no' and hit enter.
  11. Run your tests!

Update 1.3



  1. Added more detailed messages on test failures
  2. Added a status flag for turning off data consistency checks on inserts and updates. Useful when data is modified by methods like beforeSave().
  3. Everyone should download the latest version.


Total 4 comments

#10768 report it
gaurish at 2012/11/22 07:26am
createunittest command working on wamp...

I tried to implement this on wamp env. .. c:/wamp/www/yiidemo/yiic shell createunittest but it give error as , " createunittest doesnot exist or is not an entry script file"....

#9895 report it
Zugluk at 2012/09/19 12:19pm
Fatal error

I get a fatal error running test, class 'CrudBase' not found... though I well wrote the import array in my config/test.php... Did I miss something ?


Ok it's because when you run your test, you need to be in protected/test directory... you cannot launch your tests from protected/ for example... weird...

#4615 report it
evan108108 at 2011/07/26 02:14pm
Re: assertion messages

Great suggestion.

I have updated UnitTestYiis to include appropriate assertion messages.

Thanks redguy!

#4609 report it
Maciej Liżewski at 2011/07/26 05:04am
assertion messages

Please provide more informative messages to your assertions. You can pass them as parameter to assert functions like this:

$this->assertTrue( $a == $b, 'A is not equal B' );

and they will be used when such assertion fails providing more info about what was tested and failed...

Leave a comment

Please to leave your comment.

Create extension