如何使用SpecFlow设置单个跟踪/日志记录 [英] How to set up individual tracing / logging with SpecFlow
问题描述
对于我的SpecFlow测试,我想在执行测试过程中为测试进度设置一个单独的日志记录/跟踪记录.例如.我想写
For my SpecFlow tests, I want to setup an individual logging / tracing of the test progress during the execution of tests. E.g. i want to write
- 所有通过/失败的步骤
- 开始/结束场景
- 开始/结束功能
到Windows事件日志(以使其与测试期间其他系统组件生成的事件日志消息同步).
to the windows event log (in order to synchronize it with event log messages generated by other system components during the test).
我尝试使用[BeforeFeature],[BeforeScenario],[BeforeStep]钩子来执行此操作,但是事实证明,我在此钩子中没有所有必需的信息.例如.我不知道如何执行当前步骤的当前文本行(包括行信息等)或当前步骤的结果(失败/通过).
I tried to use the [BeforeFeature], [BeforeScenario], [BeforeStep] Hooks for doing that, but it turned out that I do not have all the required information within this hooks. E.g. i do not know how to get the current text line of the current step executed (including line information, etc.) or the result (failed / passed) of the current step.
在执行测试期间,是否有办法在这些挂钩中或以任何其他方式获取此信息?
Is there a way to get this information within those hooks or in any other way during the execution of the test?
如果不是: 有没有其他方法可以自定义Specflow创建的跟踪输出?
If not: Is there a way to customize the trace output created by Specflow in any other way?
推荐答案
为了提供ITestTracer的自定义实现,您应该为SpecFlow创建一个插件.
In order to provide a custom implementation of ITestTracer you should create a plugin for SpecFlow.
创建一个名称为CustomTracer.SpecflowPlugin的类库项目. CustomTracer是您选择插件的名称.
Create a class library project with a name CustomTracer.SpecflowPlugin. CustomTracer is your name of choice for plugin.
然后将以下代码放入新程序集中
Then put the following code into your new assembly
[assembly: RuntimePlugin(typeof(CustomTracer.SpecflowPlugin.CustomTracerPlugin))]
namespace CustomTracer.SpecflowPlugin
{
public class CustomTracerPlugin : IRuntimePlugin
{
public void RegisterDependencies(ObjectContainer container)
{
}
public void RegisterCustomizations(ObjectContainer container, RuntimeConfiguration runtimeConfiguration)
{
container.RegisterTypeAs<CustomTracer, ITestTracer>();
}
public void RegisterConfigurationDefaults(RuntimeConfiguration runtimeConfiguration)
{
}
}
public class CustomTracer : ITestTracer
{
// Your implementation here
}
}
编译程序集,并将其放入您的规格所在的项目文件夹(.csprog文件所在的位置).
Compile assembly and put in the project folder where your specs are (where .csprog file is).
编辑app.config,specFlow部分以包括:
Edit app.config, specFlow section to include:
<plugins>
<add name="CustomTracer" path="." type="Runtime"/>
</plugins>
就是这样.您的插件应加载,并且在方案执行期间应调用您的自定义ITracer实现.如果您在调试下运行方案,您甚至可以对其进行调试.
That is it. Your plugin should load and your custom ITracer implementation should be called during scenario execution. You can even debug it, if you run scenarios under debug.
这篇关于如何使用SpecFlow设置单个跟踪/日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!