我使用 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());
}
}