locked
ClassInitialize isn't executed when it's in a Father Class RRS feed

  • Question

  • Hi,

    I have a class F that encapsulate some shared testing code, and then some children classes C1, C2, .. , that inherit from F.

    I want that BEFORE any of the tests methods selected to execute in C1, C2, .. execute, the ClassInitialize method in F is executed, to initialize something shared by all those tests.

    And I also want, that AFTER all tests methods selected to execute in C1, C2, .. execute, the ClassCleanUp method in F is executed.

    I have found two ways to make this work but I don't like any of them:

    1 - Using [AssemblyInitialize] in the methods of F. I don't like this because the Init and Cleanup methods of F will execute even when no test methods of C1, C2... are selected to execute.

    2 - Having a method in C1, C2, .. with [ClassInitialize], that just invoques the ClassInitialize in F.  It repeats code on each class. :S

    Setting the methods in F with ClassInitialize and ClassCleanup doesn't work.

    Is there any other way to do this?

    Wednesday, March 27, 2013 4:10 PM

Answers

  • My solution was initializing the tests in the static constructor of the F class, and executing the CleanUp method using the [AssemblyCleanup] attribute:

            /// <summary>
            /// Executes when class in created
            /// </summary>
            static IntegrationTest()
            {
                StartWebserver();
                StartWebdriver(Browser.PhantomJS);
            }

            /// <summary>
            /// Executes after test execution is finished
            /// </summary>
            [AssemblyCleanup]
            public static void ClassCleanup()
            {
                StopWebdriver();
                StopWebserver();
            }

    • Marked as answer by sdeuvarow Thursday, March 28, 2013 2:56 PM
    Thursday, March 28, 2013 2:56 PM

All replies

  • Hello,

    Thank you for your post.

    Based on your description, I create a scenario. There is a ClassInitialize method and TestMethodF1 method in F test class. Test class C1 will be inherit from test class F. There is ClassInitialize method and TestMethodC1 in C1 class. I found that there will be two TestMethodF1 from F class in the Test Explorer after I build the test project. And the order which the tests in F and C class are built is TestMethodC1, TestMethodF1(from C class) and then TestMethodF1(from F class). I think that one of them is the one in F class. Another one is the one from C1 class inherited from F class since C1 class will call TestMethodF1 in F class.

    In this case the ClassInitialize method in F class is not executed when you run the TestMethodF1 which C1 calls from F class. ClassInitialize method in F class will be executed just when you run the TestMethodF1which is actually from F class. I think that it is by design for ClassInitialize attribute and unit test framework.

    If you want the ClassInitialize method executed, I suggest that you can call it in a test method and before executing other codes in this test method.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.




    • Edited by Amanda Zhu Friday, March 29, 2013 2:19 AM
    Thursday, March 28, 2013 6:42 AM
  • My solution was initializing the tests in the static constructor of the F class, and executing the CleanUp method using the [AssemblyCleanup] attribute:

            /// <summary>
            /// Executes when class in created
            /// </summary>
            static IntegrationTest()
            {
                StartWebserver();
                StartWebdriver(Browser.PhantomJS);
            }

            /// <summary>
            /// Executes after test execution is finished
            /// </summary>
            [AssemblyCleanup]
            public static void ClassCleanup()
            {
                StopWebdriver();
                StopWebserver();
            }

    • Marked as answer by sdeuvarow Thursday, March 28, 2013 2:56 PM
    Thursday, March 28, 2013 2:56 PM
  • Hello,

    Glad to see this issue has been resolved and thank you for sharing your solutions & experience here. It will be very beneficial for other community members who have similar questions.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 29, 2013 2:18 AM