Test-Driven Development Is a Paradox
TDD is favored by very succesfull engineers as a means of accellerating software development by preserving quality.
Objections are plenty. The talk by Burk Hufnagel tries to address those.
The TDD Paradox
TDD requires you to write more code, so you can be done sooner. It's like learning to drive a car. Your first mile takes, give or take, 40-50h to complete: Driving lessons, driving tests, get the license issued before you drive. You can walk a lot more in 50 hours, you get the drift...
Looking deeper at "development" we see, it has multiple components:
- understanding and clarifying the business requirements (business rather loosely defined)
- write code
- manual testing
TDD helps with the first and third component. A well defined test communicates the desired outcome and serves as a living specification. Having the machine running tests reduces the need for manual testing and frees up time to work on functionality
Test early, test often
TDD proposes to write tests first, not as an afterthought. The strongest new argument Burk proposed was: nobody wants to write a test for code freshly debugged and proven working (at that time). There's no immediate value in it. A test, initially failing, written upfront is a reassurance to be on the right track when going green.
Using the "test first" approach, tests turn into a design process, increasing the confidence in not only doing things right, but also doing the right thing. In other words: Tests are Specifications
Best results are to be had when working in small increments and resist the urge to "code for things you think are coming next" instead of "(just) make the test pass".
It's not a cure-it-all
TDD might not be a good fit:
- no clear business requirements - you can't test when you don't know what you want
- exploratory coding - trying stuff out
A common objection is "we have an existing non-TDD code base, we can't switch to TDD". Burk suggest to use "Defect Driven Development". Write a test that test for defect free behavior and then fix the defect. Your test harness will grow while defects shrink
Test long and prosper
The usual subjects for testing in Java are JUnit and Mockito. Burk introduced another, very promising tool: Spock. Spock is written in Groovy and allows to specify a test's purpose more expressive and concise than Junit's
@DisplayName property. Luckily it isn't an exclusive-or question, Spock interacts with JUnit and Mockito quite nicely.
Burk made a good case for TDD, backed by experience and tooling, if in doubt visit his arguments.
Posted by Stephan H Wissel on 19 October 2022 | Comments (0) | categories: Java