SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 

Kürzlich hochgeladen (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 

Introduction to Unit Testing

Hinweis der Redaktion

  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!