GrizzlyBear
GrizzlyBear

Reputation: 35

How is this unit test passing when exercised?

I have made two variables of which both are not equal, I have created a if statement for the method and on visual studio it still passes, only way I got it to fail was if I removed the if statement completely and then used

Assert.IsTrue(a == b); 

But if I try using it through if statements it passes for some reason

[TestClass]
public class SectionQuiz
{
   static int a;         
    static int  b;              

    [ClassInitialize]
    public static void IntegerInitalize(TestContext testContext)
    {
        a = 10;  // set variables and a != b
        b = 5;
    }

    [TestMethod]
    public void Number1_isnotequalto_number2()
    {
        if (a == b)
        {
            Assert.IsTrue(a==b);
        }
    }
}

Upvotes: 1

Views: 69

Answers (1)

dodgy_coder
dodgy_coder

Reputation: 13063

The if statement has nothing to do with it passing or not. The unit test Number1_isnotequalto_number2 isn't asserting anything so it will always pass.

Add this line to the end of the unit test Number1_isnotequalto_number2 and outside of the if statement.

Assert.AreEqual(a, b);

By adding this line, the unit test should always fail, given your initialization.

Another option - if you want to combine if statements with asserts, but its normally not done like this...

if (a != b)
  Assert.Fail("they are not equal");
// else
// There is no Assert.Pass - just return and it passes

Upvotes: 2

Related Questions