So, one opinion I’ve upheld for a long time was the idea that you should feel the pain of your design decisions. Why? Simple: you learn about the consequences of those design decisions. On the surface, it may appear that you made some excellent trade-offs, while in reality it turned out to be less than ideal.
The same is true when it comes to testing. Far too many organizations take the “toss it over the wall and let the test engineers write tests” attitude. Some organizations even believe it’s beneath a developer to write test code. I, however, believe developers need to play a large role in testing, especially at the unit-test level. It’s far too easy to make a decision that severely cripples testability. Without feeling the pain of having to develop the tests, you lose the feedback loop that lets developers learn and write more testable code.
I was excited to hear that advice being offered by Misko Hevery in The Clean Code Talks – Unit Testing. He basically offers the same advice I did above. He also offers something else: how to proportion your unit-tests relative to functional and scenario tests. I’ve always struggled with that question myself. On the one hand, I want to ensure the product handles edge-cases robustly. On the other hand, we’ve invested a lot of time in unit-tests.
What Misko offers is this:
- Unit tests should roughly match the number of lines of production code
- Functional tests should be a much smaller set of tests
- Scenario tests should be really small
To paraphrase Misko: once you get into functional and scenario tests, it’s more about testing the wiring than testing functionality (despite the name functional tests!). These tests are ensuring that the output of one object can be fed into another. While functional and scenario tests help show that a project is functioning, it doesn’t do much to tell you where a problem exists so that it can be fixed readily. And I have to agree with that assessment. Having struggled with scenario tests, I can say without a doubt that without extremely deep knowledge of a system, it can be impossible to get useful information from a scenario test.
I hate to keep linking to GoogleTalks, but I got into blogging to share. So: