Introduction ¶
Some of you have experienced the need to log some information while doing unit testing, due to the fact that the information given in the summary is just not enough, to know why the test is actually failing. this article focuses on 2 ways to get you logs up and running again.
Bootstrap file ¶
The basic bootstrap.php file looks like this.
<?php
// change the following paths if necessary
$yiit=dirname(__FILE__).'/../../../framework/yii/1.1.8/framework/yiit.php';
$config=dirname(__FILE__).'/../config/test.php';
require_once($yiit);
require_once(dirname(__FILE__).'/WebTestCase.php');
Yii::createWebApplication($config);
In order to enable trace logging, you need to tell the application that it will run in debug mode. so you need to add the following 2 lines (borrowed from index.php)
defined('YII_DEBUG') or define('YII_DEBUG',true);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 3);
Config file ¶
Of course... if you want logging you need to enable it in the config file, by default bootstrap.php uses /protected/config/test.php which by default merges the main.php config file and overwrites as necessary:
return CMap::mergeArray(
require(dirname(__FILE__).'/main.php'),
array(
You can either enable your logs in main.php or test.php
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning, trace, info',
'categories'=>'application.*',
),
),
),
To the Point. ¶
This was just the basic set-up to enable logging on any web/console application, but what about unit testing?
Since there is no actual 'request' when running tests, the onEndRequest event is never raised. so our logs never get written.
To reenable logging you have 2 options:
Enable Real Time Logging: ¶
Avaliable since 1.1.8
To enable real time logging in your application (web/console/test) just add the following lines, in this case ill add them in the boostrap.php
// automatically send every new message to available log routes
Yii::getLogger()->autoFlush = 1;
// when sending a message to log routes, also notify them to dump the message
// into the corresponding persistent storage (e.g. DB, email)
Yii::getLogger()->autoDump = true;
Your bootstrap.php now should look like this:
<?php
// change the following paths if necessary
$yiit=dirname(__FILE__).'/../../../framework/yii/1.1.8/framework/yiit.php';
$config=dirname(__FILE__).'/../config/test.php';
defined('YII_DEBUG') or define('YII_DEBUG',true);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 3);
require_once($yiit);
require_once(dirname(__FILE__).'/WebTestCase.php');
Yii::createWebApplication($config);
// automatically send every new message to available log routes
Yii::getLogger()->autoFlush = 1;
// when sending a message to log routes, also notify them to dump the message
// into the corresponding persistent storage (e.g. DB, email)
Yii::getLogger()->autoDump = true;
This way you get your logging in real time and don't even have to wait until the test finishes.
Raise the onEndRequest event ¶
Another way around, if you can't use real time logging (for whatever reason that might be) is to raise the onEndRequest after the tests. to do that, add the following function to each of your test files.
public static function tearDownAfterClass()
{
Yii::app()->onEndRequest(new CEvent(null));
}
This will raise the event and the application will write the logs to our file.
Thanks a lot
Use it all the time.
Thanks for sharing :)
Saved my day
At least, I can have logs under phpunit testing. And moreover, I understand why I didn't have them.
Thanks a lots, you saved my day !
You are "The Gay"
Thank you very much!
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.