当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Visual Studio 2010 Unit Testing - Any way to continue a TestMethod after failed assertion?

问题描述:

I have a test project in visual studio 2010. I have a TestMethod. Inside of this, I want to iterate over a list of things and test each. So, I have 1 test and want to assert N times (once for each item in the list).

However, I don't want to stop if one fails. I want to continue and then report all failures together.

Example:

[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;

}

网友答案:

I would do something like this:

// 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);
}
网友答案:

You can try Tom's suggestion and instead of

foreach (string failure in failures)
{
   Console.WriteLine(failure);
}

do

var errorMessage = failures.Aggregate((current, next) => current + ", " + next);
Assert.AreEqual(0, failures.Count, errorMessage);

BTW the fail method is supposed to contain the logic that detects a failure in an item

分享给朋友:
您可能感兴趣的文章:
随机阅读: