Welcome Guest, Not a member yet? Register   Sign In
A Decent Unit Test Template
#1

[eluser]Kyle Johnson[/eluser]
Here is a decent landscape oriented unit-test template that I've been using. I've split off the individual tests into various other functions for my own sanity.

I'd love a better way to formulate the actual tests to have the name be equal to the test function, but that's not a big deal at this point.

Specifying set_test_items requires a super simple fix (https://github.com/EllisLab/CodeIgniter/issues/395)

This goes in my debug.php controller. http://localhost/index.php/debug/unit_tests
Code:
public function unit_tests() {
  $this->output->enable_profiler(FALSE);
  $this->load->model(array('product_model','inventory_model', 'inventory_item_model'));
  $this->load->library('unit_test');
  $this->unit->use_strict(TRUE);
  $this->unit->set_test_items(array('test_name','test_datatype','res_datatype', 'result','line'));
  $str = '<tr>
  {rows}
  <td>
  {result}
  </td>
  {/rows}
  </tr>';
  $this->unit->set_template($str);
  
  $this->_run_unit_tests();
}

private function _run_unit_tests() {
  echo '<table>';
  echo '<tr><th width="45%">Test Name</th><th>Test Type</th><th>Result Type</th><th width="15%">Result</th><th>Line</th></tr>';
  
  $this->_unit_test_inventory();
  $this->_unit_test_permissions();
  
  echo $this->unit->report();
  echo '</table>';
}
private function _unit_test_inventory() {
  $test = $this->product_model->is_serialized(5944);
  $expected = TRUE;
  $name = '$this->product_model->is_serialized(5944);';
  $this->unit->run($test,$expected,$name);
}

private function _unit_test_permissions() {
  $test = $this->acl->is_logged_in();
  $expected = TRUE;
  $name = '$this->acl->is_logged_in();';
  echo $this->unit->run($test,$expected,$name);
  
  $test = $this->acl->user_has_permission("employee");
  $expected = TRUE;
  $name = '$this->acl->user_has_permission("employee");';
  $this->unit->run($test,$expected,$name);
}

I tried to attach a screenshot, but was unable to at this time.


Messages In This Thread
A Decent Unit Test Template - by El Forum - 10-03-2012, 03:19 PM



Theme © iAndrew 2016 - Forum software by © MyBB