【问题标题】:Accessing scope of test during parallel execution on Specflow and Nunit在 Specflow 和 Nunit 上并行执行期间访问测试范围
【发布时间】:2019-08-07 21:00:13
【问题描述】:

我使用 NUnit 实现了一个测试框架,在子级具有并行性 - ParallelScope.Children 没有问题。我在 textFixture 中创建了一个嵌套类,这样每个 [Test] 都有自己的范围,并且不会相互重叠。

现在,我必须将 Specflow 集成到上述测试中。我使用“依赖注入”来按照指导共享一些状态,并且一切运行正常,没有问题,没有并行性

当我尝试在 Specflow 的夹具级别并行运行时出现问题 - 这意味着该功能的 1 个测试与另一个功能的另一个测试并行运行。

这是我的配置: NUnit 3.11 Specflow 2.41(我遇到了 Specflow 3 的一些问题,所以我使用了 2.41) 网 4.6.1 NUnit 测试适配器 3

钩子:

private static TestScopeContext _testScope;
private readonly IObjectContainer _objectContainer;

public Hooks(IObjectContainer objectContainer)
{
     _objectContainer = objectContainer;
}

[BeforeTestRun]
public static void SetUpTestScope()
{
     _testScope = new TestScopeContext();
}

[BeforeScenario]
public void CreateScenario(FeatureContext featureContext, ScenarioContext scenarioContext)
{
     _objectContainer.RegisterInstanceAs<TestScopeContext>(_testScope);
     //some codes that need access to feature context, scenario context. Not sure if this is the correct way
}

TestScopeContext.cs

public class TestScopeContext:IDisposable
{
   public string value;
   //other codes
}

绑定步骤

文件A.cs


[Binding]
    public class FileProcessingTestSteps
    {
        private readonly TestScopeContext _testScope;
        private readonly ScenarioContext _scenarioContext;

        public FileProcessingTestSteps(
            TestScopeContext testScope,
            ScenarioContext scenarioContext)
        {
            _testScope = testScope;
            _scenarioContext = scenarioContext;
        }


        [When(@"The user drops the file to (.*) UNC path")]
        public void WhenTheUserDropsTheFileToUNCPath(string client)
        {
            Console.WriteLine(_scenarioContext.ScenarioInfo.Title);
            Console.WriteLine(_testScope.value);  //issue at this line          
        }
}

文件B.cs

[Binding]
    public class CitiTestStepsDefinition
    {
        private readonly TestScopeContext _testScope;
        private readonly ScenarioContext _scenarioContext;

        public CitiTestStepsDefinition(
            TestScopeContext testScope,
            ScenarioContext scenarioContext)
        {
            _testScope = testScope;
            _scenarioContext = scenarioContext;
        }

        [Given(@"The user modifies the File (.*)")]
        public void GivenTheUserModifiesTheFile(string text)
        {            
            _testScope.value= _scenarioContext.ScenarioInfo.Title;           
        }
    }

问题一:

鉴于我有 2 个测试 - Test1、Test2。

Console.WriteLine(_testScope.value); 在运行时将为两个测试提供 Test1,或者 Test2 取决于最后分配的值。

不管我定义了多少个worker,这个testScope是否只有一个实例。

问题 2: 我试图删除此代码_objectContainer.RegisterInstanceAs&lt;TestScopeContext&gt;(_testScope);

那么测试的范围正确运行,_testScope.value 对于这个场景返回 Test1,对于另一个场景返回 Test2

我认为使用RegisterInstanceAs 以便可以在绑定步骤之间共享“TestScope”状态。我的配置有问题吗?

如果不清楚,请告诉我,我可以尝试复制我的工作项目并在此处附加代码

【问题讨论】:

    标签: c# nunit specflow parallel-testing


    【解决方案1】:

    我在将 TestScopeContext _testScope 声明为静态时犯了一个错误。移除 static 关键字,将创建新 TestScopeContext 实例的逻辑移至 [BeforeScenario] 修复此问题。

    【讨论】:

      猜你喜欢
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多