Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error if the [AssemblyInitialize] already exists in the test project with Specflow

I've updated Specflow from the 3.0.225 to the 3.1.62 and I received the error Tests_Integration_MSTestAssemblyHooks: Cannot define more than one method with the AssemblyInitialize attribute inside an assembly.

The reason is obviously that I'd had the [AssemblyInitialize] attribute in my project already. How can I fix it?

like image 633
Andrey Stukalin Avatar asked Dec 10 '25 15:12

Andrey Stukalin


1 Answers

The reason is that Specflow generates another file in the background which has the AssemblyInitialize/AssemblyCleanup hooks defined. In order to fix that one should use the hooks provided by Specflow, namely BeforeTestRun/AfterTestRun. Like this:

[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()
  {
    // ...
  }
}
like image 166
Andrey Stukalin Avatar answered Dec 14 '25 07:12

Andrey Stukalin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!