如何并行运行 SpecFlow 测试

Tob*_*ayn 5 c# unit-testing mstest specflow .net-core

我尝试使用 NUnit 测试运行程序并行运行 SpecFlow 测试。我在用:

  • C#/.Net Core 3.1
  • NUnit 作为测试运行程序
  • 光谱流

我已在文件顶部添加了这一行(如下所述: https: //specflow.org/documentation/parallel-execution/):

[assembly: Parallelizable(ParallelScope.All)]
Run Code Online (Sandbox Code Playgroud)

测试开始并行执行,但立即抛出错误:

Message: 
    System.ArgumentException : An item with the same key has already been added. Key: NUnitTestProject1.SpecFlowFeature1Steps
Stack Trace: 
    Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
    Dictionary`2.Add(TKey key, TValue value)
    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)
    lambda_method(Closure , IContextManager , Int32 )
    BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
    TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments)
    TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance)
    TestExecutionEngine.OnAfterLastStep()
    TestRunner.CollectScenarioErrors()
    SpecFlowFeature1Feature.ScenarioCleanup()
    SpecFlowFeature1Feature.AddTwoNumbers() line 8
Run Code Online (Sandbox Code Playgroud)

我尝试不使用 SpecFlow(只是原始的 NUnit)并且它有效。我也尝试使用 MSTest 测试运行程序,但得到了相同的异常。

我制作了一个非常小的示例项目,您可以在这里克隆: https: //github.com/davidguidali/specflowerror

任何帮助,将不胜感激。:)

编辑:完整的步骤代码

using NUnit.Framework;
using System;
using System.Threading;
using TechTalk.SpecFlow;

[assembly: Parallelizable(ParallelScope.All)]

namespace NUnitTestProject1
{
    [Binding]
    public class SpecFlowFeature1Steps
    {
        [Given(@"test(.*)")]
        public void GivenTest(int p0)
        {
            Thread.Sleep(5000);
            Assert.IsTrue(true);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 0

我正在使用这个:

[assembly: Parallelizable(ParallelScope.Fixtures)]
Run Code Online (Sandbox Code Playgroud)

  • 它没有写到方法级别的执行不起作用。 (2认同)