Unit Testing After, Before and TDD

Antoine Vernois / @avernois

Antoine Vernois

Software Anarchist

Common Sens Consultant

blog : https://blog.crafting-labs.fr

twitter : @avernois

#ISpeakForFood

Disclaimer

  • this is an introduction to unit testing
  • i won't talk about :
    • mocking
    • integration test
  • example are in java
and yes, i will do some over exaggeration :)

My definition

Unit test

a test that verify one behviour of one piece of code.

Why ?

  • because it's cool !
  • tests are a proof the you code does what you think it should when you wrote it
  • the sooner you find problem, the cheaper you'll solve them
  • confidence in your code
  • testable and tested code often lead to better design
  • tests tell what your code do

Quality of a unit test

  • Fast
  • Isolated
  • Repeatable
  • Self-validating
  • Timely




Test After.

bowling time !

(false) problems

Why bother with tests, it just works !
I'm in hurry, I have lot of functionnal to do. I'll write the test later.
Tests are for people who don’t know how to code !

lesjoiesducode.fr

(Bigger ?) problems

  • we could have write untestable code
  • we have never seen our tests fail !
  • often, we build test by reverse engeeniring the code

Test First

Test First

  1. Think what I want to do.
  2. Write tests according it.
  3. Write code that make all the tests pass.


Enjoy.

bowling time !





confession time

TDD : Test Driven Developpement

Test Driven Developpement

  1. Write exactly one failing test
  2. Make it pass
  3. Make it clean





Repeat

Test Driven Developpement

  1. Red
  2. Green
  3. Refactor


That's all !

bowling time !

Thank You !






Antoine Vernois / @avernois

Credits photos

Bowling - Markus Spiske
One - Andrechin
Ginny - One person can [...]
seanbjack - bowling 1
Alexander C. Kafka - Bowling pins
Ginny - bowling
Rachel Zack - Kitchen sink
/woodleywonderworks - dark before dark
Wonderlane - Fu / Return
Trevor Leyenhorst - i've never used the word bokeh before
Pargon - A kitten and a Scholar
jenny downing - after the rain
Tambako The Jaguar - Cute winking kitten!
Barbara Muller Walter - After Work