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

PPTX
CodeIgniter Ant Scripting
PPTX
Code Igniter Code Sniffer
PPT
Functional Testing Swing Applications with Frankenstein
PPTX
Automation testing with Drupal 8
PPT
JavaScript Unit Testing
PPTX
Setting UIAutomation free with Appium
PPT
JavaScript Unit Testing
PDF
Test all the things! Automated testing with Drupal 8
CodeIgniter Ant Scripting
Code Igniter Code Sniffer
Functional Testing Swing Applications with Frankenstein
Automation testing with Drupal 8
JavaScript Unit Testing
Setting UIAutomation free with Appium
JavaScript Unit Testing
Test all the things! Automated testing with Drupal 8

What's hot (20)

PDF
Testing Django Applications
PDF
Unit-testing and E2E testing in JS
PDF
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
PPTX
Test automation with php codeception
PDF
Unit testing for WordPress
PDF
Appium & Robot Framework
PPTX
Getting started with PHPUnit
PPTX
CI / CD w/ Codeception
PPTX
Test-Driven JavaScript Development (JavaZone 2010)
PDF
Effective testing with pytest
PPTX
Laravel Unit Testing
PPTX
Selenium Open Source Tool
PDF
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
PDF
Codeception: introduction to php testing
PDF
Testing with Codeception (Webelement #30)
PDF
Join the darkside: Selenium testing with Nightwatch.js
PDF
PHP-VCR behat case study
PPTX
Jasmine with JS-Test-Driver
PDF
Automated Web Testing using JavaScript
PDF
Intro to testing Javascript with jasmine
Testing Django Applications
Unit-testing and E2E testing in JS
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
Test automation with php codeception
Unit testing for WordPress
Appium & Robot Framework
Getting started with PHPUnit
CI / CD w/ Codeception
Test-Driven JavaScript Development (JavaZone 2010)
Effective testing with pytest
Laravel Unit Testing
Selenium Open Source Tool
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Codeception: introduction to php testing
Testing with Codeception (Webelement #30)
Join the darkside: Selenium testing with Nightwatch.js
PHP-VCR behat case study
Jasmine with JS-Test-Driver
Automated Web Testing using JavaScript
Intro to testing Javascript with jasmine
Ad

Similar to Code igniter unittest-part1 (20)

PPT
Unit testing
PDF
PHPunit and you
PDF
Create, test, secure, repeat
PPTX
Test in action week 2
PPTX
Test in action – week 1
PDF
Php tests tips
PPT
Automated Unit Testing
PDF
Leveling Up With Unit Testing - LonghornPHP 2022
KEY
PHPUnit testing to Zend_Test
ODP
Testing in Laravel
PPT
Unit testing php-unit - phing - selenium_v2
PDF
Unit testing in PHP
PPT
Test Driven Development with PHPUnit
KEY
Workshop quality assurance for php projects tek12
PPTX
Test in action week 4
PDF
Introduction to Unit Testing with PHPUnit
PDF
Fighting Fear-Driven-Development With PHPUnit
PDF
Unit Testing in SilverStripe
ODP
From typing the test to testing the type
KEY
CakePHP 2.0 - It'll rock your world
Unit testing
PHPunit and you
Create, test, secure, repeat
Test in action week 2
Test in action – week 1
Php tests tips
Automated Unit Testing
Leveling Up With Unit Testing - LonghornPHP 2022
PHPUnit testing to Zend_Test
Testing in Laravel
Unit testing php-unit - phing - selenium_v2
Unit testing in PHP
Test Driven Development with PHPUnit
Workshop quality assurance for php projects tek12
Test in action week 4
Introduction to Unit Testing with PHPUnit
Fighting Fear-Driven-Development With PHPUnit
Unit Testing in SilverStripe
From typing the test to testing the type
CakePHP 2.0 - It'll rock your world
Ad

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
sap open course for s4hana steps from ECC to s4
PDF
KodekX | Application Modernization Development
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Cloud computing and distributed systems.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Digital-Transformation-Roadmap-for-Companies.pptx
Electronic commerce courselecture one. Pdf
Encapsulation_ Review paper, used for researhc scholars
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
sap open course for s4hana steps from ECC to s4
KodekX | Application Modernization Development
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
The AUB Centre for AI in Media Proposal.docx
NewMind AI Weekly Chronicles - August'25 Week I
Cloud computing and distributed systems.
20250228 LYD VKU AI Blended-Learning.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Dropbox Q2 2025 Financial Results & Investor Presentation
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Review of recent advances in non-invasive hemoglobin estimation
The Rise and Fall of 3GPP – Time for a Sabbatical?
Per capita expenditure prediction using model stacking based on satellite ima...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Chapter 3 Spatial Domain Image Processing.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

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: rosalbert@gmail.comAim: 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.