Please Use Assert.AreEqual Correctly

NUnit and MSTest both have an assertion method named AreEqual and it does what you'd expect: checks whether or not two values are equal. In both unit testing frameworks, the method signature looks like this:

public static void AreEqual<T>(T expected, T actual)

Notice that the first parameter is named "expected" and the second parameter is named "actual", not the other way around. For all that is holy and good, do not switch these around. Doing so can lead to unexpected test results: a test will fail (maybe not unexpected) with an output that shows the opposite of what you expect (that's the unexpected part), and then you're left wondering why and it's all because you didn't pay attention to details and switched the parameters.

So please get it right, and while you're at it, I'm gonna need you to send in your TPS report.

Print | posted on Thursday, December 20, 2007 12:34 PM

Feedback

# re: Please Use Assert.AreEqual Correctly

Gravatar left by Dan Hounshell at 12/20/2007 2:30 PM
Dude, that is so funny because just a couple of weeks ago I had to go through and correct a bunch of unit tests that I had written where I had the params reversed. Having them reversed didn't bite me in the sense that you discussed, but it was just wrong and bothered me.

# re: Please Use Assert.AreEqual Correctly

Gravatar left by Kevin U at 12/21/2007 1:05 AM
Yeah, I also like the AreEqual as opposed to the Assert.IsTrue(obj1 == obj2,"Objets are not equal");
Comments have been closed on this topic.