How can I make a unit test run in DEBUG mode only?

Try this:

#if DEBUG

// here is your test

#endif

As for most of your question, it depends somewhat on what unit testing tool your using. However, in general what you want are preprocessor directives

//C#
#ifndef DEBUG
    //Unit test
#endif

Perhaps for your situation

//C# - for NUnit
#if !DEBUG
    [Ignore("This test runs only in debug")] 
#endif 

But as to whether to leave unit tests in the release version? I'd give a resounding NO. I'd suggest moving all your unit tests into it's own project and NOT including this in your releases.


If you're using XUnit, you can use the following method as described by Jimmy Bogard by extending the fact attribute:

public class RunnableInDebugOnlyAttribute : FactAttribute
{
    public RunnableInDebugOnlyAttribute()
    {
        if (!Debugger.IsAttached)
        {
            Skip = "Only running in interactive mode.";
        }
    }
}

and then you can use this as follows:

[RunnableInDebugOnly]
public void Test_RunOnlyWhenDebugging()
{
    //your test code
}

If you're using NUnit, you can make your unit test methods conditional:

[System.Diagnostics.Conditional("DEBUG")]
public void UnitTestMethod()
{
   // Tests here
}

This way it will only be executed in DEBUG builds. I don't have a lot of experience with Visual Studio unit tests, but I'm pretty sure that this should work there in VS too.

EDIT: Others have mentionned conditional compilation directives. I don't think that it is a very good idea, for a number of reasons. To learn more about the differences between conditional compilation directives and the conditional attribute, read Eric Lippert's excellent article here.