Close

February 28, 2012

Small unit testing tip

When asserting the content of collections, lists, dictionaries,… in unit tests, don’t do this:

Assert.That(this.result.Contains(this.expectedResult1));
Assert.That(this.result.Contains(this.expectedResult2));

 

Or this:

Assert.AreEqual(this.expectedResult1, this.result[0]);
Assert.AreEqual(this.expectedResult2, this.result[1]);

What if you implemented your method wrong, and the result contains more than two elements? Unless you’re totally not interested in the size of the result, always check if the collection is as big as you expect it to be:

Assert.AreEqual(2, this.expectedResult.Count);
Assert.AreEqual(this.expectedResult1, this.result[0]);
Assert.AreEqual(this.expectedResult2, this.result[1]);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.