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

C# 使用SpecRun.Specflow和xUnit(ITestOutputHelper)进行日志记录-BoDi.ObjectContainerException,c#,xunit,specflow,C#,Xunit,Specflow,我试图将日志添加到specflow测试中,但不幸的是,我遇到了异常。我正在将SpecRun.Specflow v3.4.19库与xunit一起使用。在尝试通过上下文注入注入ITestOutputHelper时,我得到了以下结果: 无法解析接口:Xunit.Abstractions.ITestOutputHelper(解析路径:SpecflowTesting.Steps.CalculatorStepDefinitions) 堆栈跟踪: BoDi.ObjectContainerException:无

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

无法解析接口:Xunit.Abstractions.ITestOutputHelper(解析路径:SpecflowTesting.Steps.CalculatorStepDefinitions) 堆栈跟踪: BoDi.ObjectContainerException:无法解析接口:Xunit.Abstractions.ITestOutputHelper(解析路径:SpecflowTesting.Steps.CalculatorStepDefinitions) TypeRegistration.Resolve(ObjectContainer容器、RegistrationKey键到Resolution、ResolutionList resolutionPath) ObjectContainer.ResolveObject(RegistrationKey keyToResolve,ResolutionList resolutionPath) Resolve(键入typeToResolve、ResolutionList resolutionPath、字符串名) c_uuu显示类57_u0.b_uuu0(参数信息p) 选择ArrayIterator
2.ToArray()可枚举。ToArray[t源](IEnumerable
1源) ObjectContainer.ResolveArguments(IEnumerable`1参数、RegistrationKey keyToResolve、ResolutionList resolutionPath) CreateObject(类型类型、ResolutionList resolutionPath、RegistrationKey keyToResolve) TypeRegistration.Resolve(ObjectContainer容器、RegistrationKey键到Resolution、ResolutionList resolutionPath) ObjectContainer.ResolveObject(RegistrationKey keyToResolve,ResolutionList resolutionPath) Resolve(键入typeToResolve、ResolutionList resolutionPath、字符串名) ObjectContainer.Resolve(键入typeToResolve,字符串名称) TestObjectResolver.ResolveBindingInstance(类型bindingType,IObjectContainer容器)第11行 lambda_方法(闭包,IContextManager,Int32) BindingInvoker.InvokeBinding(IBinding绑定、IContextManager contextManager、Object[]参数、ITestTracer testTracer、TimeSpan和duration)第69行 TestExecutionEngine.ExecuteSpMatch(BindingMatch,对象[]参数)第514行 RunnerTestExecutionEngine.ExecuteSpMatch(BindingMatch匹配,对象[]参数) TestExecutionEngine.ExecuteTEP(IContextManager contextManager,StepInstance StepInstance)第435行 TestExecutionEngine.OnAfterLastStep()第260行 RunnerTestExecutionEngine.OnAfterLastStep() TestRunner.CollectScenarioErrors()第60行 CalculatorFeature.ScenarioCleanup() CalculatorFeature.AddTwoNumber()第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+运行程序用作单元测试运行程序。您没有使用xUnit,因此无法使用XUnitAPI来记录内容

您有两个选择:

  • 您可以继续使用SpecFlow+Runner,并使用
    ispecflowOutputhHelper
    界面编写日志条目

  • 您可以更改为SpecFlow.xUnit包(并删除SpecRun.SpecFlow包),以将xUnit用作单元测试运行程序。然后您仍然可以使用
    ISpecFlowOutputHelper
    接口或直接使用XUnitAPI


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