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


i have console application. in release environment, works @ time. when in ide debug environment, don't want console window close, added function, , calling in end of program.

[conditional("debug")] public static void debugwaitakey(string message = "press key") {     console.writeline(message);     console.readkey(); } 

it works me, when debug program. unit testing, still wait key before exiting!

the work-around unit-testing release edition of program, or test other functions. want can identify current session under unit testing, , use flag in function.

i believe this should answer question. took class there , adapted situation.

/// <summary> /// detects if running inside 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 added line method if running test not hit console.readkey();

[conditional("debug")] public static void debugwaitakey(string message = "press key") {     console.writeline(message);     if(!unittestdetector.isinunittest)         console.readkey(); } 

note: considered hack , not considered best practice.

edit: created sample project on github demonstrate code. https://github.com/jeffweiler8770/unittest


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -