Comparing lists with NUnit – the definite guide
Very often you’ll run into situation where in your unit test you have to compare two lists against each other. In this post I show several ways to do that, describe common pitfalls and nuances.
Simple types with Assert.AreEqual
NUnit will then use its custom EnumerablesEqual to verify whether each element of
actual has its corresponding element in
expected (note that both lists have to be in the same order). Comparison of individual elements will default to
In case order of elements is irrelevant we’ll have to use
Equivalent comparison instead:
Problems arise when we have to compare collections that contain our custom-defined types. Consider this naive Quotation type which stores information about stock closing price:
The following test will naturally fail as we know even though compared quotations are semantically the same, they are in fact, different instances:
This is easily fixable though. Remember how I mentioned NUnit will default to using
Equals in individual objects comparisons? All we have to do is provide one. Or do we? There are few issues with such approach:
- To do it correctly, we’ll have to implement not only
GetHashCode(which has nothing to do with our original problem).
- It forces us to extend our custom class with test-only implementation. While it is true that unit tests will often drive design of our code, the changes should come from how our code is used, not from testing frameworks limitations.
Since there doesn’t seem to be any better way to do this2, for now we’ll implement
Equals to make the test pass.
Problem with reference types
What about types we cannot override
Equals? Like XElement? We could project our collection to bit more representable form:
This works but you can probably already see problems with such approaches – we have to add methods we don’t really need (
Equals), use methods that were not meant to be used this way (
ToString) or resort to overly complex solutions. It looks very clumsy. We need to find a better way.
Just like virtually any problem you might ever encounter this one has, too, already been solved. FluentAssertions is a library that contains a set of extensions to make asserts more fluent (name kind of spoiled it). On top of really good API and clear error messages it provides solutions for common assertions problems, like the one we are facing here.
ShouldBeEquivalentTo extension method is what we are looking for. It is a smart method that will compare entire objects graph and apply appropriate strategy depending on underlying types. In our case, the actual XMLs will be compared (or to be more specific, properties of XElement that are relevant to XML semantics). This makes our test look like this:
You might have noticed I changed the order of elements in
expected lists. By default
ShouldBeEquivalentTo performs orderless comparison (same as NUnit’s Is.EquivalentTo). Changing this behavior is as easy as this:
In case order is different we’ll get helpful message explaining what’s going on:
Expected item.Name.LocalName to be “a”, but “b” differs near “b” (index 0).
Expected item.Name.LocalName to be “b”, but “a” differs near “a” (index 0).
We can also get rid of
Equals method on our Quotation class.
ShouldBeEquivalentTo will compare type’s public properties against each other to a decent depth level – they don’t even have to be the same type (all of this is highly configurable).
ShouldBeEquivalentTo is just one of multitude of methods and approaches you can use. For collections alone there’s 20+ custom methods. Resulting assertions are fluent. I bet you know exactly what’s going on in the following snippet without a word of explanation:
31.July(2014). It is FluentAssertions’s fluent API for date & time.
I barely scratched the surface of FluentAssertions. It supports many different types, has excellent extensibility options, is highly configurable, produces meaningful error messages and very readable unit tests code. Go discover it all yourself. Forget about NUnit’s Assert. Even though it is good API, FluentAssertions is simply vastly superior. Once you’ll start using it you will never go back.
The definite guide contains single rule – start using FluentAssertions.