我尝试使用 NUnit 测试运行程序并行运行 SpecFlow 测试。我正在使用:
我已将此行添加到文件顶部(如下所述:https://specflow.org/documentation/parallel-execution/):
[assembly: Parallelizable(ParallelScope.All)]
测试开始并行执行,但立即抛出错误:
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
我尝试不使用 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);
}
}
}
我正在用这个:
[assembly: Parallelizable(ParallelScope.Fixtures)]
您应该使用 usings.cs 创建新类 global using NUnit.Framework;[ assembly: Parallelized(ParallelScope.Fixtures)] // 设置并行 [程序集:LevelOfParallelism(13)] // 线程数