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

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

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

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

原因是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()
  {
    // ...
  }
}
本文链接:https://www.f2er.com/3075970.html

大家都在问