Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell that my program is under unit test environment

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

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

It works well for me, when I debug my program. But with unit testing, it still wait for a key before exiting!

The work-around is only unit-testing release edition of my program, or test other functions. But I do want something can identify current session is under unit testing, and use that flag in this function.

like image 873
Herbert Yu Avatar asked Apr 22 '15 17:04

Herbert Yu


People also ask

What is unit test environment?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.

What is example of unit testing?

A Real-world ExampleThe above unit test “asserts” that 5 + 10 is equal to 15. If the Add function returns anything else Assert. IsEqual result in error and the test case will fail. After you write your test cases, you will run them to verify that everything is working correctly.

Which is a program that invokes the unit under test?

A test driver is a program that invokes the unit under test.

How do you unit test a program?

A typical unit test contains 3 phases: First, it initializes a small piece of an application it wants to test (also known as the system under test, or SUT), then it applies some stimulus to the system under test (usually by calling a method on it), and finally, it observes the resulting behavior.


1 Answers

I believe this should answer your question. I took a 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 is running a test it will not hit the Console.ReadKey();

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

Note: This would be considered a hack and would not be considered a best practice.

EDIT: I also created a sample project on github to demonstrate this code. https://github.com/jeffweiler8770/UnitTest

like image 190
jsw Avatar answered Sep 19 '22 06:09

jsw