如何并行运行 SpecFlow 测试

问题描述 投票:0回答:3

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

  • C#/.Net Core 3.1
  • NUnit 作为测试运行器
  • 规格流

我已将此行添加到文件顶部(如下所述: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);
        }
    }
}
c# unit-testing .net-core mstest specflow
3个回答
0
投票

我正在用这个:

[assembly: Parallelizable(ParallelScope.Fixtures)]

0
投票

文档来看,SpecFlow+ Runner 支持场景并行。 NUnit、MsTest、xUnit 运行程序仅支持功能并行性。

我注意到您正在使用 NUnit。它还记录在NUnit 配置下:“注意:SpecFlow 不支持 NUnit 的场景级别并行化(当来自相同功能的场景并行执行时)。如果您配置比“Fixtures”更高级别的 NUnit 并行化,您的测试将因运行时错误而失败。”


0
投票

您应该使用 usings.cs 创建新类 global using NUnit.Framework;[ assembly: Parallelized(ParallelScope.Fixtures)] // 设置并行 [程序集:LevelOfParallelism(13)] // 线程数

© www.soinside.com 2019 - 2024. All rights reserved.