如果使用Specflow在测试项目中已经存在[AssemblyInitialize],则会发生错误

时间:2019-11-19 06:07:16

标签: mstest specflow

我已将Specflow从3.0.225更新为3.1.62,并收到错误Tests_Integration_MSTestAssemblyHooks: Cannot define more than one method with the AssemblyInitialize attribute inside an assembly.

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

1 个答案:

答案 0 :(得分:0)

原因是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()
  {
    // ...
  }
}