SpecFlow将其输出写入Console,如下所示:
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>();
}
}
它不起作用。我知道有能力添加plugins,但这似乎有太大的开销。
我们还使用了ObjectivityLtd Test.Automation个扩展程序。
生成的xunit测试工作答案 0 :(得分:0)
问题可能是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
请参见wiki
如果仍然存在问题,请检查internal log
答案 1 :(得分:0)
我通过以下解决方法在框架中完成了类似的操作:
这里是示例: 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.");
}