I've recently read The Art of Unit Testing by Roy Osherove which I found very useful for helping me establish how to define a good unit test.
One key aspect of the guidelines Roy puts forward are to make sure that one test only tests one thing, which is part of the following guidelines he puts forward:
- Make sure the test tests one thing only (p. 179)
- Make sure the test verifies only on a single call to a mock object. Verifying multiple calls on a mock object is either over specification or testing multiple things.
One good way to help achieve this is by having a single assert in each unit test.
However if the unit test is to validate the behaviour of a method returning an object which specific properties, how can this be verified with a single assert? It seems the possible answers are:
- Don't, just use multiple asserts to validate each expected property of the returned object
Object.GetHashCode(In C#) so that the two objects can easily be compared.
- Use a framework that allows for two objects to be compared (For example http://comparenetobjects.codeplex.com/ in C#)