Writing Tests: Don’t Make “Not my Job” “Just Business”
Join the DZone community and get the full member experience.Join For Free
philosophy and economics have something to say about unit testing. there are two schools of thought about whether programmers should have to write tests to verify their own code (in addition to writing the code itself). the philosophy of economics, and psychology, and morality, all overlap in studies that show how people will readily abandon moral responsibilities if they are given ways to avoid the stigma of doing so. this leads me to feel more justified in my belief that programmers do a poorer job of reading, understanding, and implementing a specification when someone else has the responsibility of verification.
changing the rules changes people’s attitudes
there is a 1998 experiment  that keeps popping up in the new “ freakonomics ”-type literature  (e.g. economics 2.0  ), where a controlled subset of israeli day-care centers started charging a fine for parents who came late to pick up their children. to everyone’s surprise, the number of people showing up late almost doubled. additionally, when the fine was later dropped, the number of late parents stayed at the high level. it is theorized that the moral responsibility parents felt to be on time was much stronger than the economic cost of paying a fine, which was rationalized by the parents as a fee, thus removing the stigma of being late. the fee made it “ just business ” . as professor michael sandel summarized  …
“so what happened? introducing the fine changed the norms. before, parents who came late felt guilty; they were imposing an inconvenience on the teachers. now parents considered a late arrival a service for which they were willing to pay. rather than imposing on the teacher, they were simply paying her to stay longer. part of the problem here is that the parents treated the fine as a fee. it’s worth pondering the distinction. fines register moral disapproval, whereas fees are simply prices that imply no moral judgement.”
don’t make “not my job” “just business”
the blind experiment is a well established doctrine in science requiring that people should not know too much about something they are testing, otherwise the results are often biased. scientists gathering their own raw data ( not to mention interpreting their own data ) often get the results they expected to get , where objective outsiders don’t. with that theory, it is argued that software development projects should engage external, objective, “qa testers” to develop and administer test suites against the code produced by the “programmers”. since many programmers don’t like to eat their spinach, ahem , write their own tests ( or documentation for that matter ), there are not usually arguments against the idea.
from my experience though…
- programmers will suffer peer pressure and social costs if they fail their own tests ( which is a good thing ).
- programmers who understand that they are obligated to deliver testable components, will do so more often if they must actually produce the tests themselves, compared to those where testing is “not my job” .
- the act of writing a test forces a clearer understanding of both the interface and the implementation of the tested component compared to just programming it.
- programmers who fail their own tests will be much more likely to change that component’s implementation if needed, rather than obstinately maintaining that the externally-produced test is wrong.
- writing your own tests is the most systematic method of “ eating your own dogfood ”
belt and suspenders
i say, both independent testers and the original programmers should
write independent test suites. this way you get the power of both
perspectives. this is an old lesson from the days of computer
punch-cards; two different people key-punch the same data so that they
can later be compared, thus eliminating most typos.
 a fine is a price, uri gneezy and aldo rustichini, journal of legal studies, vol. xxix ( january 2000)
 brain food: when does a fine become a fee?, aditya chakrabortty, the guardian, tuesday 23 february 2010
 why an l.a. times wikitorial effort went wrong, clay shirky, o'reilly media gov 2.0 summit, 2009-09-09
 pg 7, economics 2.0, norbert haring, olaf storbeck, palgrave macmillan, 2009
 michael sandel, the reith lectures 2009, bbc, 9th june 2009
this article was originally posted on existentialprogramming .
Opinions expressed by DZone contributors are their own.