locked
Root parent base class AssemblyCleanup not being called RRS feed

  • Question

  • I have a root base class that resides in a separate assembly from my main test assembly. This base class is derived by another class in the main test assembly. There are 2 levels of inheritance. The root base class has a method marked with AssemblyCleanup that is not called when the test completes.  I basically debug the TestMethod1 TestMethod and have breakpoints set on AssemblyCleanup.  It is never called.  The same is true if I put in AssemblyInitialize in the root base class.

    The class relationship code is as follows (sorry for all the formatting problems):

     [TestClass]
        public class CommonTestBase
        {
            [AssemblyCleanup]
            public static void CommonTestBaseAssemblyCleanup()
            {
    // THIS METHOD IS NOT CALLED
            }

            [TestInitialize]
            public virtual void CommonTestBaseInitialize()
            {

            }

            [TestCleanup]
            public virtual void CommonTestBaseCleanup()
            {

            }
        }

       

     [TestClass]     public class ModuleTestBase : CommonTestBase     {         [AssemblyInitialize]         public static void ModuleTestBaseAssemblyInitialize(TestContext context)         {         }         [TestInitialize]         public virtual void TestBaseInitialize()         {         }         [TestCleanup]         public virtual void TestBaseCleanup()         {         }     }

     [TestClass]
        public class MyTest1 : ModuleTestBase
        {
            [TestMethod]
            public void TestMethod1()
            {
     
            }
        }

            




    • Edited by Ken Varn Friday, September 18, 2015 6:33 PM
    Friday, September 18, 2015 6:31 PM

Answers

All replies

  • Hi Ken,

    It seems that no test method in your class CommonTestBase.

    If possible, you could use this sample in this blog to really understand/test this issue.

    [TestClass]
    public class TestClass1
    {
        public TestClass1() { }
     
        [AssemblyInitialize]
        public static void InitializeAssembly(TestContext ctx)
        { Debug.WriteLine("AssemblyInitialize"); }
     
        [AssemblyCleanup]
        public static void CleanupAssembly()
        { Debug.WriteLine("AssemblyCleanup"); }
     
        [ClassInitialize]
        public static void InitializeClass(TestContext ctx)
        { Debug.WriteLine("TestClass1: ClassInitialize"); }
     
        [ClassCleanup]
        public static void CleanupClass()
        { Debug.WriteLine("TestClass1: ClassCleanup"); }
     
        [TestInitialize]
        public void InitializeTest()
        { Debug.WriteLine("TestClass1: TestInitialize"); }
     
        [TestCleanup]
        public void CleanupTest()
        { Debug.WriteLine("TestClass1: TestCleanup"); }
     
        [TestMethod]
        public void MyTestCase1()
        { Debug.WriteLine("TestClass1: MyTestCase1"); }
    }
     
    [TestClass]
    public class TestClass2
    {
        public TestClass2() { }
     
        [ClassInitialize]
        public static void InitializeClass(TestContext ctx)
        { Debug.WriteLine("TestClass2: ClassInitialize"); }
     
        [ClassCleanup]
        public static void CleanupClass()
        { Debug.WriteLine("TestClass2: ClassCleanup"); }
     
        [TestInitialize]
        public void InitializeTest()
        { Debug.WriteLine("TestClass2: TestInitialize"); }
     
        [TestCleanup]
        public void CleanupTest()
        { Debug.WriteLine("TestClass2: TestCleanup"); }
     
        [TestMethod]
        public void MyTestCase2()
        { Debug.WriteLine("TestClass2: MyTestCase2"); }
    }
    
    
    

    Reference:

    http://blogs.msdn.com/b/ploeh/archive/2007/01/06/classcleanupmayrunlaterthanyouthink.aspx

    Best Regards,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 21, 2015 3:31 AM
  • I am still a bit confused.  I followed your advice to add a TestMethod to the CommonTestBase class.  The AssemblyInitialize and/or AssemblyCleanup still does not get executed unless I actually run that test method.  The strange thing about this is that I don't have to run the TestMethod in the root base class to get the TestInitialize method to get called.

    The problem is not in the order of execution of the methods, but on why AssemblyInitialize and AssemblyCleanup are not called at all unless I execute a TestMethod in the root base class.  Technically the base class should be regarded as an extension to the derived class, so I shouldn't have to execute a TestMethod in the base class to get the AssemblyInitialize method to execute.  Keep in mind as I said before, TestInitialize is run on both the base and derived class no matter which test method is run.  However, that does not seem to be the case with AssemblyInitialize.


    • Edited by Ken Varn Monday, September 21, 2015 1:35 PM
    Monday, September 21, 2015 1:34 PM
  • Hi Ken,

    >>The problem is not in the order of execution of the methods, but on why AssemblyInitialize and AssemblyCleanup are not called at all unless I execute a TestMethod in the root base class.

    Maybe I still misunderstood this issue, since your test project use the base class. Could you share me a screen shot about the real result in your side? Maybe you could test it using the following sample.

    [TestClass]
        public class CommonTestBase
        {
            [AssemblyCleanup]
            public static void CommonTestBaseAssemblyCleanup()
            {
                Console.WriteLine("A");
            }
    
            [TestInitialize]
            public virtual void CommonTestBaseInitialize()
            {
                Console.WriteLine("B");
            }
          
    
            [TestCleanup]
            public virtual void CommonTestBaseCleanup()
            {
                Console.WriteLine("C");
            }
        }
        [TestClass]
        public class ModuleTestBase : CommonTestBase
        {
            [AssemblyInitialize]
            public static void ModuleTestBaseAssemblyInitialize(TestContext context)
            {
                Console.WriteLine("D");
            }
    
            [TestInitialize]
            public virtual void TestBaseInitialize()
            {
                Console.WriteLine("E");
            }
    
    
    
            [TestCleanup]
            public virtual void TestBaseCleanup()
            {
                Console.WriteLine("F");
            }
        }
    
        [TestClass]
        public class MyTest1 : ModuleTestBase
        {
            [TestMethod]
            public void TestMethod1()
            {
                Console.WriteLine("G");
            }
        }
    If you run the testmethod1 in test explorer windows, could you get the result like this screen shot? 

    Click "Output".

    I could get the output message of the AssemblyCleanup.

    If I have misunderstood this issue, please feel free to let me know.

    Best Regards,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 22, 2015 9:36 AM
  • Jack.

    I received an e-mail of your response, but for some reason it didn't show up here.  Just to let you know, the problem seems to only occur only when CommonTestBase class is in a different assembly than the ModuleTestBase and MyTest1 class.  Here is a result of the same run from the sample you provided with CommonTestBase in a separate assembly (Note that A is missing, which is AssemblyCleanup):



    • Edited by Ken Varn Wednesday, September 23, 2015 5:26 PM
    Wednesday, September 23, 2015 5:22 PM
  • Hi Ken,

    >>the problem seems to only occur only when CommonTestBase class is in a different assembly than the ModuleTestBase and MyTest1 class.

    Could you share me a whole project using one drive? I will download and really test it in my side.

    I doubt that it is by design, it would not really call AssemblyCleanup out of the assembly file.

    http://blogs.msdn.com/b/densto/archive/2008/05/16/using-a-base-class-for-your-unit-test-classes.aspx

    https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assemblycleanupattribute.aspx

    Best Regards,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 24, 2015 10:34 AM
  • Jack.

    I was having trouble getting the file to upload to OneDrive, so I used my Google Drive instead.  Here is a link to the project zip file.

    https://drive.google.com/file/d/0B7dSpDGTY7cbdXVGRVliQl9iNDQ/view?usp=sharing

    Note that neither AssemblyCleanup nor AssemblyInitialize are being called in the CommonTestBase class that resides in ClassLibrary1.

    Thursday, September 24, 2015 1:19 PM
  • Hi Ken Varn,

    I could repro this issue in my side.

    I doubt that it really has this limitation for the two attributes out of the assembly file.

    You can submit this feedback to Microsoft Connect feedback portal: http://connect.microsoft.com/VisualStudio/feedback/CreateFeedback.aspx,Microsoft engineers will evaluate them seriously.

    Best Regards,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 25, 2015 10:39 AM
  • Thanks. I submitted the bug. Hopefully this will get corrected soon.

    Looks like the only workaround at this point is to explicitly call the base class AssemblyCleanup method from the derived class.

    Friday, September 25, 2015 1:36 PM
  • Hi Ken,

    https://connect.microsoft.com/VisualStudio/feedback/details/1834204/test-executiion-will-not-run-assemblyinitialize-or-assemblycleanup-in-a-base-class

    As you already submit one feedback on the Microsoft Connect site, let's focus on the case on that channel, may the senior members can provide more professional support.

    Thanks,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 28, 2015 2:54 AM