Search code examples
c#mstest

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


Solution

  • 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; }
    }