SlideShare uma empresa Scribd logo
1 de 56
Unit Testing How to make your code rock Mike Pearce
Introduction to unit testing
Toes Toes
He madePHPUnit Sebastian Bergmann
History
? Why use it
$confidence++
Living Tests
Onto the CODE
%> phpunit --skeleton-test
%> phpunitunittests
%> phpunitunittests
%> phpunitunittests
Other test methods
stubs
mock objects
Not a database table
Extending
xinc
broken build
Test harness
test suite
selenium
Automated Testing Robot
Hi! Automated Testing Robot
and now... TDD!
sdrawkcaB
Remember this? %> phpunit --skeleton-test
%> phpunit --skeleton-class
and now... BDD!
it’s NOT testing...
Huh?
So if it's not about testing, what's it about? It's about figuring out what you are trying to do before you run off half-cocked to try to do it. You write a specification that nails down a small aspect of behaviour in a concise, unambiguous, and executable form. Dave Astel
www.phpunit.de/manual/current/en/behaviour-driven-development.html#behaviour-driven-development.bowlinggame-example
questions?
more info... http://en.wikipedia.org/wiki/Portal:Software_Testing http://phpunit.de http://en.wikipedia.org/wiki/PHPUnit http://www.ohloh.net/p/phpunit_pocket_guide http://www.youtube.com/watch?v=28KGs9c0bSw (Part 1,2...4) This presentation on Slideshare.net:
flickr attributions * hiro008 tm-tm malingering ...someonestolemyname EricNixon Hryck. Balakov Adam Foster | Codefor Zen Sebastian Bergmann FlySi ....Tim

Mais conteúdo relacionado

Semelhante a Introduction to Unit Testing

Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit TestingMike Lively
 
The state of PHPUnit
The state of PHPUnitThe state of PHPUnit
The state of PHPUnitEdorian
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testingSteven Casey
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentjakubkoci
 
Automated Frontend Testing
Automated Frontend TestingAutomated Frontend Testing
Automated Frontend TestingNeil Crosby
 
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...mCloud
 
The State of PHPUnit
The State of PHPUnitThe State of PHPUnit
The State of PHPUnitEdorian
 
The State of PHPUnit
The State of PHPUnitThe State of PHPUnit
The State of PHPUnitEdorian
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven DevelopmentPablo Villar
 
Amplify your stack - Jsfoo pune 2012
Amplify your stack - Jsfoo pune 2012Amplify your stack - Jsfoo pune 2012
Amplify your stack - Jsfoo pune 2012threepointone
 
The Art Of Performance Tuning - with presenter notes!
The Art Of Performance Tuning - with presenter notes!The Art Of Performance Tuning - with presenter notes!
The Art Of Performance Tuning - with presenter notes!Jonathan Ross
 
Practical unit testing 2014
Practical unit testing 2014Practical unit testing 2014
Practical unit testing 2014Andrew Fray
 
Test Essentials @mdevcon 2012
Test Essentials @mdevcon 2012Test Essentials @mdevcon 2012
Test Essentials @mdevcon 2012Maxim Zaks
 
ooc - A hybrid language experiment
ooc - A hybrid language experimentooc - A hybrid language experiment
ooc - A hybrid language experimentAmos Wenger
 

Semelhante a Introduction to Unit Testing (20)

Test
TestTest
Test
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Test Driven
Test DrivenTest Driven
Test Driven
 
The state of PHPUnit
The state of PHPUnitThe state of PHPUnit
The state of PHPUnit
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Cursus phpunit
Cursus phpunitCursus phpunit
Cursus phpunit
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Automated Frontend Testing
Automated Frontend TestingAutomated Frontend Testing
Automated Frontend Testing
 
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
 
The State of PHPUnit
The State of PHPUnitThe State of PHPUnit
The State of PHPUnit
 
The State of PHPUnit
The State of PHPUnitThe State of PHPUnit
The State of PHPUnit
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Unit Testing in iOS
Unit Testing in iOSUnit Testing in iOS
Unit Testing in iOS
 
Amplify your stack - Jsfoo pune 2012
Amplify your stack - Jsfoo pune 2012Amplify your stack - Jsfoo pune 2012
Amplify your stack - Jsfoo pune 2012
 
Testing smells
Testing smellsTesting smells
Testing smells
 
The Art Of Performance Tuning - with presenter notes!
The Art Of Performance Tuning - with presenter notes!The Art Of Performance Tuning - with presenter notes!
The Art Of Performance Tuning - with presenter notes!
 
Practical unit testing 2014
Practical unit testing 2014Practical unit testing 2014
Practical unit testing 2014
 
Test Essentials @mdevcon 2012
Test Essentials @mdevcon 2012Test Essentials @mdevcon 2012
Test Essentials @mdevcon 2012
 
ooc - A hybrid language experiment
ooc - A hybrid language experimentooc - A hybrid language experiment
ooc - A hybrid language experiment
 

Mais de Mike Pearce

Agile Estimation
Agile EstimationAgile Estimation
Agile EstimationMike Pearce
 
The Shuhari of Agile
The Shuhari of AgileThe Shuhari of Agile
The Shuhari of AgileMike Pearce
 
UX For Developers
UX For DevelopersUX For Developers
UX For DevelopersMike Pearce
 
Thimbl: decentralised, open source, micro-blogging
Thimbl: decentralised, open source, micro-bloggingThimbl: decentralised, open source, micro-blogging
Thimbl: decentralised, open source, micro-bloggingMike Pearce
 
Api anti patterns
Api anti patternsApi anti patterns
Api anti patternsMike Pearce
 
Writing Effective User Stories
Writing Effective User StoriesWriting Effective User Stories
Writing Effective User StoriesMike Pearce
 

Mais de Mike Pearce (7)

Agile Estimation
Agile EstimationAgile Estimation
Agile Estimation
 
The Shuhari of Agile
The Shuhari of AgileThe Shuhari of Agile
The Shuhari of Agile
 
Being Agile
Being AgileBeing Agile
Being Agile
 
UX For Developers
UX For DevelopersUX For Developers
UX For Developers
 
Thimbl: decentralised, open source, micro-blogging
Thimbl: decentralised, open source, micro-bloggingThimbl: decentralised, open source, micro-blogging
Thimbl: decentralised, open source, micro-blogging
 
Api anti patterns
Api anti patternsApi anti patterns
Api anti patterns
 
Writing Effective User Stories
Writing Effective User StoriesWriting Effective User Stories
Writing Effective User Stories
 

Último

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 

Último (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 

Introduction to Unit Testing

Notas do Editor

  1. - This presentation WILL NOT teach you how to unit test, it will introduce you to the concept, a brief overview of how to do it, how you can extend it and how it links to TDD
  2. - Pura already introduced us to the concept of TDD.- Before we can start pure TDD, we need to understand Unit Testing- UT is the meat of TDD, without a thorough understanding, TDD is all but impossible.
  3. - Unit testing is the act of testing the smallest testable part of a code, a "unit"- Unit testing should become second nature- Unit testing can also be automated, more about that at the end.- This pres will introduce you to the way I did unit testing for the STS front end.
  4. - This pres. is about PHPUnit.- Pura has already successfully been using SimpleTest and will demonstrate his findings later.- We need to decide which is more suitable. PHPUnit, SimpleTest or both!
  5. - PHPUnit is one of the xUnit (collective term for the UNIT tests ie: JUnit, PHPUnit, RUnit. Started with Sunit for smalltalk) family of frameworks that originated with Kent Beck's SUnit.- sUnit was written to test small talk and was later adapted to jUnit for Java and then further to PHPUnit for PHP.
  6. Imowreck666Small bugs
  7. Opo TerserBig Bugs
  8.  conorwithonenYoung code* hiro008
  9.  conorwithonenYoung codeOld code* hiro008
  10. When developing and adding tests, running the tests and seeing them pass increases confidence in what you're doing, but, don't become complacent.Also...
  11. Living Documentation of the system; unit tests can be run independantly to see how a method works
  12. tm-tmBeware------------- Unit testing can never test everything- You can't possibly test all variations of input (unless it is absolutely expected)- UT should never be completley relied on- Still need integration testing and manual QA- You should also use version control, if a test that failed previously passed, then you'll need to go and look at why it passed to compare with why it fails (unless it is obvious).- Any failed tests should be identified and the problem fixed immediately otherwise the app will evolve out of sync with the tests and no-one will trust them making them useless.- Unit testing may add significant time (ex: if you have a function that returns boolean TRUE/FALSE, you need to write two tests that test both outcomes.)
  13. This is the class we want to test.
  14. malingering/
  15. This is the class we want to test.public function someFunction($parameterOne, $parameterTwo) { return ((int)$paramterOne == $parameterTwo ? TRUE : FALSE);}
  16. mailo [...someonestolemyname]
  17. Ricky Romero
  18. EricNixon
  19. A stub replaces a real component on which the SUT (subject under test) depends on. This, for example, could be a “model” which will never actually looks in the database, it just returns a predefined set of data
  20. A mock object is an object which replaces a real object in the execution of code. Similar to stubs, they don’t just return data, they check the data they are given is sane first then return pre-defined data. They can be used to throw exception in your test suite/harness to report errors in execution.It might be that you can’t directly test a method/class (it might be private, or otherwise not easily executable from external to the class). In that case, if the method/class accepted an object, you could pass it a mock object that had all the attributes of the real object, but also did some testing as it went about it’s business.
  21. Hryck.
  22. You can also test code that mutates data in a db.
  23. Simply put, you provide PHPUnit with an XML document describing the database before and after a test, then run the test, then ask PHPUnit to check the two files against the database. You can also add filters to the database tests, for example if your database has a datetime column you’ll never be able to test it, so you can filter that kind of stuff out.
  24. What else can you do?
  25. BalakovXinc is our continual integration server (or CI Server for short)Xinc watches your every move, like some draconian overlord. If you commit something to the repo with broken tests or a code coverage percentage lower than the overlord likes
  26. Adam Foster | Codefor You’ll have “broken the build” Everyone will be alerted and shame will be poured upon you… Naughty.
  27. ZenTest harness or automated test framework is a collection of software and test data configured to test a program unit by running it under varying conditions and monitoring its behaviour and outputs. It can test at a higher level, ensuring the program “does what it is supposed to do”
  28. A collection of test cases that test specific scenariosLess commonly known as a “validation suite”Test each “part” seperately A “part” can be a method, or a class, or a bunch of methods. Depending on what’s appropriate.
  29. Sebastian Bergmann Selenium is a suite of tools to automate web app testing across many platforms. Specialises in front end testing (ie: testing web interfaces)
  30. FlySiYou can think of it as your “automated testing robot” Like a QA guy you can get to run your tests many, many times
  31. FlySi He won’t mind. He’s used to it. He can test all your form fields, your logins. With a known set of data, he can test data consistency, data accuracy and data validity. Selenium will actually open your target URL in a browser of your choosing, and check it renders correctly ( to a point, it’s not a messiah ). Failing tests if it finds things it doesn’t recognise or is unable to assert as true.
  32. - Finally, on to TDD
  33. It’s very much like I just described ... But backwards.First, you write your tests. Once you’ve done unit testing for a while, you’ll get a feel for what a test should do. You’ll be surprised, it’s not always about testing each method, but a group of methods (test harness)
  34. ....TimRunning this ...
  35. Creates this...
  36. Behaviour Driven Development (not Business Driven Development, although it could be!)ITS NOT TESTING!
  37. Behaviour Driven Development (not Business Driven Development, although it could be!)ITS NOT TESTING!