Alex Rivera | Logout

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

Asked 2009-10-30T11:22:24.833
17

I have a unit test that tests if an Exception is throw, but this Exception is only throw in Debug mode (via the [Conditional("DEBUG")] Attribute). If I run this test in Release mode, it fails. I tried to apply the same Attribute on the test but it's no taken into account.

How can I exclude a test in Release mode? Does it even make sense to run unit tests in Release mode or should I stick to Debug mode?

Edit
Report

2 Answers

18

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.

answered 2009-10-30T11:26:09.667
8

Try this:

#if DEBUG

// here is your test

#endif
answered 2009-10-30T11:24:49.963

Your Answer