I was trying to explain to a manager the difference between unit testing and integration testing. I used a few different styles for explaining, but one jumped out at me as “have to blog about that one.”
The Analogy
- unit tests represent testing a word
- integration tests represent testing a sentence
Why this works
If you use spell a word wrong, you have no hope of your sentence being correct. The words are the units. If you use the words in the wrong order or are missing a word or use the wrong word, your sentence is not correct.
It also shows why both unit tests and integration tests are needed. With just unit tests you won’t know that “cat my hair ate the” is wrong. After all the individual units are correct.
With just integration tests, it is a lot more work to know everything is correct. While it is possible to test the entire sentence in one shot for spelling correctness, it is hard. As humans we spell check one word at a time even if we don’t realize it. That’s why “coding is fun” is faster to spell check than “codingisfun”.
Conclusion
Thoughts on this? I don’t remember reading this idea anywhere, but I may have read it and forgotten it. If anyone knows where it came from, feel free to post that as a comment.
Excellent analogy.
Thanks Xris!
Although I like the novelty of your analogy, it certainly a first for me, I do like to share the following insight.
A sentenced is composed out of words, but a integration test is not composed out of unit tests. I think it is important to stress this point. especially for your manager.
Excellent point Daan. You are correct that a pile of unit tests doesn’t give you an integration test. But neither does a pile of words give you a sentence. “blue name flag”. It’s words plus order that gives a sentence. That said, adding a constraint on unit tests still doesn’t yield integration tests.
Any analogy falls short of reality. Technically, spell-check, not the words themselves, are more analogous to unit tests. Higher-level actions, such as grammar checking, would correspond to integration tests. In that way, they are independent actions, not composed of each other. This can also help explain why both are needed: spell-check doesn’t catch homonyms, and grammar fails if words are spelled incorrectly.
But when communicating with non-technical managers about the need for both, the need to plan, and allow enough time for learning and executing, both, the relationships of words and sentences may suffice.