我认为没有办法通过 VS 或 MTM 来做到这一点。将所有测试方法添加到单个测试方法的选项听起来不错,但是当其中一个测试方法失败时,“父”测试方法会停止并抛出内部测试之一抛出的“AssertFailedException”。
但是,如果您的测试方法(a、b、c...)彼此完全不受影响(这意味着如果 testMethodA 失败,其他测试可以毫无问题地运行),我会尝试捕获所有内部异常并最后打印哪些方法通过,哪些不通过。
[TestClass]
public class TestClass
{
Dictionary<string, string> testMethods;
bool testResult;
[TestInitialize]
public void TestInitialize()
{
testMethods = new Dictionary<string, string>();
testResult = true;
}
[TestMethod]
public void TestMethod()
{
//Run TestMethodA
try
{
TestMethodA();
testMethods.Add("TestMethodA", "Passed");
}
catch (AssertFailedException exception) //Edit: better catch a generic Exception because CodedUI tests often fail when searcing for UI Controls
{
testResult = false;
testMethods.Add("TestMethodA", "Failed: " + exception.Message);
}
//Run TestMethodB
try
{
TestMethodB();
testMethods.Add("TestMethodB", "Passed");
}
catch (AssertFailedException exception)
{
testResult = false;
testMethods.Add("TestMethodB", "Failed: " + exception.Message);
}
}
[TestCleanup]
public void TestCleanup()
{
foreach (KeyValuePair<string, string> testMethod in testMethods)
{
//Print the result for each test method
TestContext.WriteLine(testMethod.Key.ToString() + " --> " + testMethod.Value.ToString());
}
//Assert if the parent test was passed or not.
Assert.IsTrue(testResult, "One or more inner tests were failed.");
}
}
您还可以创建一个不同的类来管理所有这些行为,以避免所有这些“try-catch”。