holyredbeard
holyredbeard

Reputation: 21178

Unit-test fail when it shouldn't do

I'm working on unit-tests for an application and below is a unit-test I just cant figure out why it fails.

The test result should be that "isosceles" is true and "equalateral" and "scalene" is false, which Console.WriteLine shows that they actually are, but by some strange reason the test fails anyhow. What am I doing wrong here? Thanks in advance.

I'm using the MSTest framework.

    public void isIsosceles()
    {
        Triangle triangle = new Triangle(2, 2, 5);

        var isosceles = triangle.isIsosceles();
        var equalateral = triangle.isEquilateral();
        var scalene = triangle.isScalene();

        Console.WriteLine(isosceles);       // True
        Console.WriteLine(equalateral);     // False
        Console.WriteLine(scalene);         // False

        Assert.IsTrue(true, "Test Isosceles", isosceles);
        Assert.IsTrue(false, "Test Equalateral", equalateral);    // Fails, why?
        Assert.IsTrue(false, "Test Scalene", scalene);            // Fails, why?
    }

Upvotes: 3

Views: 4546

Answers (5)

Christian Hayter
Christian Hayter

Reputation: 31071

The assert statements are used is a wrong way. This would be correct:

    Assert.IsTrue(isosceles, "Test Isosceles");
    Assert.IsFalse(equalateral, "Test Equalateral");
    Assert.IsFalse(scalene, "Test Scalene");

Upvotes: 0

Tudor
Tudor

Reputation: 62439

From the MSDN of Assert.IsTrue Method (Boolean, String, Object[]):

Verifies that the specified condition is true. The assertion fails if the condition is false.

Obviously Assert.IsTrue(false,...) will fail.

I think you meant:

Assert.IsTrue(equalateral == false, "Test Equalateral", equilateral);

Upvotes: 1

Fortunato
Fortunato

Reputation: 1350

for the false cases you have to do Assert.isFalse(equalateral... and similarly with scalene

Upvotes: 0

McKay
McKay

Reputation: 12604

Assert.IsTrue checks to see if the first parameter is true.

What you probably want is:

    Assert.IsTrue(isosceles, "Test Isosceles");
    Assert.IsFalse(equalateral, "Test Equalateral"); 
    Assert.IsFalse(scalene, "Test Scalene");     

But what you might be meaning is:

    Assert.AreEqual(true, isosceles, "Test Isosceles");
    Assert.AreEqual(false, equalateral, "Test Equalateral");
    Assert.AreEqual(false, scalene, "Test Scalene"); 

Upvotes: 2

SLaks
SLaks

Reputation: 887195

You're mis-calling IsTrue.
The first parameter is the boolean to test:

Assert.IsFalse(equalateral, "Test Equalateral");    

You can also call AreEqual:

Assert.AreEqual(false, equalateral, "Test Equalateral")

Upvotes: 6

Related Questions