TDD Kata

Kata

  • A form, a practice, a try
  • Small word problem made to be solved as a method of introspection
  • A place to exaggerate a methodology in order to practice the craft

Step Size

  • One Test at a Time
  • Smallest Test you can possibly think of
  • Only the code to pass the test

Step Size

class TddBasics {

    @Test
    fun firstTest() {
        assertEquals(2, sum(1, 1))
    }
}
fun sum(a: Int, b: Int): Int {
}

Step Size

class TddBasics {

    @Test
    fun firstTest() {
        assertEquals(2, sum(1, 1))
    }
}
fun sum(a: Int, b: Int): Int {
    return 2
}

Step Size

class TddBasics {

    @Test
    fun firstTest() {
        assertEquals(2, sum(1, 1))
    }

    @Test
    fun secondTest() {
        assertEquals(4, sum(1, 3))
    }
}
fun sum(a: Int, b: Int): Int {
    return a + b
}

Pairing

  • A writes a test
  • B passes the test
  • B writes the next test
  • A passes the test
  • Repeat

Commits

  • Write one commit per test
  • Write a good commit message
    • A good commit message is in the present imperative. It tells what the patch does to the code base
      • Instead of “I walked the dog”, write “Walk the dog”
    • Not what you did, but why you did it
      • Instead of “Add five to tax calculation”, write “Account for base tax amount”

Refactoring

  • Red, Green, Refactor
  • Not done every test, but should be considered every test
  • Should not change existing tests, 90% of the time
    • Changing tests indicates that the tests are too much at implementation level
  • Should NEVER add new tests or functionality during this stage
  • If you’re newer to TDD, it’s better to skip this then to fall into using this as an escape hatch to not do TDD

Points

  • Not a competition…. But if it were…
  • If I wrote a smaller test, I get more points
  • If I wrote less code to make it pass, I get more points
  • If I added functionality not needed for the test, I lose points
  • If I am clever and accomplish two things at once, I lose points
  • How literally can you do the instructions? How anal can you be?
  • This is the time to over exaggerate so you get practice.

Redundant Repetition

  • Every time I explain this to someone they nod… and then they write multiple tests or implementation in one commit. Don’t fall into that trap!
  • The point is to be anal, if you’re thinking you’re being clever by skipping steps, you’re just cheating yourself!

An Opportunity for Review

  • If you’d like me to take a look at your kata when you’re done, shoot me a link to the repo and I’ll spend some time and give you feedback. :)