Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging with Specflow and xUnit 2 (ITestOutputHelper)

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?

like image 599
softbear Avatar asked Apr 01 '16 19:04

softbear


People also ask

How do I enable logging in xUnit?

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

How do I use the itestoutputhelper interface in unit tests?

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.

What happened to the output capture mechanism in xUnit V2?

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.

How to flush the logger factory output in a unit test?

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.


1 Answers

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");
like image 68
BenTaylor Avatar answered Oct 08 '22 20:10

BenTaylor