【发布时间】:2015-06-01 11:47:21
【问题描述】:
我正在尝试使用反射从控制台应用程序运行 nunit 测试用例。我得到一个我的 catch 块没有处理的异常。您能否提供一个建议,如何处理调用的测试方法中的所有异常?
static void Main(string[] args)
{
// Take all classes of the current assebly to which TestFixture attribute is applied
var testClasses = Assembly.GetExecutingAssembly().GetTypes().Where(c =>
{
var attributes = c.GetCustomAttributes(typeof(TestFixtureAttribute));
return attributes.Any();
});
foreach (var testClass in testClasses)
{
var testMethods = testClass.GetMethods().Where(m =>
{
var attributes = m.GetCustomAttributes(typeof (TestAttribute));
return attributes.Any();
});
var instance = Activator.CreateInstance(testClass);
foreach (var method in testMethods)
{
try
{
Action action = (Action) Delegate.CreateDelegate(typeof (Action),
instance, method);
action();
}
catch (AggregateException ae)
{
Console.WriteLine(ae.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
【问题讨论】:
-
聚合异常发生在try块中,未被捕获。
-
异常的stacktrace是什么,是什么类型,异常的信息是什么?
标签: c# exception-handling nunit