So I see that Assert has dozens of methods that seem to do essentially the same thing.
Assert.IsFalse( a == b );
Assert.IsTrue( a != b );
Assert.AreNotEqual( a, b );
Why? Is it just to be more explicit? When should the various methods be used? Is there an offical best practices document?
The assert() method tests if a given expression is true or not. If the expression evaluates to 0, or false, an assertion failure is being caused, and the program is terminated. The assert() method is an alias of the assert.
An assert is inappropriate because the method guarantees that it will always enforce the argument checks. It must check its arguments whether or not assertions are enabled. Further, the assert construct does not throw an exception of the specified type. It can throw only an AssertionError .
assertEquals. Asserts that two objects are equal. If they are not, an AssertionError without a message is thrown. If expected and actual are null , they are considered equal.
A triggered assertion indicates that the object is definitely bad and execution will stop.
The difference between IsFalse
and IsTrue
is readability. AreNotEqual
allows a better error message to be presented when the test fails. IsTrue
for example will just tell you that the answer was supposed to be true and was really false. AreNotEqual
will show the two values that were compared in its error message.
Short answer: For readability.
Slightly longer answer:
Your tests are also code, and in terms of intent are as important as the code you are testing. As such, you want to make the intent of the test as clear as possible. Sometimes that means you use IsFalse
, sometimes it means using IsTrue
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With