Unit Testing with CodeIgniter
Being a programmer at some point, you'll come to the situation of testing you code. However TDD is extreme programming way. Though most of the PHP programmers don't go the extreme programming. So Lets go through some unit testing. Here in this post, I am going to talk about CodeIgniter's unit_testing library. Then manual page is at http://ellislab.com/codeigniter/user-guide/libraries/unit_testing.html.
Here is a quick jump start code for unit testing:
You can save this code in a tester file in application/controllers directory. And on you browser you can type http://yoursite.com/tester or if index.php not removed then http://yoursite.com/index.php/tester. If you are on localhost then replace yoursite.com with localhost/your_ci_directory.
The output should be as follows:
Here in this example we have done two tests.
Test1:
Its the one that's in CodeIgniter's manual page, which tests, 1+1 equals 2.
Test2:
The other one is a test to make sure an empty arrays size is 0.
The main points to consider while writing unit test are:
1. Load the library: $this->load->library('unit_test');
2. Test to run : $this->unit->run($test, $expected_result, $test_name);
3. Print the result: echo $this->unit->report();
CodeIgniter community is on integrating PHPUnit which is not complete yet. However you can use, https://github.com/ericbarnes/codeigniter-simpletest/ too for testing which is a real good one to use. I will be posting on it in the coming posts.
Here is a quick jump start code for unit testing:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Tester extends CI_Controller {
public function index()
{
//Test 1
$this->load->library('unit_test');
$test = 1 + 1;
$expected_result = 2;
$test_name = 'Adds one plus one';
$this->unit->run($test, $expected_result, $test_name);
//Test 2
$a=array();
$this->unit->run(sizeof($a), 0, 'Empty array');
echo $this->unit->report();
}
}?>
You can save this code in a tester file in application/controllers directory. And on you browser you can type http://yoursite.com/tester or if index.php not removed then http://yoursite.com/index.php/tester. If you are on localhost then replace yoursite.com with localhost/your_ci_directory.
The output should be as follows:
Here in this example we have done two tests.
Test1:
Its the one that's in CodeIgniter's manual page, which tests, 1+1 equals 2.
Test2:
The other one is a test to make sure an empty arrays size is 0.
The main points to consider while writing unit test are:
1. Load the library: $this->load->library('unit_test');
2. Test to run : $this->unit->run($test, $expected_result, $test_name);
3. Print the result: echo $this->unit->report();
CodeIgniter community is on integrating PHPUnit which is not complete yet. However you can use, https://github.com/ericbarnes/codeigniter-simpletest/ too for testing which is a real good one to use. I will be posting on it in the coming posts.
This solves very little though... What most people want to do is inside a Test Class, create a new version of a controller (that has been written somewhere, following DRY) and inject test data into it to see what the result is.... at that point we can use the so called unit testing commands supplied?
ReplyDeleteWould agree with it. I would like to see some reasonable practical examples.
DeleteNice Information, Thanks, Pari
ReplyDelete