This talk answers questions asked about unit testing by attendees at ALE 2013.
It was created in two hours, starting from an open space session where we gathered the questions.
2. We are the Code Retreat Brothers
@alexboly @adibolb
First European Code Retreat,
co-facilitated with Maria
Many Code Retreats around Europe
3. Disclaimers
1.Nobody was hurt while creating the talk
2.It's the fastest talk we ever created
3.It answers your questions
4.We might be wrong
5.It might contain jokes and 5-level pyramids
4. The top 5 things I'd like to do in the next
hour
Talk, talk, talk
Discuss, answer questions
Practice
Rescue legacy
code or tests
Learn He told me I
could draw
6. Here's what we'll answer
1. What to unit test?
2. How to organize tests?
3. How to deal with complex test setups?
4. How to avoid brittle tests?
5. How to integrate devs & testers in the team?
6. How and when to introduce tests on legacy code?
7. How to do Javascript unit testing?
8. How to unit test databases?
7. But before that, some background
● Pyramid of tests
● Behavior Slicing
● Unit Test Structure
● Stubs vs. Mocks
12. How many tests should you write for this
code?
public int sameNumber(int number){
return number;
}
13. How many tests should you write for this
code?
public int sum(int first, int second){
return first + second;
}
14. How many tests should you write for this
code?
public DateTime daysAfter(DateTime date, int
days){
...
}
15. Behavior Slicing
● The process to identify behaviors that the code
should do
● How:
– Identify input and output
– Choose the important values for input (value sampling
or equivalence partitioning)
● Write one unit test per behavior
20. Risk-based testing
● What is the feature that, if it doesn't work, you
loose money?
● What is the part of the code that, if it doesn't
work, will bring the system down?
● What is the most complex part of the software?
● What are the types of mistakes the team makes
when developing?
21. How to organize tests?
● Will not show the 5-level
pyramid again
22. How to deal with complex test setups?
● One behavior per test
● Object Mother
– creates examples for tests
– e.g., the 'John' employee
● Builder pattern:
– aDate.withDay(1).withYear(2013).build();
● Maybe your production code design needs improvement
23. How to avoid brittle tests?
● One behavior per test (focus on behavior)
● Adjust level of checks
● Short tests
24. How to integrate devs and testers?
● Cross functional teams
● Pair tester and programmer on stories
● Maybe: Developer in Test role
– Watch Aimee Rivers' talk on
http://2013.itakeunconf.com
25. How and when to introduce tests on legacy
code?
● On modules with high cost of failure
● Whenever changing the code
– fixing a bug
– adding a feature
– code improvements
● If the code doesn't need to change, don't test it
● Start from integration tests and go down the pyramid