How to set up individual tracing / logging with SpecFlow

夙愿已清 提交于 2019-12-03 22:15:29

In order to provide a custom implementation of ITestTracer you should create a plugin for SpecFlow.

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
    }
}

Compile assembly and put in the project folder where your specs are (where .csprog file is).

Edit app.config, specFlow section to include:

<plugins>
  <add name="CustomTracer" path="." type="Runtime"/>
</plugins>

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.

Finally, after some investigation, i found out that you can replace the DefaultTraceListener by your own implementation by implementing the Interface ITraceListener:

public class foo:  TechTalk.SpecFlow.Tracing.ITraceListener
{
    public void WriteTestOutput(string message)
    {
        EventLog.WriteEntry("mysource", "output: " + message);
    }

    public void WriteToolOutput(string message)
    {
        EventLog.WriteEntry("mysource", "specflow: " + message);
    }
}

And modifying your App.config configuration by adding this to the "specflow" section:

<trace traceSuccessfulSteps="true"
       traceTimings="false"
       minTracedDuration="0:0:0.1"
       listener="MyNamespace.foo, MyAssemblyName"/>

However, this is more a "workaround" for me since I don't have typed information (e.g. of the StepInstance class) and I have to rely or modify the output formatting of SpecFlow.

I would prefer replacing the TestTracer (ITestTracer) implementation by my own one in some way, but i did not find a way to do this. Does anyone now how to do it?

In Specflow 2.1 its a little different.

Instead of:

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)
    {

    }
}

Do this:

public class CustomTracerPlugin : IRuntimePlugin
{
    public void Initialize(RuntimePluginEvents runtimePluginEvents, RuntimePluginParameters runtimePluginParameters)
    {
        runtimePluginEvents.CustomizeTestThreadDependencies += (sender, args) => { args.ObjectContainer.RegisterTypeAs<CustomTracer, ITestTracer>(); };
    }
}

Every thing else is the same as Vladimir Perevalovs answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!