Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the "Assert" class have so many seemingly redundant methods? When should each be used?

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?

like image 958
Catskul Avatar asked Feb 22 '10 19:02

Catskul


People also ask

What is the use of assert method?

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.

Is it good practice to use assert in Java?

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 .

Why we use assert assertEquals?

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.

Does assert stop execution?

A triggered assertion indicates that the object is definitely bad and execution will stop.


2 Answers

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.

like image 96
Instance Hunter Avatar answered Oct 08 '22 06:10

Instance Hunter


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.

like image 45
Oded Avatar answered Oct 08 '22 07:10

Oded