@gil_zilberfeld
Introduction to unit
testing
@gil_zilberfeld
Hello!
I AM GIL ZILBERFELD
www.gilzilberfeld.com
www.everydayunittesting.com
@gil_zilberfeld
@gil_zilberfeld
What is testing?
@gil_zilberfeld
Brian
Marick’s
Testing
Quadrants
@gil_zilberfeld
Manual tests
Slow
Do not scale
Encourage forgetfulness
Immoral
@gil_zilberfeld
Automated tests
Run all the time
Don’t complain
Don’t forget
@gil_zilberfeld
The testing
pyramid
http://blogs.agilefaqs.com/2011/02/01/inverting-the-testing-pyramid/
@gil_zilberfeld
Why unit tests?
◉ Quick feedback
◉ Confidence to change your code
◉ Quicker than debugging
◉ Knowing your code works
◉ Low TCO
◉ Small setup overhead
◉ It’s not bugs if it’s caught before QA
◉ Go faster
@gil_zilberfeld
xUnit frameworks
Test identification
Execution and reporting
Asserts
@gil_zilberfeld
C++ xUnit frameworks
CppUnit
Boost Test
Catch
Google Test
@gil_zilberfeld
Java xUnit frameworks
JUnit
TestNG
@gil_zilberfeld
C# xUnit frameworks
NUnit
MSTest
xUnit
@gil_zilberfeld
javascript xUnit frameworks
QUnit
Jasmine
Mocha
@gil_zilberfeld
Demo
The humble lightsaber
@gil_zilberfeld
Make unit tests great again!
What makes them great?
@gil_zilberfeld
Quick
Readable
Focused
Deterministic
Independent
Self-cleaning
Great unit tests are…
@gil_zilberfeld
Refactoring
What is it?
@gil_zilberfeld
Refactoring
◉ Changing the form of the code without changing
the functionality
◉ Safer with tests
◉ But sometimes we need to refactor in order to
test
@gil_zilberfeld
Continuous integration
The mindset, not the tools
@gil_zilberfeld
Unit testing economics
What’s the ROI?
@gil_zilberfeld
Let’s get back to code
Legacy code, that is.
@gil_zilberfeld
Legacy code
◉ Contains all kind of dependencies
◉ Does all kinds of things together
◉ It’s ugly
◉ It’s fragile
◉ It’s rotting
@gil_zilberfeld
Legacy code
It’s hard to unit test
@gil_zilberfeld
Isolation and mocking
Manual
Frameworks
Simulators
@gil_zilberfeld
Mocking frameworks
◉ Test your code for different scenarios
◉ Fake the dependencies
◉ Change behavior of methods
◉ Assert method calls
@gil_zilberfeld
C/C++ mocking frameworks
Google Mock
Hippo Mocks
FFF
… and many more
@gil_zilberfeld
Java mocking frameworks
EasyMock
Mockito
Power Mockito
JMockIt
@gil_zilberfeld
C# mocking frameworks
Moq
FakeItEasy
Isolator
@gil_zilberfeld
javascript mocking frameworks
Sinon.js
@gil_zilberfeld
Demo
@gil_zilberfeld
Tips and tricks
@gil_zilberfeld
Unit testing advice
◉ Think ahead
◉ Make a test plan, not just for unit tests
◉ Test what you’re working on
◉ Unit testing is a team sport
◉ Coverage metrics
@gil_zilberfeld
Unit testing advice
◉ Quick feedback
◉ Organize your tests
◉ The cone of dependency
◉ Name like a detective
◉ Readability first
@gil_zilberfeld
Unit testing advice
◉ Test one thing
◉ Black box vs white box
◉ Test framework emergence
@gil_zilberfeld
Test Driven Development
@gil_zilberfeld
Why test first?
@gil_zilberfeld
Test driven
development
@gil_zilberfeld
Demo: TDD
@gil_zilberfeld
Resources
◉ Michael Feathers
Working effectively with legacy code
◉ “Uncle Bob” Robert Martin
Clean code
The clean coder
◉ Kent Beck
Test driven development by example
Extreme programming explained
@gil_zilberfeld
Resources
◉ James Grenning
Test driven development for embedded C
◉ Martin Fowler
Refactoring: Improving the design of existing code
◉ James Shore
Let’s play TDD
@gil_zilberfeld
Thanks!
ANY QUESTIONS?
You can find me at:
@gil_zilberfeld
http://www.GilZilberfeld.com
http://www.EverydayUnitTesting.com

Introduction to Unit Testing