2013-03-05 159 views
1

是否有一种方法来指定消息Assert.AreEqual(object, object, string)将自定义消息与默认消息相结合?自定义消息和默认消息

我有以下代码:

foreach (var testCase in testCases) 
{ 
    Assert.AreEqual(testCase.Value, myObj.myMethod(testCase.Key), combinedMessage); 
} 

我想指定除了从下面的VS单元测试框架的示例消息测试用例键:

Assert.AreEqual failed. Expected:<True>. Actual:<False>.

也许像Failed on the following test case: AB

+0

广东话您连接2个字符串? – Blachshma 2013-03-05 17:07:37

+0

Assert.AreEqual失败不是静态的。我想使用框架中出现的任何内容。 – 2013-03-05 17:11:24

回答

3

重载将自动为您完成此操作。作为测试我在此测试方法来看到的输出将是什么:

[TestMethod] 
    public void Test() 
    { 
     Assert.AreEqual(true, false, "Failed on the following test case: AB"); 
    } 

以及错误消息输出是:Assert.AreEqual failed. Expected:<True>. Actual:<False>. Failed on the following test case: AB

该消息参数已经被附加/组合以缺省消息。

对于你的情况,如果你只是想测试项的测试可能看起来像:

foreach (var testCase in testCases) 
{ 
    Assert.AreEqual(testCase.Value, myObj.myMethod(testCase.Key), 
     "Failed on the following test case: " + testCase.Key.ToString()); 
} 

而且如果测试用例应该每个人都有自己的自定义消息,它会接着是有意义的移动自定义错误消息给testCase类。作为创建每个对象的一部分可以接着指定这些三个属性:

testCase.Value = true; 
testCase.Key = "AB"; 
testCase.FailureMessage = "Failed on the following test case: AB"; 

这种类型的结构将允许具有要追加用于测试用例的每个实例指定消息。这样做将使单元测试看起来像这样:

foreach (var testCase in testCases) 
{ 
    Assert.AreEqual(testCase.Value, myObj.myMethod(testCase.Key), 
     testCase.FailureMessage)); 
} 

,并在您的示例输出显示为:Assert.AreEqual failed. Expected:<True>. Actual:<False>. Failed on the following test case: AB

+0

这对我来说非常合适。谢谢。 – 2013-03-05 21:48:06

0

难道你不只是连接信息?

foreach (var testCase in testCases) 
{ 
    string message = string.Format("{0}: {1}", defaultMessage, testCase.Key); 
    Assert.AreEqual(testCase.Value, myObj.myMethod(testCase.Key), message); 
} 
+0

我宁愿不硬编码默认消息,特别是它可能随测试用例而改变。 – 2013-03-05 17:11:53