Search code examples
c#xunitspecflow

Logging with SpecRun.Specflow and xUnit(ITestOutputHelper) - BoDi.ObjectContainerException


I'm trying to add logs to specflow tests but unfortunetly I got exception. I'm using SpecRun.Specflow v3.4.19 library with xunit. While trying to inject ITestOutputHelper via Context Injection I got this:

Interface cannot be resolved: Xunit.Abstractions.ITestOutputHelper (resolution path: SpecflowTesting.Steps.CalculatorStepDefinitions) Stack Trace: BoDi.ObjectContainerException: Interface cannot be resolved: Xunit.Abstractions.ITestOutputHelper (resolution path: SpecflowTesting.Steps.CalculatorStepDefinitions) TypeRegistration.Resolve(ObjectContainer container, RegistrationKey keyToResolve, ResolutionList resolutionPath) ObjectContainer.ResolveObject(RegistrationKey keyToResolve, ResolutionList resolutionPath) ObjectContainer.Resolve(Type typeToResolve, ResolutionList resolutionPath, String name) <>c__DisplayClass57_0.b__0(ParameterInfo p) SelectArrayIterator2.ToArray() Enumerable.ToArray[TSource](IEnumerable1 source) ObjectContainer.ResolveArguments(IEnumerable`1 parameters, RegistrationKey keyToResolve, ResolutionList resolutionPath) ObjectContainer.CreateObject(Type type, ResolutionList resolutionPath, RegistrationKey keyToResolve) TypeRegistration.Resolve(ObjectContainer container, RegistrationKey keyToResolve, ResolutionList resolutionPath) ObjectContainer.ResolveObject(RegistrationKey keyToResolve, ResolutionList resolutionPath) ObjectContainer.Resolve(Type typeToResolve, ResolutionList resolutionPath, String name) ObjectContainer.Resolve(Type typeToResolve, String name) TestObjectResolver.ResolveBindingInstance(Type bindingType, IObjectContainer container) line 11 lambda_method(Closure , IContextManager , Int32 ) BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration) line 69 TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments) line 514 RunnerTestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments) TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance) line 435 TestExecutionEngine.OnAfterLastStep() line 260 RunnerTestExecutionEngine.OnAfterLastStep() TestRunner.CollectScenarioErrors() line 60 CalculatorFeature.ScenarioCleanup() CalculatorFeature.AddTwoNumbers() line 8 StaticOrInstanceMethodExecutor.ExecuteInternal(ITestThreadExecutionContext testThreadExecutionContext) StaticOrInstanceMethodExecutor.Execute(ITestThreadExecutionContext testThreadExecutionContext) TestNodeTask.Execute()

Usage:

    private ITestOutputHelper _testOutputHelper;

    private readonly ScenarioContext _scenarioContext;

    public CalculatorStepDefinitions(ScenarioContext scenarioContext, ITestOutputHelper testOutputHelper)
    {
        _scenarioContext = scenarioContext;
        _testOutputHelper = testOutputHelper;
    }

Solution

  • With the SpecRun.SpecFlow package you are using the SpecFlow+ Runner as unit test runner. You are NOT using xUnit and so you can't use the xUnit APIs to log stuff.

    You have two options:

    1. You stay with the SpecFlow+ Runner and use ISpecFlowOutputHelper interface to write your log entries

    2. You change to the SpecFlow.xUnit package (and remove the SpecRun.SpecFlow package) to use xUnit as a unit test runner. Then you can still use the ISpecFlowOutputHelper interface or use the xUnit APIs directly.


    Full disclosure: I am the Community Manager for SpecFlow and SpecFlow+