繁体   English   中英

如何在 SpecFlow 中使用 Nlog 进行登录?

[英]How to log with Nlog in SpecFlow?

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扩展。

它通过SpecFlow.xUnit生成的xunit测试工作

问题可能是 NLog 找不到它的配置。

运行单元测试时,会移动 dll 而不是 nlog.config

有多种解决方案:

从固定路径加载配置,例如

LogManager.Configuration = new XmlLoggingConfiguration("c:/mydir/nlog.config");

或者从代码而不是配置进行设置,例如:

var config = new LoggingConfiguration();
config.AddRuleForAllLevels(new FileTarget()
{
    FileName = "c:/temp/logfile.log"
});
LogManager.Configuration = config; //apply config

维基

如果这仍然是一个问题,请检查内部日志

我用这个解决方法在我的框架中做了类似的事情:

  • 添加钩子 [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.");
        }

暂无
暂无

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

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