qcon'17 workshop: test first immersion
TRANSCRIPT
@mockitoguy
Test First Immersion WorkshopWriting Great Unit Tests
QCon, San Francisco, 11-16-2017
● Szczepan Faber, @mockitoguy
● Born in Poland, in US since 2015
● Codes professionally since 2002
● Creator of mockito.org in 2007
● Core dev of gradle.org 1.x and 2.x in 2011-2015
● Tech Lead at LinkedIn Dev Tools since 2015
● Author on linkedin.com/in/mockitoguy
● Believer of Great Code Reviews:
https://thenewstack.io/linkedin-code-review
Tell me and I forget.
Teach and I remember.
Involve me and I will learn.
~ Benjamin Franklin
Live as if you were to die
tomorrow. Learn as if you
were to live forever.
~ Mahatma Gandhi
Want to innovate in Open Source?
Join Shipkit - new project
used by Mockito: http://shipkit.org
@mockitoguy
IntroductionsCan you share your name, role and expectations?
@mockitoguy
How to best use this workshop?
Ask questions, interrupt me
Work together, you will learn more!
Take active part. Challenge me!
@mockitoguy
We will practice togetherTechniques for writing great tests so that...
… you can write code that is clean, maintainable and bug-free
● No way to learn it in a day
● Clean tests force clean code
● Develop sensitivity to great tests and code
● Want to truly master writing testable code? Use TDD every day for a year
@mockitoguy
Workshop agenda● Intro
● Learning models
● Workshop iteration 1
● Team code review, retrospective
● Workshop iteration 2 & 3
● Retrospective and summary
@mockitoguy
Yukiyoshi Takamura
The student must first resign
himself and his ego to a seemingly
random series of repetitious
exercises
About ego...
@mockitoguy
To learn, we need rules initially
Can you give examples of
programming rules?
1. KISS - keep it simple, stupid
2. DRY - don’t repeat yourself
3. SOLID - Single responsibility, Open-closed, Liskov,
Interface segregation, Dependency inversion)
4. One test case per method
@mockitoguy
Your thoughts about TDD?● great practice but it is not about tests. It is about…
● behaviors, best described using examples
@mockitoguy
The rules of the workshop● before implementing anything - write a test that demonstrates the behavior
● write only one test and run it before implementing code
Test
ImplementationRefactoring
● generate classes / methods from the test (alt+enter or ctrl+1)
● use //given //when //then comments template in each test
Start of iteration 1
@mockitoguy
Next steps● Iteration 1
● Team code review
● Iteration 2 & 3
● Summary and retrospective
@mockitoguy
Why test first?● Forces us to think through the functionality before implementing it
- we think about the code before we write it
- how should the system work? how should the API look like?
● We automate from the start
- testable architecture, code optimized for testability
- accelerated learning how to write testable code
- test utilities help us building future tests and functionalities
● We leverage IDE big time
- code generated from tests
- very efficient
● By the way, it gets us the test coverage, too :)
@mockitoguy
How to develop test first habit?● Practice (kata). Watch your ego and practice every day (SHU)
● Scale your expectations
- you will be slow at start, that’s ok
- you will have dilemmas how to test, code, when to refactor, that’s ok
- you will frequently refactor and rewrite the code, that’s ok
● Develop sensitivity and taste for great tests
- bad tests frustrate and discourage
- great tests guide you to write clean and well designed code
● Scale difficulty level
- don’t start by writing tests for the biggest, ugliest class in your code base
● Enjoy the journey to mastery (RI)
@mockitoguy
Your daily practice● don’t write tests, write examples of behavior
● one test -> one “should”
● one test -> one behavior
● write //given //when //then comments
● alt+enter (or ctrl+1) tells you how proficient you are