none
Can I ignore a unit test based on the environment I'm running the test in? RRS feed

  • Question

  • I have a I have a test project that contains 100 unit tests. I have 1 test that I want to run in all envs, except for one. Is there a way for me to ignore the test depending on which env I am running it in? It looks like MSTEST 2.x only allows you to set [Ignore] attribute, but if I use that it will ignore it for all envs. 

    Example:

    using Microsoft.VisualStudio.TestTools.UnitTesting;

          [TestMethod]
            public void LookupByCountry()
            {

    ...

    }
              

    Friday, August 9, 2019 6:36 PM

All replies

  • Hi Ballbuster1,

    Sorry for delay in reply.

    For your requirement, you could detect if the environment assembly is loaded, then use "Assert.Inconclusive()" to skip the test method when it is loaded. Please refer this sample:

    [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                if (!EnvironmentDetector.Isloaded)
                    Assert.Inconclusive();
                else
                { 
                    // action 
                }    
            }
        }
    
        public static class EnvironmentDetector
        {
            static EnvironmentDetector()
            {
                string EnvironmentAssemblyName = "Assembly Name";
                EnvironmentDetector.Isloaded = AppDomain.CurrentDomain.GetAssemblies().Any(a => a.FullName.StartsWith(EnvironmentAssemblyName));
            }
    
            public static bool Isloaded { get; private set; }
        }

    Please refer more: https://stackoverflow.com/a/29805836

    Any feedback will be expected.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com


    Monday, August 12, 2019 8:09 AM
  • Thanks Dylan! This is exactly what I needed. Your guidance is much appreciated.
    Tuesday, August 13, 2019 6:23 PM
  • Hi Ballbuster1,

    I am glad to hear that the reply is helpful for you. 

    In order to make it be beneficial for other community members which has similar requirement to find it, please mark it as answer.

    BTW, if you have other issue about unit testing with VS, please feel free to contact us, we will try our best to provide a solution.

    Have a nice day.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Wednesday, August 14, 2019 5:10 AM