首页 > 解决方案 > 如果 [AssemblyInitialize] 已经存在于 Specflow 的测试项目中,则会出错

问题描述

我已经将 Specflow 从 更新3.0.2253.1.62并且我收到了错误Tests_Integration_MSTestAssemblyHooks: Cannot define more than one method with the AssemblyInitialize attribute inside an assembly.

原因显然是我的[AssemblyInitialize]项目中已经有了这个属性。我该如何解决?

标签: mstestspecflow

解决方案


原因是 Specflow 在后台生成另一个文件,其中定义了AssemblyInitialize/AssemblyCleanup钩子。为了解决这个问题,应该使用 Specflow 提供的钩子,即BeforeTestRun/ AfterTestRun。像这样:

[Binding] // add the Binding attribute on the class with the assembly level hooks
public abstract class SeleniumTest 
{
  // it used to be [AssemblyInitialize]
  [BeforeTestRun]
  public static void AssemblyInitialize(/* note there is no TestContext parameter anymore */)
  {
    // ...
  }

  // it used to be [AssemblyCleanup]
  [AfterTestRun]
  public static void AssemblyCleanup()
  {
    // ...
  }
}

推荐阅读