简体   繁体   English

如何在重试时覆盖NUnit测试结果

[英]How to override NUnit test results on retry

I was trying to rerun failed case as below but while marking test case as pass on 2nd run getting below exception at Assert.Pass() step. 我试图重新运行失败的情况,如下所示,但在第二次运行时将测试用例标记为传递,在Assert.Pass()步骤中获得以下异常。 How to resolve this error? 如何解决此错误?

Exception: 例外:

 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)

Code: 码:

public void TestMethod()
        {

            run++;

            if (run == 1)
            {
                Assert.Fail();
            }
            Console.WriteLine("Rerun..");
            Assert.Pass();

        }

Retrying in clean up as below. 重试如下清理。

 [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)
                {

                }
            }
        }
    }

This can't work because NUnit treats every exception, including its own as an error in the TearDown phase. 这不起作用,因为NUnit会处理每个异常,包括它自己在TearDown阶段中的错误。 At the time of TearDown, the result of the test is already recorded. 在TearDown时,已经记录了测试结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM