Tuesday, June 08, 2010

CodeIgniter - Doctrine unit testing - starter features




So to expand on the previous post... specifically to describe a few of the extra bells and whistles in the project starter....


Unit Testing
I have created a (very) simple extension to the codeigniter unit testing as I wanted to have a simple, web based interface to unit tests. I found the whole CodeIgniter terminology used in unit testing a little confusing (coming from a NUnit / JUnit testing background). Unless I missed something in the doc (which is very possible), CodeIgniter supports test assertions ($controller->unit->run when the test helper is loaded), there was no way to group a number of assertions together into a logical unit of testing.

I'm going to use a little NUnit / JUnit terminology to describe unit testing via the web page interface in the starter. In the ApplicationCode/libraries folder I have a base_test_controller which when extended replicates basic TestFixture functionality. To create a Unit Test within the fixture, simply create a function prefixed with the name test_. The following demonstrates using a controller as a test fixture:



//
// My Test Fixture
//
class MyTest extends base_test_controller {
//
// This method is executed before any test methods are run
//
function fixture_setup(){}
//
// This method is executed after all test methods are run
//
function fixture_tear_down(){}
//
// This method is executed before each test method is run
//
function unit_setup(){}
//
// This method is executed after each test method is run
//
function unit_tear_down(){}

//
// Your Unit Test
//
function test_My_unit_test()
{
$this->test_description = "This is a description of my unit test";

$this->unit->run(true, true, "Assertion one has passed");
$this->unit->run(true, true, "Assertion two has passed");
}
}




No comments: