Why are there so many seemingly redundant methods in the Assert class? When should everyone be used?

So, I see that Assert has dozens of methods that seem to do the same thing.

Assert.IsFalse( a == b ); Assert.IsTrue( a != b ); Assert.AreNotEqual( a, b ); 

Why? Is this more explicit? When should you use various methods? Is there an official best practice document?

+6
c # assert unit-testing
source share
6 answers

The difference between IsFalse and IsTrue is readability. AreNotEqual allows you to get the best error message when a test AreNotEqual . IsTrue , for example, will simply tell you that the answer was supposed to be true and was really false. AreNotEqual display two values ​​that were mapped in the error message.

+8
source share

Short answer: for readability.

Slightly long answer:

Your tests are also code, and in terms of intent, they are important, like the code you are testing. Thus, you want to make the goal of the test as clear as possible. Sometimes it means you are using IsFalse , sometimes it means using IsTrue .

+8
source share

These three methods have three different specific goals. The purpose of testing is to provide clear verification and verification of your code. Using the most understandable and specific method, you make your test the smallest, possibly the most concrete, understandable meaning.

This helps, because it adds clarity - you can see, in particular, what the test should do in a more declarative form, where using the same method for several test scripts, each of which has a different meaning, requires more understanding of the code itself, not the nature of the test.

In this case, the third is (only) suitable for use. However, if you had this situation, you would use a different one, for example:

 Assert.IsTrue( myClass.MethodThatReturnsTrue() ); 

You should use a method that provides the most clarity regarding your goal - if you check two values ​​for equality, use Assert.IsEqual , if you check a boolean to make sure it is false, use Assert.IsFalse , This makes error messages meaningful and understandable.

+4
source share

This makes reading errors easier; Use the ones that most closely match your code.

In this case, use # 3.

+1
source share

Because you can overload == and! =, that's why.

+1
source share

He called the "Free Interface" ... and makes things more readable, according to many.

+1
source share

All Articles