4

I am the only developer in my IT shop (a mid-size retailer) writing unit tests. I have been tasked by management with giving a presentation on the benefits of doing so to our developers.

One of the "aha!" moments that led to my unit-testing addiction was the realization that a unit test suite can prevent regressions in code that you would never have thought to re-test had you been testing manually (because it seemed unrelated to the change you were making).

I am looking for an example of such a test to include in the presentation. Ideally it would be "real-world" enough to have credibility, but self-contained enough to be appropriate for a classroom setting. Homebrewed tests are fine as are pointers to relevant texts. Language of implementation is not important.

Evan Haas
  • 2,524
  • 2
  • 22
  • 34
  • Although I don't have any examples, you could ask in the presentation "Have you ever changed a bit of code related to one feature and something completely different broke?" I'm sure everyone will nod. That will come from their own experience, so you won't need to convice them about this benefit of unit testing. – Augusto Aug 11 '11 at 13:43
  • The problem with this question is that you're asking the community to find an example for you. We don't really answer those kinds of questions. Also, even if you presented your own ideas the question would still be borderline acceptable due to the amount of discussion this would generate. We don't accept questions that lead to open ended discussion. – Kev Aug 11 '11 at 15:34
  • Understood. Is there one of the StackExchange family of sites where this _would_ be an appropriate question? Perhaps on [Programmers](http://programmers.stackexchange.com/)? – Evan Haas Aug 11 '11 at 15:44
  • google is here to help.. – Baz1nga Aug 11 '11 at 15:52

1 Answers1

3

Well consider the case of a factorial function: what are the various cases that you will cover for this:

a) It returns a value for 0

b) what happens if the number is negative

c) does it return the right values for two different numbers.

Now you have an implementation written for the same in some way. Tomorrow if someone decides to rewrite the same using a recursive function it should still pass the various tests that you have written if not then you caught something.

Is this example simple enough. something that came to my mind immediately.

Your app might be responsible for calcultaing the permutation and combination of somethings or a simple calculator for that matter.

Bonus: a ppt that I found sometime back that I shared with a colleague: http://www.slideshare.net/dhelper/real-life-unit-testing

Baz1nga
  • 15,485
  • 3
  • 35
  • 61