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);
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.
使用Assert.IsTrue
更清晰,更Assert.IsTrue
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.