locked
Unit testing and static classes. RRS feed

  • Question

  • I have a static class that provides a set of services to our apps.

    Before any methods on this class can be called one must first call an Initialize method with some arguments these args then define the behavior for the remainder of the app's lifetime.

    I also have a set of unit tests but because these tests all run within the same AppDomain and static classes undergo initialization at the point they are first referenced I'm having trouble creating a set of unit tests that allow me to test different Initialize arguments.

    Now before anyone chooses to lecture me on the woes of using static classes please recognize that .Net itself provides rather a lot of these, for example ConfigurationManager is a static class and there are many others for the right kind of problem they are a legitimate languages feature.

    So - is there a way with unit tests to create a new AppDomain so that I can run multiple scenarios within a new AppDomain for each set of tests?

    It would be neat if I could have two test classes and add an attribute to the class like [NewDomain] or something. Then at runtime all the tests in a class will run within a specific app domain all the tests in one cass would run in some app domain and then all the tests in the second class would run in new, different app domain.

    Thanks


    Saturday, September 3, 2016 11:25 PM

Answers

  • Hi Korporal Kernel,

    As far as I know, we could create a new AppDomain in every test to implement run a test in separate AppDomain with following sample code. Please refer to:

    https://malvinly.com/2013/07/30/creating-new-application-domains-while-running-a-unit-test/

     AppDomain domain = AppDomain.CreateDomain(
                    "Test", 
                    new Evidence(AppDomain.CurrentDomain.Evidence), 
                    new AppDomainSetup
                    {
                        ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase
                    });

    But I don't find such attribute to implement create new AppDomain for set of tests. So if you want to use attribute to implement your requirement, I suggest you create your own custom attribute. You could create new AppDomain in your custom Attribute and then use it on the tests that you want. Following blog introduce use Custom Attributes to initialize test environments. Hope it can help you.

    https://blogs.msdn.microsoft.com/calvin_hsia/2013/06/28/use-custom-attributes-to-initialize-test-environments/

    Best Regards,
    Weiwei


    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.

    • Proposed as answer by Weiwei Cai Monday, September 12, 2016 9:38 AM
    • Marked as answer by Weiwei Cai Tuesday, September 13, 2016 8:43 AM
    Monday, September 5, 2016 7:05 AM

All replies

  • I have a static class that provides a set of services to our apps.

    Before any methods on this class can be called one must first call an Initialize method with some arguments these args then define the behavior for the remainder of the app's lifetime.

    I also have a set of unit tests but because these tests all run within the same AppDomain and static classes undergo initialization at the point they are first referenced I'm having trouble creating a set of unit tests that allow me to test different Initialize arguments.

    Now before anyone chooses to lecture me on the woes of using static classes please recognize that .Net itself provides rather a lot of these, for example ConfigurationManager is a static class and there are many others for the right kind of problem they are a legitimate languages feature.

    So - is there a way with unit tests to create a new AppDomain so that I can run multiple scenarios within a new AppDomain for each set of tests?

    It would be neat if I could have two test classes and add an attribute [NewDomain] or something. Then at runtime all the tests in a class will run within a specific app domain all the tests in one cass would run in some app domain and then all the tests in the second class would run in new, different app domain.

    Thanks


    • Edited by Captain Kernel Saturday, September 3, 2016 11:24 PM
    • Moved by DotNet Wang Monday, September 5, 2016 2:39 AM
    • Merged by Weiwei Cai Tuesday, September 6, 2016 2:52 AM duplicate
    Saturday, September 3, 2016 10:58 PM
  • Saturday, September 3, 2016 11:10 PM
  •  Hi Korporal Kernel,

    Since your problem is more related to VS unit test . I moved it to visual studio Visual studio unit test forum.

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding.

    Best Regards,

    Kevin


    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 5, 2016 2:32 AM
  • Hi Korporal Kernel,

    As far as I know, we could create a new AppDomain in every test to implement run a test in separate AppDomain with following sample code. Please refer to:

    https://malvinly.com/2013/07/30/creating-new-application-domains-while-running-a-unit-test/

     AppDomain domain = AppDomain.CreateDomain(
                    "Test", 
                    new Evidence(AppDomain.CurrentDomain.Evidence), 
                    new AppDomainSetup
                    {
                        ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase
                    });

    But I don't find such attribute to implement create new AppDomain for set of tests. So if you want to use attribute to implement your requirement, I suggest you create your own custom attribute. You could create new AppDomain in your custom Attribute and then use it on the tests that you want. Following blog introduce use Custom Attributes to initialize test environments. Hope it can help you.

    https://blogs.msdn.microsoft.com/calvin_hsia/2013/06/28/use-custom-attributes-to-initialize-test-environments/

    Best Regards,
    Weiwei


    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.

    • Proposed as answer by Weiwei Cai Monday, September 12, 2016 9:38 AM
    • Marked as answer by Weiwei Cai Tuesday, September 13, 2016 8:43 AM
    Monday, September 5, 2016 7:05 AM