[英]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.AreEqual
is that it would still return back true or false depending if it passed or failed. 我对使用新的
Assert.AreEqual
想法是,它仍然会返回true或false,这取决于它是通过还是失败。 But now my test is always returning true because I have no way of working out if my test failed or not. 但是现在我的测试总是返回true,因为如果测试失败或失败,我将无法解决。
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? 有没有办法我可以修改新代码以返回true或false,而不必还原到旧代码?
Instead of doing return true;
而不是
return true;
, you could do Assert.AreEqual(expectedEntityCount, context.Entities.Count()");
. ,则可以执行
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. 但实际上,您一直在返回
true
。
[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");
}
}
You need to follow the MSTest way of doing tests. 您需要遵循MSTest进行测试的方法。 Your class needs to have the [TestClass] attribute, and your methods need [TestMethod].
您的类需要具有[TestClass]属性,而您的方法则需要[TestMethod]。 The methods within the test class are all void.
测试类中的方法都是无效的。 Your methods are returning true regardless of the assertions.
无论断言如何,您的方法都将返回true。 VS allows you to add a test project and test classes to support your code.
VS允许您添加测试项目和测试类以支持您的代码。 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.");
}
}
in the first one you're always returning true because thats what you set it to return. 在第一个中,您总是返回true,因为这就是您将其设置为返回的内容。
you should consider making them nested if else statements 您应该考虑将它们嵌套在if语句中
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?
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.