Unit testing

I’m struggling to get unit testing to work.

Yii claims to have been built on top of PHPUnit, the base class CWebTestCase (/framework/test/CWebTestCase.php) contains an include of PHPUnit:


require_once('PHPUnit/Extensions/SeleniumTestCase.php');

But this file does not exists anywhere in the framework. I’m now trying to download and install PHPUnit manually, and putting it in the project under the vendors folder, but I’m not sure if this is the way to go, because I have to either change core files OR avoid Yii’s test classes.

What’s the best way to make PHPUnit work?

Install it either via yum/apt or directly with PEAR. You do not need to copy PHPUnit into your copy of Yii.

I wrote this a while back. If you’re using WAMP it might be helpful.

It might be worth checking out Netbeans. You can install PHPUnit within the IDE and invoke the tests from it. It’s much easier than doing it through command-prompt. If you do decide to check out Netbeans have a look at this.

Thanks, I’ve spent about 2,5 days so far trying to get unit testing running. I couldn’t get pear working correctly (seems not supported on windows 7). In the end I tried to install everything manually, but I ended up in a dependency hell, PHPUnit has all kind of inclusions that failed, I had to manually fix them. Finally this has been solved. Now Yii gives the error ‘CwebCaseTest not found’ (or something similar).

A lot of things are still not clear to me (after reading many docs/tutorials)

  • It seems there are 3 kinds of tests, unit tests (testing models), database tests (testing AR classes) and functional tests (testing the whole application by browser simulation). Is this correct?

  • What’s the best (most used) way of testing? Command line, IDE, or web? How do I unit test in the command line? How do I get a HTML report in the browser? Should I test locally, or on the server, or both? How to test modules, should every module have it’s own tests?

  • Yii is by default shipped with an index-test.php, the only thing it seems to do is include a separate config. Is this really useful and functional? Or am I supposed to setup something myself?

  • The functional tests seem to rely on Selenium Server, which uses a browser to emulate a user, these tests are useless when you’re working on a server without a desktop, right?

By the way, I’m now trying to setup XAMPP, which should have PHPUnit already included. I’ve moved away from Netbeans 2 years ago, I’m now using PHPStorm, which also has PHPUnit support, I will try to get that to work after commandline works.

can anyone …

Please share any unit testing examples … i am new to this unit testing … please share your unit testing code … in unit testing we will write code for both models and controllers or only models ? please share your code