ediblecode
ediblecode

Reputation: 11971

Assert.AreEqual vs Assert.IsTrue/Assert.IsFalse

When testing a method that is of return type bool.

Should you have:

expected = true;
Assert.AreEqual(expected, actual);

or

Assert.IsTrue(actual);

I know they both produce the same outcome, but which is better practise to use?

EDIT: For example, if I do AreEqual, is it not essentially the same as doing IsTrue on a method that returns a string a la below:

string expected = “true”;
string actual = test.testMethod(data)
bool test;

if expected.equals(actual)
    test = true;
else 
    test = false;
Assert.IsTrue(test);

Upvotes: 12

Views: 33329

Answers (2)

SLaks
SLaks

Reputation: 888077

You should only use Assert.IsTrue if you're testing something which directly returns a boolean that should always be true.

You should not massage data to get a boolean for IsTrue; instead, you should call a more relevant method in Assert or CollectionAssert.

In your edited example, you should by all means call Assert.AreEqual instead; it will give you a much nicer message.

Upvotes: 25

SLaks
SLaks

Reputation: 888077

Using Assert.IsTrue is clearer and less verbose.

Upvotes: 17

Related Questions