locked
Unit test not calling TestInitialize base class method which is in a shared assembly RRS feed

  • Question

  • Hi,

    I've got my repositoryTest project, which references a TestBase assembly project that has helper classes in it.

    Within this, my TestBase class has a '[TestInitialize]' public static void method which i expect to get triggered once per assembly during test execution.

    This doesnt seem to fire. However if i copy the same base class directly into the test project, it works fine and as expected.

    Not sure but i think this might be a bug.

    regards,

    Paul

    • Moved by Fred Bao Friday, January 30, 2015 2:44 AM VS Unit Testing Related
    Thursday, January 29, 2015 10:50 PM

Answers

  • Hi Paul,

    Base on your reply, this issue is different.

    Base on my test, I found that the method that mark with AssemblyInitialize attribute won’t execute if it is in the external assembly.

    I suggest that you could call it in the current test class’s method (e.g. AssemblyInitialize method)

    Regards

    Starain


    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, February 3, 2015 5:21 AM

All replies

  • Hello Milsnips,

    Since this issue is related with UnitTest, i move it to the Unit Test forum there Unit Test exeprts would help you.

    Regards.


    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, January 30, 2015 2:43 AM
  • Hi Paul,

    What do you mean shared assembly? Do you mean Global Assembly Cache?

    Base on my test in VS2013, it works fine.

    1. Create a new class library
    2. Add TestInitialize method (using console.writeline to do log)
    3. Add it to GAC
    4. Add the assembly that in the GAC (Add Reference=>Browser=>C:\Windows\Microsoft.NET\assembly\GAC_MSIL
    5. Do unit test and check the output

    I would like to know:

    1. What’s the version of you VS?
    2. How do you add shared assembly?
    3. How do you know it won’t fire?

    Regards

    Starain


    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, February 2, 2015 3:12 AM
  • Hi,

    I'm not using the GAC. This is my project setup:

    • Shared UnitTestHelper project (contains UnitTestBase.cs)
    • UnitTestProject1 (references UnitTestHelper)
    • UnitTestProject2 (references UnitTestHelper)

    UnitTestBase has methods:

     [TestInitialize]
            public void Init()
            {
               //runs before every test
            }

            [AssemblyInitialize]
            public static void InitAssembly(TestContext context)
            {
    // run once per test assembly...

           }

    I put a breakpoint on the assembly initalize and it doesnt fire.

    If then, i copy tope UnitTestBase and put it directly into UnitTestProject1  or UnitTestProject2, the event fires. 

    Monday, February 2, 2015 11:27 PM
  • Hi Paul,

    Base on your reply, this issue is different.

    Base on my test, I found that the method that mark with AssemblyInitialize attribute won’t execute if it is in the external assembly.

    I suggest that you could call it in the current test class’s method (e.g. AssemblyInitialize method)

    Regards

    Starain


    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, February 3, 2015 5:21 AM