SlideShare a Scribd company logo
@benjamminstl
How Not to Suck at Unit
Tests
A presentation for Code PaLOUsa 

April 28, 2015
@benjamminstl
How I’ve Sucked at Unit
Tests
A presentation for Code PaLOUsa 

April 28, 2015
@benjamminstl
Ben Bishop
Purdue University graduate with a degree in Computer Graphics
Technology. 10+ years of development experience. Rendr Co-Founder.
@benjamminstl
David Ortinau
15+ years of development experience. Xamarin MVP. Beard
Enthusiast. Rendr Co-Founder.
@benjamminstl
A little about Rendr
We are a people focused software studio, building engaging solutions to
solve real world problems and improve lives.
@benjamminstl
@benjamminstl
@benjamminstl
@benjamminstl
@benjamminstl
@benjamminstl
@benjamminstl
Goals
If you take just one thing from this presentation, then I have achieved my
goal.
@benjamminstl
2009
@benjamminstl
Typical Production Experience
Time
Productivity
@benjamminstl
Typical Production Experience
Time
Productivity
We can totally do that!
We’re Rockstar Ninjas!
@benjamminstl
Typical Production Experience
Time
Productivity
Noooooo, the world will end!
We hate our lives.
@benjamminstl
@benjamminstl
@benjamminstl
A Culture
of NO
@benjamminstl
A Culture 
of
Friction
@benjamminstl
NO
O
NNO
NO
N
NOT Possible
Can’t
NOT within scope
NO
NO Time
@benjamminstl
Friction wastes energy
Friction robs us of efficiency
@benjamminstl
@benjamminstl
Scrum
Ken Schwaber
@benjamminstl
@benjamminstl
@benjamminstl
Saying “yes”
introduces
risk.
@benjamminstl
The Land Scrum
Forgot
https://www.youtube.com/watch?v=hG4LH6P8Syk
@benjamminstl
Segue
@benjamminstl
Six Projects 
with Unit Tests
@benjamminstl
Six “Failures”
@benjamminstl
Failed Attempt #1 at Unit Testing
@benjamminstl
@benjamminstl
Lessons Learned
- Couldn’t automate testing through UI

- Video streaming is extremely hard to test

- Model/View architecture did not facilitate testing

- Any testable state was in the view
@benjamminstl
Testable View State
@benjamminstl
Failed Attempt #2 at Unit Testing
@benjamminstl
@benjamminstl
Lessons Learned
• Tests that test a server API aren’t Unit Tests

• Hard to figure out how to not pollute your database with junk
data

• Actually considered “integration test”

• Best to separate server tests from app tests

• Again, architecture didn’t support true unit tests
@benjamminstl
Separate Server Tests
@benjamminstl
Stay DRY
@benjamminstl
Stay DRY
@benjamminstl
Stay Performant
@benjamminstl
Failed Attempt #3 at Unit Testing
@benjamminstl
@benjamminstl
Lessons Learned
• Hard to write tests for already existing classes

• Couldn’t test private functions 

• Spies

• Have your TestCase extend the class you’re testing

• Levels

!
@benjamminstl
Jason Sonmez
http://simpleprogrammer.com/2010/12/12/back-to-basics-why-unit-testing-is-hard/
Level 1 Unit Testing is where we have a single class with no external dependencies and no state.
We are just testing an algorithm.
Level 2 Unit Testing is where we have a single class with no external dependencies but it does
have state. We are setting up an object and testing it as a whole.
Level 3 Unit Testing is when we have a single class with at least one external dependency, but it
does not depend on its own internal state.
Level 4 Unit Testing is when we have a single class with at least one external dependency and
depends on its own internal state.
@benjamminstl
How do you test protected
elements?
@benjamminstl
Test Spy
@benjamminstl
Test Spy
@benjamminstl
Failed Attempt #4 at Unit Testing
@benjamminstl
@benjamminstl
Lessons Learned
• Use TestCase data meta tags

• ViewModel architecture is very testable

• Beware Yak Shaving

• testing setter/getters

• constructor assignments

!
!
@benjamminstl
Solution
@benjamminstl
Solution
@benjamminstl
Solution
@benjamminstl
Failed Attempt #5 at Unit Testing
@benjamminstl
@benjamminstl
Lessons Learned
• Don’t use static/hard-singleton classes

• Static classes and/or extension methods cannot be
replaced with Fake or Mock

• TestData brittleness

• Use data fixtures like AutoFixture, otherwise you have to
constantly update your test data generators.

• When testing integration test behavior not results

• Beware of the Red, Green, Refactor blur
@benjamminstl
VMIC
View
CommandInputInvoker OutputInvoker
Mediator
@benjamminstl
@benjamminstl
Testing Results
@benjamminstl
Static Class
@benjamminstl
FakeItEasy
@benjamminstl
FakeItEasy
@benjamminstl
Homegrown Brittleness
@benjamminstl
AutoFixture
@benjamminstl
Failed Attempt #6 at Unit Testing
@benjamminstl
@benjamminstl
Lessons Learned
• Set up brittleness

• Don’t use DI containers

• Readability

• What is easy to write is not easy to read

• Find ways to write less code

• Use interface tools/markup

• Binding frameworks

• Share code across platforms

!
@benjamminstl
Improve Readability
@benjamminstl
Improve Readability
@benjamminstl
Solution
Less code
@benjamminstl
All of the Native, None of the Isolation
The best of both worlds: Code Portability and Tailored UIs
Number of Classes Percent (with Unit Tests) Percent (no Unit Tests)
Core 207 43.2% 64.4%
Tests 158 32.9%
Android 58* 12.2% 18.1%
iOS 56 11.7% 17.5%
Total 479 (321)
*includes .axml and .xml files
@benjamminstl
All of the Native, None of the Isolation
The best of both worlds: Code Portability and Tailored UIs
Number of Lines Percent (with Unit Tests) Percent (no Unit Tests)
Core 9,886 35.3% 44.9%
Tests 5,979 21.4%
Android 5,606* 20.0% 20.0%
iOS 6,512 23.3% 29.6%
Total 27,983 (22,004)
*includes .axml and .xml files
@benjamminstl
MVVM
View
ViewModel
(via bindings)
Service
@benjamminstl
Example
@benjamminstl
Closing Thoughts
@benjamminstl
Unit Testing is a habit that is
no different than dieting or
working out.
@benjamminstl
Small steps to a larger
goal works better than
trying to be perfect
from the get go.
@benjamminstl
Small steps to a larger
goal works better than
trying to be perfect
from the get go.
@benjamminstl
Test Code isn’t easy
The quality of your test code has to be equal to or
greater than your production code. If test code is an after
thought and isn’t refactored it too will become brittle.
@benjamminstl
Get used to failure
A lot of devs want to avoid unit testing because
seeing failed tests forces you to deal with the
consequences of your change.
@benjamminstl
Ignorance is bliss…
Until a client asks you to change something and you don’t know what’s
going to break.
@benjamminstl
Research
@benjamminstl
Practice
@benjamminstl
Code Reviews
@benjamminstl
Thank You
David Ortinau
314.280.8445
dave@rendr.io
Ben Bishop
314.775.8800
ben@rendr.io

More Related Content

PPTX
Type mock isolator
PPTX
Level Up Your Automated Tests
PDF
Code Review Matters and Manners
PPTX
2013 09-11 java zone - extreme programming live
PDF
Level Up Your Automated Tests
PDF
Dev Nexus 2017 - TDD with React - Josh Quintana & Tom Gamble
PDF
Tdd in swift
PDF
Test-Driven Development (TDD) in Swift
Type mock isolator
Level Up Your Automated Tests
Code Review Matters and Manners
2013 09-11 java zone - extreme programming live
Level Up Your Automated Tests
Dev Nexus 2017 - TDD with React - Josh Quintana & Tom Gamble
Tdd in swift
Test-Driven Development (TDD) in Swift

What's hot (19)

PDF
TDD — Are you sure you properly test code?
PDF
Is this how you hate unit testing?
PPTX
An Introduction to Unit Testing
PPTX
Exceptions: Why, When, How and Where!
PPTX
The problem with tdd
PDF
Effective Unit Testing
PPTX
Make a better with clean code
ODP
Testing Philosphies
PDF
Tdd - Test Driven Development
PPT
TDD - Christchurch APN May 2012
PDF
Unit testing (workshop)
PPTX
Implementing TDD in for .net Core applications
PPTX
Benefit From Unit Testing In The Real World
PDF
Writing Tests Effectively
PPTX
Code review at large scale
PPTX
While You Are Coding
PDF
TDD Workshop UTN 2012
PPTX
PPTX
Lập trình hướng kiểm thử - Test Driven development
TDD — Are you sure you properly test code?
Is this how you hate unit testing?
An Introduction to Unit Testing
Exceptions: Why, When, How and Where!
The problem with tdd
Effective Unit Testing
Make a better with clean code
Testing Philosphies
Tdd - Test Driven Development
TDD - Christchurch APN May 2012
Unit testing (workshop)
Implementing TDD in for .net Core applications
Benefit From Unit Testing In The Real World
Writing Tests Effectively
Code review at large scale
While You Are Coding
TDD Workshop UTN 2012
Lập trình hướng kiểm thử - Test Driven development
Ad

Similar to How not to suck at unit tests (20)

PPTX
The Test way
PPTX
Unit testing
PDF
Unit testing, principles
PPTX
An Introduction to Developer Testing
PPTX
2016 10-04: tdd++: tdd made easier
PPTX
Making the Unstable Stable - An Intro To Testing
PDF
Building Maintainable Android Apps (DroidCon NYC 2014)
PDF
Testing and TDD - KoJUG
PPTX
Writing testable code
PDF
Unit testing - An introduction
PDF
Test Drive Development
PDF
Unit testing (Exploring the other side as a tester)
PPTX
Loopt unit test experiences
PDF
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
PPTX
Unit Testing and TDD 2017
PPTX
Unit testing
PPTX
Unit Testing talk
PDF
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
ODP
Writing useful automated tests for the single page applications you build
PPTX
Testing, a pragmatic approach
The Test way
Unit testing
Unit testing, principles
An Introduction to Developer Testing
2016 10-04: tdd++: tdd made easier
Making the Unstable Stable - An Intro To Testing
Building Maintainable Android Apps (DroidCon NYC 2014)
Testing and TDD - KoJUG
Writing testable code
Unit testing - An introduction
Test Drive Development
Unit testing (Exploring the other side as a tester)
Loopt unit test experiences
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Unit Testing and TDD 2017
Unit testing
Unit Testing talk
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Writing useful automated tests for the single page applications you build
Testing, a pragmatic approach
Ad

Recently uploaded (20)

PDF
medical staffing services at VALiNTRY
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
L1 - Introduction to python Backend.pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
AI in Product Development-omnex systems
PPTX
Introduction to Artificial Intelligence
PDF
Digital Strategies for Manufacturing Companies
PPTX
Transform Your Business with a Software ERP System
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
ai tools demonstartion for schools and inter college
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
medical staffing services at VALiNTRY
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Design an Analysis of Algorithms I-SECS-1021-03
Understanding Forklifts - TECH EHS Solution
L1 - Introduction to python Backend.pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
AI in Product Development-omnex systems
Introduction to Artificial Intelligence
Digital Strategies for Manufacturing Companies
Transform Your Business with a Software ERP System
Wondershare Filmora 15 Crack With Activation Key [2025
ai tools demonstartion for schools and inter college
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Reimagine Home Health with the Power of Agentic AI​
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus

How not to suck at unit tests