views:

123

answers:

4

Isn't it true that every assert statement can be translated to an Assert.IsTrue, since by definition, you are asserting whether something is true or false?

Why is it that test frameworks introduce options like AreEquals, IsNotNull, and especially IsFalse? I feel I spend too much time thinking about which Assert to use when I write unit tests.

+2  A: 

Yes,

The AreEqual(obj1, obj2) basically does a Assert.IsTrue(obj1.Equals(obj2)). So, I would say this is true.

But perhaps they introduce those overloads for readability, per example make it obvious they want to compare two objects, or check if a value is equal to false.

Meiscooldude
+3  A: 

You can use Assert.IsTrue all the time if you prefer. The difference is Assert.AreEqual and the like will give you a better error message when the assertion fails.

NUnit (and probably other frameworks) now supports syntax like this:

Assert.That(foo, Is.Equal.To(bar))

Matt Greer
Agreed. But couldn't an IsTrue override be provided for better error messages?
Prabhu
@Prabhu What do you override? IsTrue takes a boolean. What would IsTrue( string s ) do? And how does it know you are testing for equality or something? Assert.IsTrue( a < b ). Do you mean overloaded? IsTrue( bool condition, string message )? Now you have to type 'a and b are not equal' or something everytime you have a test that tests for equality.
BioBuckyBall
There typically is an IsTrue(bool condition) override: IsTrue(bool condition, string errormessage). It's just way shorter to is AreEqual(foo, bar) than IsTrue(foo.Equals(bar), string.Format("Expected: {0}\nGot: {1}", foo, bar));Besides, by that logic the whole Assert class is unneeded because you could just throw an exception yourself, since that's essentially what Assert does:if (foo != bar) throw new Exception ("Foo doesn't equal Bar");
Hounshell
Not to mention `AreEqual` deals with nulls gracefully.
Matt Greer
I meant overload.
Prabhu
+4  A: 

Given enough extra code on your part, yes it's true that almost every Assent.XXX can be turned into an Assert.IsTrue call. However there are some which are very difficult to translate like Throws

Assert.Throws<ArgumentNullException>(() => x.TestMethod(null));

Translating that to an Assert.IsTrue is possible but really not worth the effort. Much better to use the Throws method.

JaredPar
+1  A: 

It is very simple - to make your test code more readable.

Which is more readable

Assert.IsTrue(quantity > 0)

or

Assert.That(quantity, Is.GreaterThan( 0 ))

Don't you think the second option is more readable?

P.K