Consuming IExecutionEvent
AnsweredIs there a way to implement IExecutionEventListener in a runtime plug-in? I've tried registering a class in various events of my runtime plug-in (RegisterGlobalDependencies, CustomizeGlobalDependencies, CustomizeThreadDependencies) using e.ObjectContainer.RegisterTypeAs<IExecutionEventListener, class>().
I am using various Before/After hooks currently, but I was hoping to move to implementing interfaces / consuming events to better match what I am doing elsewhere.
Ideally, I would be able to receive the ExecutionEvents directly in my code, replacing the use of hooks.
-
Take a look at the code of TestThreadExecutionEventPublisher. You have to add the listener manually. So you have to resolve an ITestThreadExecutionEventPublisher from the thread container and call AddListener or AddHandler to hook into the execution events of the test thread.
0 -
Thanks Zoltán Tóth, that worked perfectly. I'll document here for future reference by others:
public class CustomRunner : IRuntimePlugin
{
........
public void Initialize(RuntimePluginEvents runtimePluginEvents,
RuntimePluginParameters runtimePluginParameters,
UnitTestProviderConfiguration unitTestProviderConfiguration)
{
runtimePluginEvents.CustomizeTestThreadDependencies += CustomizeTestThreadDependencies;
}
private void CustomizeTestThreadDependencies(Object sender, CustomizeTestThreadDependenciesEventArgs e)
{
if (e.ObjectContainer.IsRegistered<ITestThreadExecutionEventPublisher>())
{
var publisher = e.ObjectContainer.Resolve<ITestThreadExecutionEventPublisher>();
publisher.AddListener(new CustomListener());
}
}
}
public class CustomListener : IExecutionEventListener
{
public void OnEvent(IExecutionEvent executionEvent)
{
switch (executionEvent)
{
case TestRunStartedEvent startEvent:
{
// do stuff
break;
}
case FeatureStartedEvent startEvent:
{
// do other stuff
break;
}
case ScenarioStartedEvent startEvent:
{
// hey look - more stuff!
break;
}
}
// etc.
}
}0
Please sign in to leave a comment.
Comments
2 comments