mrtentje
mrtentje

Reputation: 1422

AssemblyInitialize in derived class

I have a abstract base test class that has an AssemblyInitialize attribute applied to a method. But it will be never executed... The abstract base test class is in another assembly because it is for a generic extension. Any ideas how to solve this?

The code

[TestClass]
public abstract BaseTestClass
{    
   [AssemblyInitialize]
   public static void AssemblyInit(TestContext context)
   {
      //DoDomething
   }
}

Thanks in advance

Upvotes: 12

Views: 9189

Answers (4)

resp78
resp78

Reputation: 1534

You should also be able to inject the TextContext, this is much cleaner and also allows to have TestContext available in a separate steps binding assembly if required easily.

class Hooks
{
    private readonly ScenarioContext _scenarioContext;

    public Hooks(ScenarioContext scenarioContext, TestContext context)
    {
        _scenarioContext = scenarioContext;
    }

    [AfterStep]
    public void AfterStep()
    {
    }
}

Upvotes: 0

MikroDel
MikroDel

Reputation: 6735

Try to implement a separate class without inheritance in your Test Project :

[TestClass]
public static class YourClass
{
    [AssemblyInitialize]
    public static void AssemblyInit(TestContext context)
    {
       //DoSomething
    }
}

It should be called.

Upvotes: 6

astef
astef

Reputation: 9498

I had the same problem when I didn't mark the test base class with the [TestClass] attribute.

Upvotes: 5

chaliasos
chaliasos

Reputation: 9783

This is happening because the Assembly is never initialized if you don't run tests from it. A solution I can give (maybe a fool one) is to use the AssemblyInitialize on the other assemblies and call the base AssemblyInitialize

In a TestProject which contains tests add the following code:

[TestClass]
public class UnitTest1
{
    [AssemblyInitialize]
    public static void AssemblyInitialize(TestContext testContext)
    {
        // call the base AssemblyInitialize
        BaseTestProject.BaseTest.AssemblyInitialize(testContext);
    }

    public TestContext TestContext { get; set; }
}

Upvotes: 10

Related Questions