4. Why Unit Test Consistent Validation of Code Decrease of Errors and Bugs on upgrade and refactoring
5. What to consider when writing your Unit Test What am I testing? What should I be expecting to happen? Does this Test Fail? Does this Test cover everything in the class / function?
6. I can test myself! CodeIgniter has it’s own unit test library We load the library as any other library $this->load->library('unit_test'); We run a test with $this->unit->run( test, expected result, 'test name' ); We can get a report by $data['testing'] = $this->unit->report(); We can test Models, Controllers and our own custom functions and actions
11. Getting Toasty.. Toast is another unit testing suite for Code Igniter Specifically. It was designed to intergrade perfectly. Allowing more points of testing and a more defined way of testing closely related to jUnit. You can get more information at http://jensroland.com/projects/toast/. INSTRUCTIONS Download and unzip the files Create two new folders: * /app/controllers/test * /app/views/test Move the two controller files and the example test class to the first folder, and the three view files to the second
12. Toast Test … require_once(APPPATH . '/controllers/test/Toast.php'); class Example_tests extends Toast{ function Example_tests(){ parent::Toast(__FILE__); // Load any models, libraries etc. you need here $this->load->model('blogmodel'); } /** * OPTIONAL; Anything in this function will be run before each test * Good for doing cleanup: resetting sessions, renewing objects, etc. */ function _pre() {} /** * OPTIONAL; Anything in this function will be run after each test * I use it for setting $this->message = $this->My_model->getError(); */ function _post() {}
13. function test_simple_addition(){ $var = 2 + 2; $this->_assert_equals($var, 4); } function test_that_fails() { $a = true; $b = $a; $this->_assert_true($a); // true $this->_assert_false($b); // false $this->_assert_equals($a, $b); // true } function test_or_operator(){ $a = true; $b = false; $var = $a || $b; $this->_assert_true($var); $this->message = '$a || $b'; } public function test_model(){ $result = $this->blogmodel->get_last_ten_entries(); $this->_assert_not_empty($result); $this->_assert_equals($result[0]->title,'Hello World!', 'blog title first Record'); } }?>
14. Toast All Toast Test must be within the Test Folder with all the Toast files All Toast Test must include the Toast.php All Toast Test classes must extend Toast To Run and View the Toast Results, go to the test folder in the browser and go to the class you want to run http://www.example.com/test/class_toasted You can all go to http://www.example.com/test/toast_all
15. Things to Keep in mind A Test is only as good as you create it to be Try to think of every possible scenario The test must be able to fail Many testers find that the test is always passing and cannot make the test fail… there is a something wrong with the test. Don’t just test your work but test the framework too! Every framework has some pit fall or an area where the the contributors over looked… it happens .. Give back and submit your “bugs” and fail points
16.
17. Links and Stuff CodeIgniter group: www.meetup.com/codeigniter Get CodeIgniter: http://www.codeigniter.com/download Get Toast: http://jensroland.com/projects/toast/ Me: Email: rosalbert@gmail.com Aim: albertrosa2000 …. (it was the future then and 2099 was taken … Marvel and their 2099 series ;-) )
18. Finally Thanks Rokkan For the Space and time.. Visit them at www.rokkan.com Slides will be posted and shared Example application code will be posted and shared.