[eluser]redguy[/eluser]
I'm originally a Java developer, and there I use JUnit to run my test-cases. I couldn't do what I wanted to do with the Unit Testing class in CI, so I created a helper:
Code:
<?
function assert_true($assertion) {
if($assertion) {
return TRUE;
} else {
return FALSE;
}
}
function assert_false($assertion) {
if($assertion) {
return FALSE;
} else {
return TRUE;
}
}
function assert_equals($base, $check) {
if($base === $check) {
return TRUE;
} else {
return FALSE;
}
}
function assert_not_equals($base, $check) {
if($base !== $check) {
return TRUE;
} else {
return FALSE;
}
}
function assert_not_empty($assertion) {
if(!empty($assertion)) {
return TRUE;
} else {
return FALSE;
}
}
function assert_empty($assertion) {
if(empty($assertion)) {
return TRUE;
} else {
return FALSE;
}
}
function unit_test_report($data) {
$str = '<table border="1">';
$str .= '<tr><th>Test</th><th>Name</th><th>Result</th></tr>';
foreach($data as $key => $test){
$str .= '<tr><td>'.$key.'</td><td>'.$test['Test Name'].'</td>';
$str .= '<td style="background-color:'.($test['Result'] == 'Passed' ? '#00FF00' : '#FF0000').';">'.$test['Result'].'</td></tr>';
}
$str .= '</table>';
return $str;
}
?>
I call the following method from within an index() in a test controller (so I can access it with my browser):
Code:
function _test_categories_model() {
$this->load->model('Categories_model', 'categories');
$data1['name'] = 'foo';
$cid = $this->categories->insert_category($data1);
$this->unit->run(assert_not_empty($cid), TRUE, 'Add a category');
$this->unit->run(assert_not_empty($this->categories->get_category_by_id($cid)), TRUE, "Select category with cid [$cid]");
$data2['name'] = 'bar';
$this->unit->run(assert_not_empty($this->categories->update_category($cid, $data2)), TRUE, "Update category with cid [$cid]");
$this->unit->run(assert_not_empty($this->categories->get_all_categories()), TRUE, "Update category with cid [$cid]");
$this->unit->run(assert_not_empty($this->categories->remove_category($cid)), TRUE, "Removing category with cid [$cid]");
print unit_test_report($this->unit->result());
}
That gives me the following HTML:
Code:
<table border="1"><tr><th>Test</th><th>Name</th><th>Result</th></tr><tr><td>0</td><td>Add a category</td><td style="background-color:#00FF00;">Passed</td></tr><tr><td>1</td><td>Select category with cid [24]</td><td style="background-color:#00FF00;">Passed</td></tr><tr><td>2</td><td>Update category with cid [24]</td><td style="background-color:#00FF00;">Passed</td></tr><tr><td>3</td><td>Update category with cid [24]</td><td style="background-color:#00FF00;">Passed</td></tr><tr><td>4</td><td>Removing category with cid [24]</td><td style="background-color:#00FF00;">Passed</td></tr></table>
I'm thinking about of putting the $this->unit->result() of every test-case into 1 Array, so I can make my own test-suite. If I pass that test-suite to unit_test_report() I can (kind of) automate my unit testing.
By the way, I'm sure my PHP can be improved, I've just started a few months ago so I don't know every in and out of the language.