you want to be very careful about using if(TEST) in your code, the purpose of unit tests is to test the actual code that will be used for a specific function/class.
Doing it the way you are, only ensures that the code works in test mode, not in real mode - which defies the point.
If you follow the paradigm that each functional class should have its own set of unit tests, then mock the other classes that it interacts with, rather than doing specific actions when in test mode.
For example, if you are testing the functionality of your model class, and you dont want to save data to a database, you can instead mock the database connection with one that does not actually store any data.
That being said, if you note your config folder, you will likely see that you have a main.php and a test.php. What you can do is define a variable "is_test" as follows:
'params'=>array(
'is_test'=>false
)
you put that in your main.php, and in your test.php you set it to true. Then you can check Yii::app()->params["is_test"]