Unfortunately I have a Specflow test passing locally, but it fails on the VSO Build vNext server, and I really need to see verbose information during the test run so I can figure out what is going on.
But I'm struggling to try to inject ITestOutputHelper
into a Specflow binding like so
public SomeSteps(ITestOutputHelper outputHelper)
but Specflow complains with the message
BoDi.ObjectContainerException Interface cannot be resolved: Xunit.Abstractions.ITestOutputHelper (resolution path: ...)
How on earth can view log and view output during a Specflow test?
By default logging inside XUnit can be done through the ITestOutputHelper. But this is only useful in situations where you are inside in your test code and have direct access to this interface. Most of the logging in your application however, is typically happening inside the application logic itself (through the ILogger<T> provided by .NET).
Unit tests have access to a special interface which replaces previous usage of Console and similar mechanisms: ITestOutputHelper . In order to take advantage of this, just add a constructor argument for this interface, and stash it so you can use it in the unit test.
When xUnit.net v2 shipped with parallelization turned on by default, this output capture mechanism was no longer appropriate; it is impossible to know which of the many tests that could be running in parallel were responsible for writing to those shared resources.
Note that, starting from .NET Core 3.0, we need to update the instantiation method to be var loggerFactory = LoggerFactory.Create (builder => builder.AddConsole ()). In the unit test, the using clause is necessary to flush the Console output, which disposes the logger factory.
not sure if I'm using a newer version and it's easier now, but this seems to work for me:
ScenarioContext.Current.ScenarioContainer.Resolve<ITestOutputHelper>().WriteLine("Hello");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With