win at lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · unit tests...
TRANSCRIPT
![Page 1: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/1.jpg)
Win at Lifewith Unit Testing.
by Mark Story
![Page 2: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/2.jpg)
Who is this goofball
Art college graduate that needed to make money.
CakePHP core contributor for 2.5 years. Developer of DebugKit, ApiGenerator and several other plugins.
Lead front end developer at FreshBooks
![Page 3: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/3.jpg)
Can unit testing make me
awesome?
![Page 4: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/4.jpg)
Hell yes.
![Page 5: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/5.jpg)
Why write tests?
Helps ensure things that did work, still do.
Helps reduce time it takes to fix new defects.
Helps you plan a design, showing you where it might be complicated or overly coupled.
Living documentation.
![Page 6: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/6.jpg)
...
Catch issues earlier.
Increased developer confidence.
Automated killer robots.
![Page 7: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/7.jpg)
What to test?
Code that keeps you up at night.
Code that involves real money.
Code that has broken before.
Code that is tedious to test manually.
![Page 8: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/8.jpg)
Different flava’s
Unit tests
Functional or Integration tests
![Page 9: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/9.jpg)
Unit Tests
Testing the smallest parts of your application in isolation.
Often testing single methods or single functions.
Generally uses many mocks.
Really useful when doing Test driven development.
![Page 10: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/10.jpg)
Functional Tests
Tests pieces working well together.
Test top level objects to ensure all their guts are working together.
Tests hitting database fixtures are often functional tests.
Functional tests run slower, but use more active code.
![Page 11: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/11.jpg)
Challenges of testing
Time. It takes time to write tests. But it takes more time to fix the same bugs later multiple times.
Only shows the presence of issues, not the absence.
Only catches errors you have tests for.
![Page 12: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/12.jpg)
Benefits of testing
Find problems earlier.
Can be automated, robots are helpful.
Less manual testing.
Easier to ensure requirements are met.
Unicorns.
![Page 13: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/13.jpg)
The End.Of the part where I extol the winsauce tests
can bring.
![Page 14: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/14.jpg)
Mock objects <insert>A funny joke</insert>
![Page 15: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/15.jpg)
Mocks and test doubles
Writing isolated tests is hard. Mock objects make it easier.
Mocks help ensure your objects are touching other objects in the right places.
Mocks help you stub out behaviour before you write the real deal.
![Page 16: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/16.jpg)
Can I use a mock for:
Global functions?Stuff not in functions?
![Page 17: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/17.jpg)
No.
![Page 18: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/18.jpg)
When to use mocks
When talking to the outside world.
Email.
Webservices (Twitter, Paypal, YouTube).
Writing files to disk.
Writing to the database.
![Page 19: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/19.jpg)
...
When you want to ensure two objects are working together properly.
When you do want to isolate problems.
![Page 20: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/20.jpg)
Mocking your code
Make sure dependencies can be replaced at runtime.
Constructor injection.
Factory methods.
Setter methods.
![Page 21: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/21.jpg)
Constructor Injection
Accept objects your object depends on in the constructor.
Jam mocks in when testing.
![Page 22: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/22.jpg)
Factory Factory Factory
Factory methods can be overridden in subclasses to return mocks.
![Page 23: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/23.jpg)
Setter method
Allows post construction modification of internal dependencies.
![Page 24: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/24.jpg)
Mocks in the house
Mocks can be critical of a test subject.
Mocks and stub out dangerous components, or methods.
![Page 25: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/25.jpg)
Critical mockingCritic mocks check that other objects touch them correctly.
![Page 26: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/26.jpg)
Stub danger.
You can stub methods that send headers or touch the outside world.
![Page 27: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/27.jpg)
Stub expensive stuff.
Mocks can be used to stub network resources or expensive to fetch results.
![Page 28: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/28.jpg)
In conclusion
Mocks help make tests run faster, and require less setup & teardown.
They allow you to write true unit tests.
![Page 29: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/29.jpg)
The End.(of the part where I blabber about mocks)
![Page 30: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/30.jpg)
Automated Killer Robots
test automation for squishy humans.
![Page 31: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/31.jpg)
Automated testing.
Tests that are never run, are as good as deleted.
Build servers to the rescue!
![Page 32: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/32.jpg)
Hudson
Hudson is a continuous integration server built in Java.
Has a great plugin community.
Can send emails, Jabber and give pithy Chuck Norris quotes.
![Page 33: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/33.jpg)
Can be configured to run on a schedule or post commit.
Integrates well with SVN and there is a GIT plugin too.
...
![Page 34: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/34.jpg)
Typical Hudson setup
Run tests on each commit/push.
Run all tests again each night, incase there was a slow day.
Nightly builds are also a good time to run code coverage builds.
![Page 35: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/35.jpg)
Setting Hudson up
Go to http://hudson-ci.org
wget http://hudson-ci.org/latest/hudson.war
java -jar hudson.war
![Page 36: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/36.jpg)
Hudson dashboard
![Page 37: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/37.jpg)
Hudson dashboard
![Page 38: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/38.jpg)
Hudson dashboard
![Page 39: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/39.jpg)
Hudson dashboard
![Page 40: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/40.jpg)
Hudson project
![Page 41: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/41.jpg)
The End.For real this time.
![Page 42: Win at Lifemark-story.com/files/win_at_life_with_unit_testing.pdf · 2015. 4. 26. · Unit Tests Testing the smallest parts of your application in isolation. Often testing single](https://reader035.vdocuments.site/reader035/viewer/2022071509/612d55881ecc515869421fde/html5/thumbnails/42.jpg)
Questions?