How to re enable logging during unit testing

  1. Introduction
  2. Bootstrap file
  3. Config file
  4. To the Point.

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.

11 0
10 followers
Viewed: 17 718 times
Version: 1.1
Category: How-tos
Written by: Asgaroth
Last updated by: Asgaroth
Created on: Nov 14, 2011
Last updated: 6 years ago
Update Article

Revisions

View all history

Related Articles