Quick example : where to draw the line on a unit test
| code | craftsmanship | fundamentals |
This just came up and I've been asked numerous times "when do I draw the line?" when it comes to a set of tests. Here's a great example of where that line happens...
[TestClass]
public class ExportStory_Tests
{
private string fakeXml = @"
<rss version=""0.92"">
<channel>
<item>
<title>[TEST-1] Test</title>
<type id=""1"" iconUrl=""https://someinstance/"">Story</type>
</item>
<item>
<title>[TEST-2] Test</title>
<type id=""2"" iconUrl=""https://someinstance/"">Backlog</type>
</item>
<item>
<title>[TEST-3] Test</title>
<type id=""3"" iconUrl=""https://someinstance/"">Defect</type>
</item>
<item>
<title>[TEST-4] Test</title>
<type id=""4"" iconUrl=""https://someinstance/"">Task</type>
</item>
</channel>
</rss>
";
private ExportStories _exportStories;
private IEnumerable<XElement> _result;
[TestInitialize]
public void Setup()
{
_exportStories = new ExportStories(null, null);
var elements = XDocument.Parse(fakeStoryXml).XPathSelectElements("rss/channel/item");
_result = _exportStories.GrabStories(elements, new[] { "story", "backlog" });
}
[TestMethod]
public void grabs_two_stories()
{
_result.Count().ShouldEqual(2);
}
[TestMethod]
public void grabs_test_one_and_test_two()
{
_result.First().Element("title").Value.ShouldEqual("[TEST-1] Test");
_result.Last().Element("title").Value.ShouldEqual("[TEST-2] Test");
}
}
Somewhat simple, nothing fancy going on here -- all it tests is grabbing the right items via a filter (kinda?). So I could've drawn the line at the first test. It was 4 elements, now its two, so we're good to go !
Not so fast. I have two of them, but are they the right ones? What if my logic was 100% reversed so instead of two stories it grabbed the other 2 things? Ok, so I could add in 1 more item to my test data and that would certainly hint that was correct ...but I preferred to take the inspection to the items directly so the second test checks for the titles.
Where I drew the line is here : "does the order matter, ever?" No, absolutely not. I'm controlling whats going in and I know it's sequential. I could write a bit of code to grab the two titles and ask if it contains what I expect -- but that's the line I decided to draw.