Sean
Sean

Reputation: 907

C# test always return true even when test fails

Before when I was running my tests I had set up my code to look like the following:

private bool ValidateTestOne(EntityModel.MultiIndexEntities context)
{
    if (context.SearchDisplayViews.Count() != expectedSdvCount)
    {
        Assert.Fail("Search Display View count was different from what was expected");
    }

    if (sdv.VirtualID != expectedSdVirtualId)
    {
        Assert.Fail("Search Display View virtual id was different from what was expected");
    }

    if (sdv.EntityType != expectedSdvEntityType)
    {
        Assert.Fail("Search Display View entity type was different from what was expected");
    }

    return true;
}

This would also return the correct path telling me if a test passed or failed. Since then, and after some advice, I changed my code so it look like the following:

private bool ValidateTestOne(EntityModel.MultiIndexEntites  context)
{
    Assert.AreEqual(expectedEntityCount, context.Entities.Count(),
        "Entity Count was different from what was expected");

    return true;
}

My thinking on using the new Assert.AreEqualis that it would still return back true or false depending if it passed or failed. But now my test is always returning true because I have no way of working out if my test failed or not.

All this is determined by called the following line in my main test method:

Assert.IsTrue(test4PassFail = ValidateTest("4.4"), "Test 4 ");

Is there a way I can ammend my new code to return true or false without having to revert back to my old code?

Upvotes: 1

Views: 1470

Answers (3)

PiousVenom
PiousVenom

Reputation: 6918

Instead of doing return true;, you could do Assert.AreEqual(expectedEntityCount, context.Entities.Count()");.

This would be your fail/pass. You don't need to do a return. But as it is, you're returning true all the time.

[TestClass]
public class Tests
{
     [TestMethod]
     private void ValidateTestOne(EntityModel.MultiIndexEntites  context)
     {
          Assert.AreEqual(expectedEntityCount, context.Entities.Count(), "Entity Count was different from what was expected");
     }
}

Upvotes: 4

user2831805
user2831805

Reputation:

in the first one you're always returning true because thats what you set it to return.

you should consider making them nested if else statements

private bool ValidateTestOne(EntityModel.MultiIndexEntities context)
  {
     if (context.SearchDisplayViews.Count() != expectedSdvCount)
        {
              Assert.Fail(" Search Display View count was different from what was expected");
//does it return true or false?
//return true or false

        } else if (sdv.VirtualID != expectedSdVirtualId){

             Assert.Fail(" Search Display View virtual id was different from what was expected");

//again true or false?

        } else if(sdv.EntityType != expectedSdvEntityType) {
           Assert.Fail(" Search Display View entity type was different from what was expected");
//true or false?

        }

  }

Upvotes: 0

Mike Weber
Mike Weber

Reputation: 311

You need to follow the MSTest way of doing tests. Your class needs to have the [TestClass] attribute, and your methods need [TestMethod]. The methods within the test class are all void. Your methods are returning true regardless of the assertions. VS allows you to add a test project and test classes to support your code. It will add in the appropriate attributes for you.

[TestClass]
public class Tests
{
   [TestMethod]
   public void DriveInfoTest()
   {
       // set up
       DriveUnit uut = new DriveUnit();

       // run
       var result = uut.GetInfo();


      // verify
      Assert.IsNotNull(result, "Get Info did not return an object.");
   }

}

Upvotes: 3

Related Questions