0

我使用 specflow/xunit 为日志消息和范围报告实现了 log4net。如果发生任何错误/异常,并且我没有 log4net 的 try/catch 语句,则范围报告会捕获异常并将其作为报告中的失败步骤打印出来。但是,如果我有 try 和 catch 语句,并且 log4net 正在捕获异常,则范围报告不会将其记录为失败的步骤,并且会执行与通过的步骤相同的步骤,但它应该失败。

我怎样才能使范围报告认为 log4net 已捕获异常/错误并且此步骤必须失败。

下面是我的 try/catch 语句,该语句将失败

public void ClickonLoginButton()
{
try{
      ClickonElement(LoginbuttonLocator);
      Log.info("successfully clicked");
}
catch(exception ex){
Log.error("unable to click");
Console.WriteLine(ex.StackTrace);
}
}

我的范围报告后续代码:

 [AfterStep]
        public void InsertReportingSteps()
        {

            var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();

            if (ScenarioContext.Current.TestError == null)
            {
                if (stepType == "Given")
                    _scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text);
                else if (stepType == "When")
                    _scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text);
                else if (stepType == "Then")
                    _scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text);
                else if (stepType == "And")
                    _scenario.CreateNode<And>(ScenarioStepContext.Current.StepInfo.Text);
            }
            else if (ScenarioContext.Current.TestError != null)
            {
                if (stepType == "Given")
                    _scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.InnerException);
                else if (stepType == "When")
                    _scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.InnerException);
                else if (stepType == "Then")
                {
                    string Runname = screenshot();
                    _scenario.AddScreenCaptureFromPath("C:\\Users\\xxxx- PC\\Desktop\\XUnit_Assignement_3\\XUnit_Assignement\\target\\ErrorScreenshots\\" 
                        + Runname + ".Png");
                    _scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text).
                        Fail(ScenarioContext.Current.TestError.Message);
                    _scenario.Fail("Failed Because of Some issues",
                        MediaEntityBuilder.CreateScreenCaptureFromPath(@"C:\Users\xxxx- PC\Desktop\XUnit_Assignement_3\XUnit_Assignement\TestResults\image.JPG", 
                        "Failed").Build());
                }
            }
4

1 回答 1

1

使用 try/catch 时会发生什么:

它与 log4net 无关,但与您捕获异常有关。如果你不这样做,异常将通过堆栈传递,直到有东西捕获它或它最终进入全局处理程序。如果您确实在代码中捕获它,它将停在那里。它将被视为“已处理”,并且控制流在 catch 块之后继续移动。

因此,您的报表引擎不会“看到”有异常,因为您在它到达引擎将捕获它的地方之前捕获它。

如何让引擎意识到异常:

您需要重新抛出异常:

catch(Exception ex){
    Log.error("unable to click");
    Console.WriteLine(ex.StackTrace);
    throw;
}

然后它会像往常一样冒泡。捕捉它会阻止它进一步冒泡 - 因为它预计会在 catch 块中进行相应的处理。如果您仅将 catch 块用于日志记录,那么您需要再次抛出相同的异常。

注意:throw;和之间有区别throw ex;。他们将产生不同的堆栈跟踪。throw;将保留堆栈跟踪,同时throw ex将其重置。(见https://stackoverflow.com/a/730255/982149

于 2019-01-15T12:56:30.420 回答