[英]C#- Print an additional message to the the actual specflow error
我正在嘗試使用如下所示的 AfterStep 掛鈎向實際的 specflow 錯誤消息添加一條附加消息,但它不起作用,有人可以建議更好的方法。
[AfterStep]
public void AfterStep()
{
if (this.scenarioContext.ScenarioExecutionStatus == ScenarioExecutionStatus.TestError)
{
var stepName = this.scenarioContext.StepContext.StepInfo.Text;
var stepTable = this.scenarioContext.StepContext.StepInfo.Table;
var errorLength = this.scenarioContext.TestError.Message.Length;
this.scenarioContext.TestError.Message.Insert(errorLength, "\r\n Failed at Step: " + stepName + "\r\n" + stepTable + "\r\n");
}
}
我試圖拋出另一個自定義異常以從 AfterStep 掛鈎內部打印我的附加消息,但這會更改原始堆棧跟蹤。
這個答案並不完全是您要問的,但我希望它能體現您要實現的目標的精神。
在這種情況下,我的首選做法是使用SpecFlow Output API來添加更多關於失敗的上下文。
長格式的說明在上面的鏈接中,但您可以將ISpecFlowOutputHelper
注入到您的步驟定義類中。 這將允許您在給定步驟中在 SpecFlow output 中寫入其他行。
通常,我會使用try/catch
語句來捕獲步驟執行期間發生的異常,然后我會使用ISpecFlowOutputHelper
的WriteLine
方法來打印一些附加信息。
這允許您在發生的步驟中打印上下文,我發現這在審查失敗的場景時很有用。
我實施了這個來修復它。 這會打印出我想要打印的消息,還會顯示實際的堆棧跟蹤。
[AfterStep]
public void AfterStep()
{
if (this.scenarioContext.ScenarioExecutionStatus == ScenarioExecutionStatus.TestError)
{
var errorMessage = this.scenarioContext.TestError.Message;
var stepName = this.scenarioContext.StepContext.StepInfo.Text;
var stepTable = this.scenarioContext.StepContext.StepInfo.Table;
var exception = this.scenarioContext.TestError.GetBaseException();
exception.GetType().GetField("_message", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(exception, "Failed at Step: \r\n" + stepName + "\r\n" + stepTable + "\r\n Actual Error: \r\n" + errorMessage);
var ex = ExceptionDispatchInfo.Capture(exception);
ex.Throw();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.