Writing Testable Code
Martin Hlavac @m_hlavac
Software Engineer at Wayfair
2
Write tests
Not important
Profit!
Agenda
3 step business model applied to testing
3
• What are unit tests?
Tests
What are tests?
4
• What are unit tests?
Unit tests are tests developers write to prove to themselves,
that the code they are writing works.
It enables them to make changes, without fear of breaking
everything. And they are really what enables teams to make
changes to code aggressively.
Tests
What are tests?
5
• What are unit tests?
Unit tests are tests developers write to prove to themselves,
that the code they are writing works.
It enables them to make changes, without fear of breaking
everything. And they are really what enables teams to make
changes to code aggressively.
Tests
What are tests?
6
• What are unit tests?
Unit tests are tests developers write to prove to themselves,
that the code they are writing works.
It enables them to make changes, without fear of breaking
everything. And they are really what enables teams to make
changes to code aggressively.
Tests
What are tests?
7
• What are unit tests?
Unit tests are tests developers write to prove to themselves,
that the code they are writing works.
It enables them to make changes, without fear of breaking
everything. And they are really what enables teams to make
changes to code aggressively.
Tests
What are tests?
8
• What are unit tests?
Unit tests are tests developers write to prove to themselves,
that the code they are writing works.
It enables them to make changes, without fear of breaking
everything. And they are really what enables teams to make
changes to code aggressively.
Tests
What are tests?
9
17 000 lines file + FEAR = EPIC SLOWDOWN
but
17 000 lines file + Easy changes = DELIVER FAST
Huge code example
Million well hidden dependencies
10
17 000 lines file + FEAR = EPIC SLOWDOWN
but
17 000 lines file + No fear = JUST SLOWDOWN
Huge code example
Million well hidden dependencies
11
17 000 lines file + FEAR = EPIC SLOWDOWN
but
17 000 lines file + No fear = JUST SLOWDOWN
but
100 lines file + No fear = MIGHT BE FASTER
Huge code example
Million well hidden dependencies
12
Different approaches for different problems
Spaghetti, Lasagna, Ravioli can live together
• Different problems require different solutions
• One is not necessarily better than other
• Small script might be just fine as Spaghetti code
• Some framework code can be nice Lasagna
• AWS services are tasty Ravioli
13
Fear slows us down
Regression bugs slow us down
Wrong assumptions slow us down
Manual testing slows us down
Outdated documentation slows us down
Unit tests
what is solves
Unit Tests
Remove fear
Prevent repetitive regression
Validate our assumptions
Are really fast
Document code
Make us faster
14
One more thing
Easy to test == Easy to reuse
Testable code
Easy to reuse
15
What is Repository?
It’s not only database
• Repository per Model, not necessary per table
• Provides a substitution for unit test
• Access to Data Store (not necessary SQL) is done only through Repository
- Never from model
- Never ever from controller
- Never ever ever from template
16
Service Pattern
Specific class that does one thing, in our case this is included in models
• Functional Layer / Business Logic
• Unit tests definately for these cases
• Services should be stateless
• Don’t be afraid to introduce new entity/repository
• NO INTERFACES FOR SERVICES
17
Demo
Let’s dive in!
18
Poisoning the Codebase with Non-Deterministic Factors
Let’s look at what’s they way out of it?
19
Poisoning code with side effects
Bamboo?
20
Write tests
Continous Integration with reliable test suite
Profit!
The hidden ingredient
CI, reliable test suite
21
Questions? Twitter: @m_hlavac
Writing testable code

Writing testable code

  • 1.
    Writing Testable Code MartinHlavac @m_hlavac Software Engineer at Wayfair
  • 2.
    2 Write tests Not important Profit! Agenda 3step business model applied to testing
  • 3.
    3 • What areunit tests? Tests What are tests?
  • 4.
    4 • What areunit tests? Unit tests are tests developers write to prove to themselves, that the code they are writing works. It enables them to make changes, without fear of breaking everything. And they are really what enables teams to make changes to code aggressively. Tests What are tests?
  • 5.
    5 • What areunit tests? Unit tests are tests developers write to prove to themselves, that the code they are writing works. It enables them to make changes, without fear of breaking everything. And they are really what enables teams to make changes to code aggressively. Tests What are tests?
  • 6.
    6 • What areunit tests? Unit tests are tests developers write to prove to themselves, that the code they are writing works. It enables them to make changes, without fear of breaking everything. And they are really what enables teams to make changes to code aggressively. Tests What are tests?
  • 7.
    7 • What areunit tests? Unit tests are tests developers write to prove to themselves, that the code they are writing works. It enables them to make changes, without fear of breaking everything. And they are really what enables teams to make changes to code aggressively. Tests What are tests?
  • 8.
    8 • What areunit tests? Unit tests are tests developers write to prove to themselves, that the code they are writing works. It enables them to make changes, without fear of breaking everything. And they are really what enables teams to make changes to code aggressively. Tests What are tests?
  • 9.
    9 17 000 linesfile + FEAR = EPIC SLOWDOWN but 17 000 lines file + Easy changes = DELIVER FAST Huge code example Million well hidden dependencies
  • 10.
    10 17 000 linesfile + FEAR = EPIC SLOWDOWN but 17 000 lines file + No fear = JUST SLOWDOWN Huge code example Million well hidden dependencies
  • 11.
    11 17 000 linesfile + FEAR = EPIC SLOWDOWN but 17 000 lines file + No fear = JUST SLOWDOWN but 100 lines file + No fear = MIGHT BE FASTER Huge code example Million well hidden dependencies
  • 12.
    12 Different approaches fordifferent problems Spaghetti, Lasagna, Ravioli can live together • Different problems require different solutions • One is not necessarily better than other • Small script might be just fine as Spaghetti code • Some framework code can be nice Lasagna • AWS services are tasty Ravioli
  • 13.
    13 Fear slows usdown Regression bugs slow us down Wrong assumptions slow us down Manual testing slows us down Outdated documentation slows us down Unit tests what is solves Unit Tests Remove fear Prevent repetitive regression Validate our assumptions Are really fast Document code Make us faster
  • 14.
    14 One more thing Easyto test == Easy to reuse Testable code Easy to reuse
  • 15.
    15 What is Repository? It’snot only database • Repository per Model, not necessary per table • Provides a substitution for unit test • Access to Data Store (not necessary SQL) is done only through Repository - Never from model - Never ever from controller - Never ever ever from template
  • 16.
    16 Service Pattern Specific classthat does one thing, in our case this is included in models • Functional Layer / Business Logic • Unit tests definately for these cases • Services should be stateless • Don’t be afraid to introduce new entity/repository • NO INTERFACES FOR SERVICES
  • 17.
  • 18.
    18 Poisoning the Codebasewith Non-Deterministic Factors Let’s look at what’s they way out of it?
  • 19.
    19 Poisoning code withside effects Bamboo?
  • 20.
    20 Write tests Continous Integrationwith reliable test suite Profit! The hidden ingredient CI, reliable test suite
  • 21.