SlideShare a Scribd company logo
CodeIgniterUnit Testing Part 1
TopicCodeIgniter’s Internal Unit TestToast To Test CodeIgniter
Code igniter unittest-part1
Why Unit TestConsistent Validation of CodeDecrease of Errors and Bugs on upgrade and refactoring
What to consider when writing your Unit TestWhat 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 libraryWe 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 Testhttp://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 Youhttp://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/.INSTRUCTIONSDownload and unzip the filesCreate two new folders:* /app/controllers/test* /app/views/testMove 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');}		}?>
ToastAll Toast Test must be within the Test Folder with all the Toast filesAll Toast Test must  include the Toast.phpAll Toast Test classes must extend ToastTo Run and View the Toast Results, go to the test folder in the browser and go to the class you want to runhttp://www.example.com/test/class_toastedYou can all go to http://www.example.com/test/toast_all
Things to Keep in mindA Test is only as good as you create it to beTry to think of every possible scenarioThe test must be able to failMany 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 aboutPHPUnitPHPUnit and CodeIgniterAnt scriptingAutomating PHPUnit Test with Ant Q & AGot a question…Got a real world problem…Got an imaginary world problem…
Links and StuffCodeIgniter group: www.meetup.com/codeigniterGet CodeIgniter: http://www.codeigniter.com/downloadGet  Toast: http://jensroland.com/projects/toast/Me:Email: rosalbert@gmail.comAim: albertrosa2000 …. (it was the future then and 2099 was  taken … Marvel and their 2099 series ;-) )
FinallyThanks Rokkan For the Space and time.. Visit them at www.rokkan.comSlides will be posted and sharedExample application code will be posted and shared.
Peace.. We out!..
Code igniter unittest-part1

More Related Content

What's hot (20)

PDF
Testing Django Applications
Honza Král
 
PDF
Unit-testing and E2E testing in JS
Michael Haberman
 
PDF
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
Cogapp
 
PPTX
Test automation with php codeception
buddhieash
 
PDF
Unit testing for WordPress
Harshad Mane
 
PDF
Appium & Robot Framework
Furkan Ertürk
 
PPTX
Getting started with PHPUnit
Khyati Gala
 
PPTX
CI / CD w/ Codeception
Tudor Barbu
 
PPTX
Test-Driven JavaScript Development (JavaZone 2010)
Christian Johansen
 
PDF
Effective testing with pytest
Hector Canto
 
PPTX
Laravel Unit Testing
Dr. Syed Hassan Amin
 
PPTX
Selenium Open Source Tool
onlinemindq
 
PDF
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Ondřej Machulda
 
PDF
Codeception: introduction to php testing
Engineor
 
PDF
Testing with Codeception (Webelement #30)
Adam Štipák
 
PDF
Join the darkside: Selenium testing with Nightwatch.js
Seth McLaughlin
 
PDF
PHP-VCR behat case study
Pascal Thormeier
 
PPTX
Jasmine with JS-Test-Driver
Devesh Chanchlani
 
PDF
Automated Web Testing using JavaScript
Simon Guest
 
PDF
Intro to testing Javascript with jasmine
Timothy Oxley
 
Testing Django Applications
Honza Král
 
Unit-testing and E2E testing in JS
Michael Haberman
 
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
Cogapp
 
Test automation with php codeception
buddhieash
 
Unit testing for WordPress
Harshad Mane
 
Appium & Robot Framework
Furkan Ertürk
 
Getting started with PHPUnit
Khyati Gala
 
CI / CD w/ Codeception
Tudor Barbu
 
Test-Driven JavaScript Development (JavaZone 2010)
Christian Johansen
 
Effective testing with pytest
Hector Canto
 
Laravel Unit Testing
Dr. Syed Hassan Amin
 
Selenium Open Source Tool
onlinemindq
 
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Ondřej Machulda
 
Codeception: introduction to php testing
Engineor
 
Testing with Codeception (Webelement #30)
Adam Štipák
 
Join the darkside: Selenium testing with Nightwatch.js
Seth McLaughlin
 
PHP-VCR behat case study
Pascal Thormeier
 
Jasmine with JS-Test-Driver
Devesh Chanchlani
 
Automated Web Testing using JavaScript
Simon Guest
 
Intro to testing Javascript with jasmine
Timothy Oxley
 

Similar to Code igniter unittest-part1 (20)

PPT
Unit testing
davidahaskins
 
PDF
PHPunit and you
markstory
 
PDF
Create, test, secure, repeat
Michelangelo van Dam
 
PPTX
Test in action week 2
Yi-Huan Chan
 
PPTX
Test in action – week 1
Yi-Huan Chan
 
PDF
Php tests tips
Damian Sromek
 
PPT
Automated Unit Testing
Mike Lively
 
PDF
Leveling Up With Unit Testing - LonghornPHP 2022
Mark Niebergall
 
KEY
PHPUnit testing to Zend_Test
Michelangelo van Dam
 
ODP
Testing in Laravel
Ahmed Yahia
 
PPT
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
PDF
Unit testing in PHP
Lee Boynton
 
PPT
Test Driven Development with PHPUnit
Mindfire Solutions
 
KEY
Workshop quality assurance for php projects tek12
Michelangelo van Dam
 
PPTX
Test in action week 4
Yi-Huan Chan
 
PDF
Introduction to Unit Testing with PHPUnit
Michelangelo van Dam
 
PDF
Fighting Fear-Driven-Development With PHPUnit
James Fuller
 
PDF
Unit Testing in SilverStripe
Ingo Schommer
 
ODP
From typing the test to testing the type
Wim Godden
 
KEY
CakePHP 2.0 - It'll rock your world
Graham Weldon
 
Unit testing
davidahaskins
 
PHPunit and you
markstory
 
Create, test, secure, repeat
Michelangelo van Dam
 
Test in action week 2
Yi-Huan Chan
 
Test in action – week 1
Yi-Huan Chan
 
Php tests tips
Damian Sromek
 
Automated Unit Testing
Mike Lively
 
Leveling Up With Unit Testing - LonghornPHP 2022
Mark Niebergall
 
PHPUnit testing to Zend_Test
Michelangelo van Dam
 
Testing in Laravel
Ahmed Yahia
 
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
Unit testing in PHP
Lee Boynton
 
Test Driven Development with PHPUnit
Mindfire Solutions
 
Workshop quality assurance for php projects tek12
Michelangelo van Dam
 
Test in action week 4
Yi-Huan Chan
 
Introduction to Unit Testing with PHPUnit
Michelangelo van Dam
 
Fighting Fear-Driven-Development With PHPUnit
James Fuller
 
Unit Testing in SilverStripe
Ingo Schommer
 
From typing the test to testing the type
Wim Godden
 
CakePHP 2.0 - It'll rock your world
Graham Weldon
 
Ad

Recently uploaded (20)

PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
July Patch Tuesday
Ivanti
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
July Patch Tuesday
Ivanti
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Ad

Code igniter unittest-part1

  • 2. TopicCodeIgniter’s Internal Unit TestToast To Test CodeIgniter
  • 4. Why Unit TestConsistent Validation of CodeDecrease of Errors and Bugs on upgrade and refactoring
  • 5. What to consider when writing your Unit TestWhat 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 libraryWe 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
  • 8. 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);
  • 9. 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>
  • 10. Toast Can Test Youhttp://ds.albert-rosa/test/Blog
  • 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/.INSTRUCTIONSDownload and unzip the filesCreate two new folders:* /app/controllers/test* /app/views/testMove 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. ToastAll Toast Test must be within the Test Folder with all the Toast filesAll Toast Test must include the Toast.phpAll Toast Test classes must extend ToastTo Run and View the Toast Results, go to the test folder in the browser and go to the class you want to runhttp://www.example.com/test/class_toastedYou can all go to http://www.example.com/test/toast_all
  • 15. Things to Keep in mindA Test is only as good as you create it to beTry to think of every possible scenarioThe test must be able to failMany 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. What’s Coming with Unit Testing Part 2 For the second part of this two part series we will talk aboutPHPUnitPHPUnit and CodeIgniterAnt scriptingAutomating PHPUnit Test with Ant Q & AGot a question…Got a real world problem…Got an imaginary world problem…
  • 17. Links and StuffCodeIgniter group: www.meetup.com/codeigniterGet CodeIgniter: http://www.codeigniter.com/downloadGet Toast: http://jensroland.com/projects/toast/Me:Email: [email protected]: albertrosa2000 …. (it was the future then and 2099 was taken … Marvel and their 2099 series ;-) )
  • 18. FinallyThanks Rokkan For the Space and time.. Visit them at www.rokkan.comSlides will be posted and sharedExample application code will be posted and shared.