How to name our Unit Tests

Connection - 10'

Imagine we want to test this method :
1
public static boolean isLeapYear(int year)
Copied!
How would you name your first test ?
  • Ask attendees to note on a Sticky Note what is the name of their first test
  • Compare the different styles
  • What do they prefer (Dot voting)

Concepts - 10'

Tests names v1

1
yearsNotDivisibleBy4...
2
yearsDivisibleBy4ButNotBy100...
3
yearsDivisibleBy100ButNotBy400...
4
yearsNotDivisibleBy400...
Copied!
  • Is it readable ?
  • What is the intent ?

Tests names v2

1
years_not_divisible_by_4...
2
years_divisible_by_4_but_not_by_100...
3
years_divisible_by_100_but_not_by_400...
4
years_not_divisible_by_400...
Copied!
  • Is it more readable ?
  • What is the intent ?

Tests names v3

1
years_not_divisible_by_4_are_not_leap_years
2
years_divisible_by_4_but_not_by_100_are_leap_years
3
years_divisible_by_100_but_not_by_400_are_not_leap_years
4
years_not_divisible_by_400_are_not_leap_years
Copied!
  • Intent is more clear, isn’t it ?

Tests names v4

1
public class Leap_year_spec {
2
public static class A_year_is_a_leap_year {
3
@Test public void if_it_is_divisible_by_4_but_not_by_100()...
4
@Test public void if_it_is_divisible_by_400()...
5
}
6
7
public static class A_year_is_not_a_leap_year {
8
@Test public void if_it_is_not_divisible_by_4()...
9
@Test public void if_it_is_divisible_by_100_but_not_by_400()...
10
}
11
}
Copied!
  • Express what the class should be able to do
  • Read it as a full sentence : express a true business specification
  • You should avoid technical terms

Concrete Practice - 30'

  • Choose a Kata on Coding Dojo
  • Extract the Test Cases you want to write
    • Define the structure and naming of your tests

Conclusion - 10'

Imagine you use this naming technique during the next 6 months :
  • What has changed ?
  • Who else have noticed those changes ?
  • What are the impacts on your day to day ?

Resources