SlideShare uma empresa Scribd logo
1 de 20
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 ,[object Object],[object Object]
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!..
Code igniter unittest-part1

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

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

Semelhante a Code igniter unittest-part1

Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
Priya Sharma
 

Semelhante a Code igniter unittest-part1 (20)

Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
J Unit
J UnitJ Unit
J Unit
 
Unit Testing in iOS
Unit Testing in iOSUnit Testing in iOS
Unit Testing in iOS
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Google test training
Google test trainingGoogle test training
Google test training
 
Testing in Craft CMS
Testing in Craft CMSTesting in Craft CMS
Testing in Craft CMS
 
Dusan Lukic Magento 2 Integration Tests Meet Magento Serbia 2016
Dusan Lukic Magento 2 Integration Tests Meet Magento Serbia 2016Dusan Lukic Magento 2 Integration Tests Meet Magento Serbia 2016
Dusan Lukic Magento 2 Integration Tests Meet Magento Serbia 2016
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
 
Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Creating testing tools to support development
Creating testing tools to support developmentCreating testing tools to support development
Creating testing tools to support development
 
Mockito with a hint of PowerMock
Mockito with a hint of PowerMockMockito with a hint of PowerMock
Mockito with a hint of PowerMock
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
DotNet unit testing training
DotNet unit testing trainingDotNet unit testing training
DotNet unit testing training
 

Último

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Último (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

Code igniter unittest-part1

  • 2. Topic CodeIgniter’s Internal Unit Test Toast To Test CodeIgniter
  • 3.
  • 4. Why Unit Test Consistent Validation of Code Decrease of Errors and Bugs on upgrade and refactoring
  • 5. 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?
  • 6. 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
  • 7. Example CI Test http://ds.albert-rosa.com
  • 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 You http://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/. 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
  • 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. 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
  • 15. 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
  • 16.
  • 17. 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 ;-) )
  • 18. 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.
  • 19. Peace.. We out!..