Being new to test based development, this question has been bugging me. How much is too much? What should be tested, how should it be tested, and why should it be tested? The examples given are in C# with NUnit, but I assume the question itself is language agnostic.
Here are two current examples of my own, tests on a generic list object (being tested with strings, the initialisation function adds three items {'Foo', 'Bar', 'Baz'}):
[Test] public void CountChanging() { Assert.That(_list.Count, Is.EqualTo(3)); _list.Add('Qux'); Assert.That(_list.Count, Is.EqualTo(4)); _list[7] = 'Quuuux'; Assert.That(_list.Count, Is.EqualTo(8)); _list.Remove('Quuuux'); Assert.That(_list.Count, Is.EqualTo(7)); } [Test] public void ContainsItem() { Assert.That(_list.Contains('Qux'), Is.EqualTo(false)); _list.Add('Qux'); Assert.That(_list.Contains('Qux'), Is.EqualTo(true)); _list.Remove('Qux'); Assert.That(_list.Contains('Qux'), Is.EqualTo(false)); }
The code is fairly self-commenting, so I won’t go into what’s happening, but is this sort of thing taking it too far? Add() and Remove() are tested seperately of course, so what level should I go to with these sorts of tests? Should I even have these sorts of tests?
I would say that what you’re actually testing are equivalence classes. In my view, there is no difference between a adding to a list that has 3 items or 7 items. However, there is a difference between 0 items, 1 item and >1 items. I would probably have 3 tests each for Add/Remove methods for these cases initially.
Once bugs start coming in from QA/users, I would add each such bug report as a test case; see the bug reproduce by getting a red bar; fix the bug by getting a green bar. Each such ‘bug-detecting’ test is there to stay – it is my safety net (read: regression test) that even if I make this mistake again, I will have instant feedback.