Webapp unit testing

Hello guys, I can't wait for 1.1 with test suite embedded.

But I need unit tests right now and I'm not sure how to correctly add tests to Yii

Does anybody using unit tests for their web applications now? How to implement them, where to store tests? how to test controllers and so on.

Any help would be appreciated.

By the way, I'm trying to write all my code grouped by modules, And would like to distribute modules with test only for that module…

PS. I'm going to use PHPUnit for testing.

In svn trunk, I already checked in the testing framework (based on phpunit). There is no documentation though (only API comments). You may use trunk to create a new webapp and see how the skeleton test scripts are generated.

Thanks,

I’ve checkedout 1.1 branch and generated webapp. And have questions :)

What is fixtures folder for?

How I should run tests? I'm not very familiar with phpunit, how can I use bootstrap.php file?

Can I create similar test folder in yii 1.0.x and run my tests from there?

fixtures stores table data initializing tables to be tested. You need to install phpunit first. bootstrap.php is a script that will be invoked by phpunit when you start a test. Unfortunately, the test framework requires yii 1.1 since it needs some special database support that is only available in 1.1.

I'm going to add some examples of using this framework and also add support to yiic so that it can generate test code.

Hello,

I just generate my webapp from YII in trunk.

I have phpunit installed by pear and working on eclipse.

I have my path to YII in my php Include Path.

I have some doubts about how to work with these tests.  I had defined my TEST_BASE_URL.

I input bootstrap file to phpunit and try to run SieTest.

Sometimes it brings me back an exception when it didnt finds WebTestCase File, and sometimes it shows me HTML index file.

I am newer and all help is welcome, thanks.

Did you include WebTestCase.php in bootstrap.php?

Displaying HTML index file is normal when a test method finishes.

Yes I did.

With HTML code it shows:

PHPUnit 3.3.17 by Sebastian Bergmann.

Class C:\path\to\app\index-test could not be found in C:\path\to\app\index-test.php.

I think its not running the tests

A different error message now? What is the complete error message?

First that I am getting now:

"Fatal error: Class 'WebTestCase' not found in C:app_pathprotectedtestsfunctionalSiteTest.php on line 2


PHP Fatal error:  Class 'WebTestCase' not found in C:app_pathprotectedtestsfunctionalSiteTest.php on line 2"

I'm confused. You said the following:

Class C:\path\to\app\index-test could not be found in C:\path\to\app\index-test.php.

Fatal error: Class 'WebTestCase' not found in C:\app_path\protected\tests\functional\SiteTest.php on line 2

Which one is true? What does your bootstrap.php look like?

Yes, I am really sorry but I am confused too.

My bootstrap file



<?php





// change the following paths if necessary


$yiit=dirname(__FILE__).'/../../YII_SVN/framework/yiit.php';


$config=dirname(__FILE__).'/../config/test.php';





require_once($yiit);


require_once(dirname(__FILE__).'/WebTestCase.php');





Yii::createWebApplication($config);

It looks like sometimes I got bootstrap initialized and sometimes not.

I get the error class not found message much more times.

I think I am not getting it to work because I dont know exactly how to configure phpunit properly in these files you had provided us.

Your bootstrap looks fine. How do you run the test? You should:



cd C:app_pathprotectedtests


phpunit functional/SiteTest.php


Ok. PHPUnit takes a long running.

6 errors

With error messages:

Could not connect to the Selenium RC Server

CWebTestCase.php (61)

Quote

I think I am not getting it to work because I dont know exactly how to configure phpunit properly in these files you had provided us.

DISCLAIMER: I’m writing a test framework for a CMS right now with PHPUnit, so I don’t know if this information is completely relevant (I haven’t used any for Yii). I was scratching my head for a while over ‘class not found’ sorts of things, and discovered that any files in my setup that I tested needed to have files included relative to where PHPUnit was being run from, not from where the file was.

Not sure if this is relevant to this situation, I don’t have time to learn the setup of the package, but there could be something similar going on in this situation  :-\ .

Quote

Ok. PHPUnit takes a long running and shows me letter E more times than the number of tests

'E' means there was an error running the test, usually because there was a PHP  error or a problem requiring a file (outside of Yii, at least). Not sure how you're having more errors than tests, unless a path is wrong and the test runners themselves are failing…

Quote

Ok. PHPUnit takes a long running.

6 errors

With error messages:

Could not connect to the Selenium RC Server

CWebTestCase.php (61)

You need to start Selenium RC server first before running functional tests.

BTW, did you read the documentation about testing under /docs/guide ?

Yeah. Qiang asked me to try it in cmd:



EEEEEE





Time: 06:00





There were 6 errors:





1) testIndex(SiteTest)


RuntimeException: Could not connect to the Selenium RC server.


C:DesenvolvimentoworkspacesphpYII_SVNframeworktestCWebTestCase.php:61





2) testContact(SiteTest)


RuntimeException: Could not connect to the Selenium RC server.


C:DesenvolvimentoworkspacesphpYII_SVNframeworktestCWebTestCase.php:61





3) testLoginLogout(SiteTest)


RuntimeException: Could not connect to the Selenium RC server.


C:DesenvolvimentoworkspacesphpYII_SVNframeworktestCWebTestCase.php:61





4) testIndex(SiteTest)


RuntimeException: Could not connect to the Selenium RC server.


C:DesenvolvimentoworkspacesphpYII_SVNframeworktestCWebTestCase.php:61





5) testContact(SiteTest)


RuntimeException: Could not connect to the Selenium RC server.


C:DesenvolvimentoworkspacesphpYII_SVNframeworktestCWebTestCase.php:61





6) testLoginLogout(SiteTest)


RuntimeException: Could not connect to the Selenium RC server.


C:DesenvolvimentoworkspacesphpYII_SVNframeworktestCWebTestCase.php:61





FAILURES!


Tests: 6, Assertions: 0, Errors: 6.


Qiang::I am getting started with PHPUnit and reading its guide. I will solve this issue with Selenium and came back with an answer :)

Thank you for all help.