How can I tell my program is in a unit test environment

I have a console application. In a release environment, it works great at this time. When in the debug IDE I don't want the console window to close, so I added this function and called it at the very end of my program.

[Conditional("DEBUG")]
public static void DebugWaitAKey(string message = "Press any key")
{
    Console.WriteLine(message);
    Console.ReadKey();
}

      

This works well for me when I am debugging my program. But with unit testing, it still waits for a key until it comes out!

The workaround is just a release version of my program or testing other features. But I want something to be able to identify the current session under unit testing and use that flag in this function.

+3


source to share


4 answers


I believe this should answer your question. I took the class from there and adapted it to your situation.

/// <summary>
/// Detects if we are running inside a unit test.
/// </summary>
public static class UnitTestDetector
{
    static UnitTestDetector()
    {
        string testAssemblyName = "Microsoft.VisualStudio.QualityTools.UnitTestFramework";
    UnitTestDetector.IsInUnitTest = AppDomain.CurrentDomain.GetAssemblies()
        .Any(a => a.FullName.StartsWith(testAssemblyName));
    }

    public static bool IsInUnitTest { get; private set; }
}

      

Then I added a line to your method, which if it runs a test will not end up in Console.ReadKey ();



[Conditional("DEBUG")]
public static void DebugWaitAKey(string message = "Press any key")
{
    Console.WriteLine(message);
    if(!UnitTestDetector.IsInUnitTest)
        Console.ReadKey();
}

      

Note. This will be considered a hack and not considered best practice.

EDIT: I've also created a sample github project to showcase this code. https://github.com/jeffweiler8770/UnitTest

+3


source


Instead of looking to see if the program was compiled in debug mode, you can see if a debugger is connected:

if (Debugger.IsAttached)
{
    Console.WriteLine(message);
    Console.ReadKey();
}

      



Please note that this will only detect if you start with F5 from Visual Studio, not Ctrl-F5 (i.e. start with debug only)

+1


source


An easy way if your test is running from a special UnitTest project: use a flag in AppSettings ...

I would not research templates for such a purpose, I would execute the test in a special UnitTest project with my own configuration.

If you need to collect data, perhaps you should just use traces (they can be configured from your .config file) ...?

Hope it helps ...

+1


source


I used Jeff's UnitTestDetector variant. I didn't want to check a unit test build, and would like to control which unit tests will count it or not.

So, I created a simple class with IsInUnitTest by default false. Then in the unit test classes where I wanted to run the conditional code, I added TestInitializer and TestCleanup where I set bool accordingly.

Then in my normal code I can use UnitTestDetector.IsInUnitTest

Simple UnitTestDetector class:

/// <summary>
/// Detects if we are running inside a unit test.
/// </summary>
public static class UnitTestDetector
{
    static private bool _isInUnitTest = false;
    public static bool IsInUnitTest
    {
        get { return _isInUnitTest; }
        set { _isInUnitTest = value; }
    }
}

      

Unit Test Check this stuff:

[TestClass]
public class UnitTestDetectorTest_WithoutIsInUnitTest
{
    [TestMethod]
    public void IsInUnitTest_WithoutUnitTestAttribute_False()
    {
        bool expected = false;
        bool actual = UnitTestDetector.IsInUnitTest;
        Assert.AreEqual(expected, actual);
    }
}

[TestClass]
public class UnitTestDetectorTest_WithIsInUnitTest
{
    [TestInitialize()]
    public void Initialize()
    {
        UnitTestDetector.IsInUnitTest = true;
    }

    [TestCleanup()]
    public void Cleanup()
    {
        UnitTestDetector.IsInUnitTest = false;
    }

    [TestMethod]
    public void IsInUnitTest_WithUnitTestAttribute_True()
    {
        bool expected = true;
        bool actual = UnitTestDetector.IsInUnitTest;
        Assert.AreEqual(expected, actual);
    }
}

      

Condition in code:

if (UnitTestDetector.IsInUnitTest)
            return "Hey I'm in the unit test :)";

      

0


source







All Articles