Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 379
ediblecode Avatar asked Nov 03 '11 16:11

ediblecode


1 Answers

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.

like image 112
SLaks Avatar answered Sep 22 '22 14:09

SLaks