我试图重新运行失败的案例,如下所示,但在第二次运行时将测试案例标记为传递,在Assert.Pass()步骤中获得以下异常。如何解决此错误?
例外:
NUnit.Framework.SuccessException
at NUnit.Framework.Assert.Pass(String message, Object[] args)
at NUnit.Framework.Assert.Pass(String message)
at CodedUI.NUnit2.TestMethod() in NUnit2.cs:line 80
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
代码:
public void TestMethod()
{
run++;
if (run == 1)
{
Assert.Fail();
}
Console.WriteLine("Rerun..");
Assert.Pass();
}
重新进行清理,如下所示。
[TearDown]
public void cleanup()
{
if (TestContext.CurrentContext.Result.Outcome.Status==TestStatus.Failed)
{
var type = Type.GetType(TestContext.CurrentContext.Test.ClassName);
if (type != null)
{
var instance = Activator.CreateInstance(type);
var method = type.GetMethod(TestContext.CurrentContext.Test.MethodName);
try
{
method.Invoke(instance, null);
}
catch(Exception e)
{
}
}
}
}
答案 0 :(得分:0)
这不起作用,因为NUnit会处理每个异常,包括它自己在TearDown阶段中的错误。在TearDown时,已经记录了测试结果。