• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Code igniter unittest-part1

Code igniter unittest-part1



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

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



Total Views
Views on SlideShare
Embed Views



5 Embeds 1,622

http://blog.albert-rosa.com 1615
http://localhost 4
http://webcache.googleusercontent.com 1
http://translate.googleusercontent.com 1
https://www.linkedin.com 1



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.


14 of 4 previous next Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Code igniter unittest-part1 Code igniter unittest-part1 Presentation Transcript

    • CodeIgniter
      Unit Testing Part 1
    • Topic
      CodeIgniter’s Internal Unit Test
      Toast To Test CodeIgniter
    • Why Unit Test
      Consistent Validation of Code
      Decrease of Errors and Bugs on upgrade and refactoring
    • 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?
    • I can test myself!
      CodeIgniter has it’s own unit test library
      We load the library as any other library
      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
    • Example CI Test
    • Function code:
      $data['title'] = 'CI DEMO: CI Internal Unit Testing';
      $this->unit->run($data, array('title'=>'CI DEMO'), 'title');
      $this->unit->run(1, 2, 'Force Error');
      $data['blogTitles'] = $this->getData();
      $viewResult = $this->load->view('blogview', $data, true);
      $this->unit->run($this->viewTest($viewResult, "<title>CI DEMO: CI Internal Unit Testing</title>"), TRUE, 'Testing Tile node');
      $data['testing'] = $this->unit->report();
      $this->load->view('blogview', $data);
    • View Code
      <title><?= $title ?></title>
      <h1><?= $title ?></h1>
      <h2>Blog Titles</h2>
      <?phpforeach($blogTitles as $blog):?>
      <li><?= $blog->title ?></li>
      <? endforeach;?>
      <h3>Unit Test</h3>
      <?= $testing ?>
    • Toast Can Test You
    • 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/.
      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
    • Toast Test …
      require_once(APPPATH . '/controllers/test/Toast.php');
      class Example_tests extends Toast{
      function Example_tests(){
      // Load any models, libraries etc. you need here
      * 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() {}
    • 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->message = '$a || $b';
      public function test_model(){
      $result = $this->blogmodel->get_last_ten_entries();
      $this->_assert_equals($result[0]->title,'Hello World!', 'blog title first Record');
    • 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
      You can all go to
    • 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
    • What’s Coming with Unit Testing Part 2
      For the second part of this two part series we will talk about
      PHPUnit and CodeIgniter
      Ant scripting
      • Automating PHPUnit Test with Ant
    • Q & A
      Got a question…
      Got a real world problem…
      Got an imaginary world problem…
    • Links and Stuff
      CodeIgniter group: www.meetup.com/codeigniter
      Get CodeIgniter: http://www.codeigniter.com/download
      Get Toast: http://jensroland.com/projects/toast/
      Email: rosalbert@gmail.com
      Aim: albertrosa2000 …. (it was the future then and 2099 was taken … Marvel and their 2099 series ;-) )
    • 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.
    • Peace..
      We out!..