Are logical operators in assertions acceptable?

pappati

What do you think about the following assertion?

Assert.IsTrue(condition1 && condition2);

I came up with this type of assertions during a review session. As far as I see using && operator in an assertion is not a good practice. I am wondering what others think? Is it worth mentioning this is an issue during the next review sessions? Or it is just a personal preference?

Sergey Berezovskiy

When you write

Assert.IsTrue(condition1 && condition2);

Then it's not clear what caused test to fail - first or second condition. Thus you need both of them to be true just check one after another:

Assert.IsTrue(condition1);
Assert.IsTrue(condition2);

Now let's think what's wrong with OR conditions:

Assert.IsTrue(condition1 || condition2);

This looks like random test - it will pass in different scenarios:

  1. first condition is true and second is false
  2. if both are true
  3. if second is true but first is false

So, what scenario you are checking exactly? Create three different scenarios which reproduce this three situations. Each tests will contain two asserts, e.g. for last scenario:

Assert.IsFalse(condition1);
Assert.IsTrue(condition2);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related