CodeIgniter<br />Unit Testing Part 1<br />
Topic<br />CodeIgniter’s Internal Unit Test<br />Toast To Test CodeIgniter<br />
Why Unit Test<br />Consistent Validation of Code<br />Decrease of Errors and Bugs on upgrade and refactoring<br />
What to consider when writing your Unit Test<br />What am I testing?<br />What should I be expecting to happen?<br />Does ...
I can test myself!<br />CodeIgniter has it’s own unit test library<br />We load the library as any other library<br />$thi...
Example CI Test<br />http://ds.albert-rosa.com<br />
Function code:<br />	$data['title'] = 'CI DEMO: CI Internal Unit Testing';<br />	$this->load->library('unit_test');<br />	...
View Code<br /><html><br /><head><br />		<title><?= $title ?></title><br /></head><br /><body>	<br />		<h1><?= $title ?></...
Toast Can Test You<br />http://ds.albert-rosa/test/Blog<br />
Getting Toasty..<br />Toast is another unit testing suite for Code Igniter Specifically. It was designed to intergrade per...
Toast Test …<br />require_once(APPPATH . '/controllers/test/Toast.php');<br />class Example_tests extends Toast{<br />	fun...
function test_simple_addition(){<br />	$var = 2 + 2;<br />$this->_assert_equals($var, 4);<br />}<br />function test_that_f...
Toast<br />All Toast Test must be within the Test Folder with all the Toast files<br />All Toast Test must  include the To...
Things to Keep in mind<br />A Test is only as good as you create it to be<br />Try to think of every possible scenario<br ...
What’s Coming with Unit Testing Part 2 <br />For the second part of this two part series we will talk about<br />PHPUnit<b...
Links and Stuff<br />CodeIgniter group: www.meetup.com/codeigniter<br />Get CodeIgniter: http://www.codeigniter.com/downlo...
Finally<br />Thanks Rokkan For the Space and time.. Visit them at www.rokkan.com<br />Slides will be posted and shared<br ...
Peace.. <br />We out!.. <br />
Code igniter unittest-part1
Upcoming SlideShare
Loading in …5
×

Code igniter unittest-part1

12,750 views

Published on

This is a slide show of a three part series for Unit testing and CodeIgniter. In this Presentation we have codeIgniters built in unit test, and

Published in: Technology
4 Comments
5 Likes
Statistics
Notes
No Downloads
Views
Total views
12,750
On SlideShare
0
From Embeds
0
Number of Embeds
1,657
Actions
Shares
0
Downloads
117
Comments
4
Likes
5
Embeds 0
No embeds

No notes for slide

Code igniter unittest-part1

  1. 1. CodeIgniter<br />Unit Testing Part 1<br />
  2. 2. Topic<br />CodeIgniter’s Internal Unit Test<br />Toast To Test CodeIgniter<br />
  3. 3.
  4. 4. Why Unit Test<br />Consistent Validation of Code<br />Decrease of Errors and Bugs on upgrade and refactoring<br />
  5. 5. What to consider when writing your Unit Test<br />What am I testing?<br />What should I be expecting to happen?<br />Does this Test Fail?<br />Does this Test cover everything in the class / function?<br />
  6. 6. I can test myself!<br />CodeIgniter has it’s own unit test library<br />We load the library as any other library<br />$this->load->library('unit_test');<br />We run a test with<br />$this->unit->run( test, expected result, 'test name' );<br />We can get a report by<br />$data['testing'] = $this->unit->report();<br />We can test Models, Controllers and our own custom functions and actions<br />
  7. 7. Example CI Test<br />http://ds.albert-rosa.com<br />
  8. 8. Function code:<br /> $data['title'] = 'CI DEMO: CI Internal Unit Testing';<br /> $this->load->library('unit_test');<br /> $this->unit->run($data, array('title'=>'CI DEMO'), 'title');<br /> $this->unit->run(1, 2, 'Force Error');<br /> $data['blogTitles'] = $this->getData();<br /> $viewResult = $this->load->view('blogview', $data, true);<br /> $this->unit->run($this->viewTest($viewResult, "<title>CI DEMO: CI Internal Unit Testing</title>"), TRUE, 'Testing Tile node');<br />$data['testing'] = $this->unit->report();<br /> $this->load->view('blogview', $data);<br />
  9. 9. View Code<br /><html><br /><head><br /> <title><?= $title ?></title><br /></head><br /><body> <br /> <h1><?= $title ?></h1><br /> <h2>Blog Titles</h2><br /> <ul><br /> <?phpforeach($blogTitles as $blog):?><br /> <li><?= $blog->title ?></li><br /> <? endforeach;?><br /> </ul><br /> <hr/><br /> <h3>Unit Test</h3><br /> <?= $testing ?><br /> <hr/><br /></body><br /></html><br />
  10. 10. Toast Can Test You<br />http://ds.albert-rosa/test/Blog<br />
  11. 11. Getting Toasty..<br />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.<br />You can get more information at http://jensroland.com/projects/toast/.<br />INSTRUCTIONS<br />Download and unzip the files<br />Create two new folders:<br />* /app/controllers/test<br />* /app/views/test<br />Move the two controller files and the example test class to the first folder, and the three view files to the second<br />
  12. 12. Toast Test …<br />require_once(APPPATH . '/controllers/test/Toast.php');<br />class Example_tests extends Toast{<br /> function Example_tests(){<br />parent::Toast(__FILE__);<br /> // Load any models, libraries etc. you need here <br /> $this->load->model('blogmodel');<br /> }<br /> /**<br /> * OPTIONAL; Anything in this function will be run before each test<br /> * Good for doing cleanup: resetting sessions, renewing objects, etc.<br /> */<br /> function _pre() {}<br /> /**<br /> * OPTIONAL; Anything in this function will be run after each test<br /> * I use it for setting $this->message = $this->My_model->getError();<br /> */<br /> function _post() {}<br />
  13. 13. function test_simple_addition(){<br /> $var = 2 + 2;<br />$this->_assert_equals($var, 4);<br />}<br />function test_that_fails() {<br />$a = true;<br />$b = $a;<br />$this->_assert_true($a); // true<br />$this->_assert_false($b); // false<br />$this->_assert_equals($a, $b); // true <br />}<br />function test_or_operator(){<br />$a = true;<br />$b = false;<br />$var = $a || $b;<br />$this->_assert_true($var);<br />$this->message = '$a || $b';<br />}<br />public function test_model(){<br /> $result = $this->blogmodel->get_last_ten_entries();<br /> $this->_assert_not_empty($result);<br /> $this->_assert_equals($result[0]->title,'Hello World!', 'blog title first Record');<br />} <br />}?><br />
  14. 14. Toast<br />All Toast Test must be within the Test Folder with all the Toast files<br />All Toast Test must include the Toast.php<br />All Toast Test classes must extend Toast<br />To Run and View the Toast Results, go to the test folder in the browser and go to the class you want to run<br />http://www.example.com/test/class_toasted<br />You can all go to <br />http://www.example.com/test/toast_all<br />
  15. 15. Things to Keep in mind<br />A Test is only as good as you create it to be<br />Try to think of every possible scenario<br />The test must be able to fail<br />Many testers find that the test is always passing and cannot make the test fail… there is a something wrong with the test.<br />Don’t just test your work but test the framework too!<br />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<br />
  16. 16. What’s Coming with Unit Testing Part 2 <br />For the second part of this two part series we will talk about<br />PHPUnit<br />PHPUnit and CodeIgniter<br />Ant scripting<br /><ul><li>Automating PHPUnit Test with Ant </li></li></ul><li>Q & A<br />Got a question…<br />Got a real world problem…<br />Got an imaginary world problem…<br />
  17. 17. Links and Stuff<br />CodeIgniter group: www.meetup.com/codeigniter<br />Get CodeIgniter: http://www.codeigniter.com/download<br />Get Toast: http://jensroland.com/projects/toast/<br />Me:<br />Email: rosalbert@gmail.com<br />Aim: albertrosa2000 …. (it was the future then and 2099 was taken … Marvel and their 2099 series ;-) )<br />
  18. 18. Finally<br />Thanks Rokkan For the Space and time.. Visit them at www.rokkan.com<br />Slides will be posted and shared<br />Example application code will be posted and shared.<br />
  19. 19. Peace.. <br />We out!.. <br />

×