October 1, 2012
When you are writing unit tests in C#, regardless of what unit testing framework you are using there are a number of methods you can choose for performing your asserts. The most common ones are perhaps Assert.AreEqual
, Assert.IsTrue
and Assert.IsFalse
that are all present in both MS Test and NUnit (and probably others as well, but these two are were my main experience is, so I am primarily looking at those today).
If you want to make an assert that a certain value meets a certain criteria, there are (at least) two ways of doing this. There is the AreEqual
way:
[Test]
public void AssertAreEqual()
{
IEnumerable list = GetListOfStuff();
Assert.AreEqual(4, list.Count());
}
That's pretty straightforward, and the test will fail if list.Count()
return anything else than 4. Another approach is to use IsTrue
:
[Test]
public void AssertIsTrue()
{
IEnumerable list = GetListOfStuff();
Assert.IsTrue(list.Count() == 4);
}
That will have pretty much the same effect. Apart from one big difference; this test will not tell you in what way it failed. The test runner that you use will have less information about the fail for you in the second case. I will illustrate this with a couple of screenshots, showing how (the very excellent) NCrunch will visualize this:
NCrunch reporiting failed Assert.AreEqual
NCrunch reporting failed Assert.IsTrue
As you can see, when using AreEqual
, the test runner is able to extract details about the failure, so that you can see the expected and actual values. When using IsTrue
, well, the expected value was true
, and the actual value was false
.
Here is a view of what Resharpers test runner makes of the tests. As you can see, you get the same information there.
Also note that NUnit’s Assert.That
will expose the same information as Assert.AreEqual
.
The bottom line is that choosing assert method is not only a matter about personal taste, it will also in a very concrete way affect the information that you get out of your failing tests. Use Assert.IsTrue
and Assert.IsFalse
only if you are actually verifying a boolean value (and no, list.Count() == 4
is not a boolean value in this context...).