c# - 如何在 SpecFlow 中使用 Nlog 登录?
问题描述
SpecFlow 将其输出写入控制台,如下所示:
Given the "TestOperator" user is logged in
-> done: WebUserSteps.GivenTheUserIsLoggedIn("TestOperator", "") (9.5s)
我们如何让它使用 NLog 来配置它应该写入的位置?
有了这个:
public class CustomListener : ITraceListener
{
private Logger Log = LogManager.GetLogger("SPECFLOW");
public void WriteTestOutput(string message)
{
Log.Trace(message);
}
public void WriteToolOutput(string message)
{
Log.Trace(message);
}
}
和
[Binding]
public class ScenarioContextInitializer
{
private readonly IObjectContainer _container;
public ScenarioContextInitializer(ScenarioContext scenarioContext)
{
_container = (scenarioContext ?? throw new ArgumentNullException(nameof(scenarioContext))).ScenarioContainer;
}
[Before]
protected void Load()
{
_container.RegisterTypeAs<CustomListener, ITraceListener>();
}
}
它没有用。我知道可以添加插件,但这似乎开销太大。
我们还使用ObjectivityLtd Test.Automation扩展。
解决方案
我用这个解决方法在我的框架中做了类似的事情:
- 添加钩子 [AfterStep],使用步骤的名称调用 Console.WriteLine() [或您的记录器],如果通过与否(如果测试错误!= null,这意味着失败,通过)
请注意,这很完美在并行执行中。(正确的输出到每个测试)
这是示例: https ://github.com/LirazShay/SpecFlowDemo/blob/master/src/SpecFlowDemo/Hooks.cs
像这样的东西:
[AfterStep]
public void LogStepResult()
{
string stepText = StepContext.StepInfo.StepDefinitionType + " " + StepContext.StepInfo.Text;
Console.WriteLine(stepText);
var stepTable = StepContext.StepInfo.Table;
if (stepTable != null && stepTable.ToString() != "") Console.WriteLine(stepTable);
var error = ScenarioContext.TestError;
Console.WriteLine(error != null ? "-> error: " + error.Message : "-> done.");
}
推荐阅读
- php - 如何在javascript中获取ajax json循环响应
- excel - 仅在特定日期的用户表单
- html - HTML标签内*内的额外空间会破坏某些浏览器吗?
- konva - 自定义形状的Konva序列化和反序列化
- spring-mvc - HTTP 状态 400 – 向我的控制器添加 @Valid 注释时显示错误请求
- javascript - 在 Firebase 数据库中的值之间获取
- javascript - jQuery - 按钮单击触发 ajax 调用,但让它们按照触发顺序依次完成
- sql - 如何在 Oracle SQL 中有效地替换 XML 中的特殊字符?
- javascript - 如何在 Node.js 上的控制器或外部文件上使用 Web 套接字?我无法访问 io 对象
- docker - 在特定时间启动 docker 容器的最佳实践