在我运行测试之前,我已将代码设置为如下所示:
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;
}
这也会返回正确的路径,告诉我测试是通过还是失败。从那时起,经过一些建议,我改变了我的代码,看起来如下:
private bool ValidateTestOne(EntityModel.MultiIndexEntites context)
{
Assert.AreEqual(expectedEntityCount, context.Entities.Count(),
"Entity Count was different from what was expected");
return true;
}
我对使用新Assert.AreEqual
的想法是,它仍将返回true或false,具体取决于它是通过还是失败。但是现在我的测试总是返回true,因为如果我的测试失败了,我就无法解决问题。
所有这些都是通过在我的主要测试方法中调用以下行来确定的:
Assert.IsTrue(test4PassFail = ValidateTest("4.4"), "Test 4 ");
有没有办法可以修改我的新代码以返回真或假,而不必回复我的旧代码?
答案 0 :(得分:4)
而不是return true;
,您可以Assert.AreEqual(expectedEntityCount, context.Entities.Count()");
。
这将是你的失败/通过。你不需要做出回报。但事实上,你一直都在返回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");
}
}
答案 1 :(得分:3)
您需要遵循MSTest的测试方式。您的类需要具有[TestClass]属性,并且您的方法需要[TestMethod]。测试类中的方法都是无效的。无论断言如何,您的方法都返回true。 VS允许您添加测试项目和测试类以支持您的代码。它会为您添加适当的属性。
[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.");
}
}
答案 2 :(得分:0)
在第一个中你总是返回true,因为那是你设置它返回的内容。
如果是else语句,你应该考虑将它们嵌套
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?
}
}