使用 SpecRun.Specflow 和 xUnit(ITestOutputHelper) 进行记录 - BoDi.ObjectContainerException

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

我正在尝试将日志添加到 specflow 测试,但不幸的是我遇到了异常。我将 SpecRun.Specflow v3.4.19 库与 xunit 一起使用。在尝试通过上下文注入注入 ITestOutputHelper 时,我得到了这个:

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

用法:

    private ITestOutputHelper _testOutputHelper;

    private readonly ScenarioContext _scenarioContext;

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

通过 SpecRun.SpecFlow 包,您使用 SpecFlow+ Runner 作为单元测试运行器。您没有使用 xUnit,因此您不能使用 xUnit API 来记录内容。

您有两个选择:

  1. 您继续使用 SpecFlow+ Runner 并使用 ISpecFlowOutputHelper 界面来编写您的日志条目

  2. 您更改为 SpecFlow.xUnit 包(并删除 SpecRun.SpecFlow 包)以使用 xUnit 作为单元测试运行器。然后您仍然可以使用 ISpecFlowOutputHelper 接口或直接使用 xUnit API。


全面披露:我是 SpecFlow 和 SpecFlow+ 的社区经理