我在visual studio 2010中有一个测试项目。我有一个TestMethod。在此内部,我想迭代一系列事物并测试每个事物。所以,我有一个测试,并想要断言N次(列表中的每个项目一次)。
但是,如果一个人失败,我不想停止。我想继续,然后一起报告所有失败。
示例:
[TestMethod]
public void Test()
{
foreach (item in list)
{
// if fail, continue on with the rest
Assert(if fail, add to output list);
}
output_failures_all_at_once;
}
答案 0 :(得分:3)
我会做这样的事情:
// Assert that each item name is fewer than 8 characters.
[TestMethod]
public void Test()
{
List<string> failures = new List<string>();
// However you get your list in the first place
List<Item> itemsToTest = GetItems();
foreach (Item item in itemsToTest )
{
// if fail, continue on with the rest
if (item.Name.Length > 8 )
{
failures.Add(item.Name);
}
}
foreach (string failure in failures)
{
Console.WriteLine(failure);
}
Assert.AreEqual(0, failures.Count);
}
答案 1 :(得分:0)
您可以尝试Tom的建议,而不是
foreach (string failure in failures)
{
Console.WriteLine(failure);
}
DO
var errorMessage = failures.Aggregate((current, next) => current + ", " + next);
Assert.AreEqual(0, failures.Count, errorMessage);
顺便说一句,失败方法应该包含检测项目失败的逻辑