Unit Testing

[size="5"][font="Arial Narrow"]Hello, somebody could provide help on how to perform unit tests.

I’ve read the tutorial, the subject of unit testing, but I can not understand.

Anyone have a clear example of how to do a unit test in Yii. Please I need your help.

Thank you.

I am testing some extensions.[/font][/size]

hello (szia)

if you are by any chance Hungarian, this might help a little: http://yiihun.blogspot.com/2010/09/kornyezetbarat-egysegteszteles.html


Thank you, but I’m in Puebla, México.


I only recently started using unit testing so I’m no expert, but here’s what I know…

  1. Make sure you have installed PHPUnit correctly for your environment: Linux/Windows, under NetBeans or remote server, etc. I use NetBeans for Windows but test/deploy on a remote CentOS 5 server. Also make sure that your application has been configured for unit testing as explained in the Yii Guide.

  2. Let’s say I have a simple Users model and I want to test one set of validation rules:

class Users extends CActiveRecord



    public function rules()


        return array(

            array('username', 'required', 'on'=>'insert', 'message'=>Yii::t('app','Campo es obligatorio.')),

            array('username', 'length', 'min'=>5, 'max'=>32, 'message'=>Yii::t('app','Debe tener entre 5 y 32 caracteres.')),

            array('username', 'match', 'pattern'=>'/^[\p{L}\p{N}_]+$/u', 'message'=>Yii::t('app','Alfanumérico y subrayado (_) solamente.')),

            array('username', 'filter', 'filter'=>'strtolower'),

            array('username', 'unique', 'message'=>Yii::t('app','Usuario previamente registrado - escoja otro diferente.')),





  1. Next, under protected/tests/unit I have UsersTest.php:

class UsersTest extends CDbTestCase



     * @dataProvider provider


    public function testValidate($a)


        $u = new Users;








    public function provider()


        return array(

            array(''),   // no username

            array('A'),   // username too short

            array('0123456789012345678901234567890123456789'),   // username too long

            array('A!B@C#D$'),   // username non alphanumeric

            array('admin'),   // username not unique 

            array('test_user'),   // pass test





  1. Finally run phpunit UsersTest.php and watch the results.

You can expand on this by testing your own functions (the example above is for CModel.validate(), a Yii function but it helped me find an error in the match regex), both for input and returned values. It can get way more sophisticated by using fixtures and other stuff, but I’m still learning it. I highly recommend reading the PHPUnit manual for more information.

Good luck.

Thank you JFReyes, do you have more informatión about unit testing with Yii?. Give me more information, please.

Like I said, I’m also getting started so I don’t know much. However, I looked up @imehesz’ suggested tutorial and translated it to English using Bing Translator (Google Translate couldn’t finish the page!?!?); it also translated it to Spanish. Anyway, it looks pretty good so I’ll follow it myself. Try it.