Why unit test?

Reading Time: 2 minutes

For the pretty reports?
About a year ago, I worked for a client for an iOS project. The developer I worked with is very enthusiastic about unit testing, test driven development. I recall he used some tool (lcov, stands for line coverage), and set up the build script such that the unit test and lcov runs frequently. I was also shown the pretty chart created by lcov. All is good. except one day, I found a utility function I wrote was broken in the iPad simulator. Turns out the developer modified the code and added test for that function (a good thing), but at the same time forgot to run the real thing to check it.

Real motivation for unit testing
I think the unit test as another perspective to verify the code works as desired, this is from my own experience. I recall we have regression test suites in the software company I first worked professionally, that was more than 10 years ago! I started with waterfall, slowly transitioned into agile, test driven development. When we found new test cases (from problem report, etc.), we added those to our regression test suites as we see fit. We ran those unit testing suites before checking in code. This does not means the product we are developing is prefect, but it does give me more confidence as we add features or do bug fixes. This is also true in the many JUnit tests I saw/added in many places I worked since then.

More recently, as I saw the “mock” concept in JUnit, while I am not expert on “mocking”. Conceptually I liked this idea, as we don’t want to repeat the same thing in the production code, we add one more perspective. I also recall the famous investor Charlie Munger (Vice chair of Berkshire Hathaway) once said, he used at least two models to value a company/investment. Check and balance.

(Update) Came across this article on the disadvantages of pair programming. Also, saw this launchCodeSTL a while ago.

%d bloggers like this: