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

Code igniter unittest-part1

on

  • 10,170 views

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

Statistics

Views

Total Views
10,170
Views on SlideShare
8,548
Embed Views
1,622

Actions

Likes
5
Downloads
87
Comments
4

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

Accessibility

Categories

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.

Cancel

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
    Processing…
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
      $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
    • Example CI Test
      http://ds.albert-rosa.com
    • Function code:
      $data['title'] = 'CI DEMO: CI Internal Unit Testing';
      $this->load->library('unit_test');
      $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
      <html>
      <head>
      <title><?= $title ?></title>
      </head>
      <body>
      <h1><?= $title ?></h1>
      <h2>Blog Titles</h2>
      <ul>
      <?phpforeach($blogTitles as $blog):?>
      <li><?= $blog->title ?></li>
      <? endforeach;?>
      </ul>
      <hr/>
      <h3>Unit Test</h3>
      <?= $testing ?>
      <hr/>
      </body>
      </html>
    • Toast Can Test You
      http://ds.albert-rosa/test/Blog
    • 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
    • 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() {}
    • 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');
      }
      }?>
    • 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
    • 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
      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/
      Me:
      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!..