Conditional(“DEBUG”) not working with [TestMethod]

Sad but true and by design. With Conditional(“DEBUG”), the compiler will merely ‘void out’ calls to the method marked with that attribute. The method itself is still compiled in. Thus, when mstest looks for test to run, it finds all of the¬†¬†Conditional(“DEBUG”) methods regardless of build configuration.

The effect of this fact is that unit test execution cannot be conditionalized for Release Mode. Hence the unit tests will still execute and fail, if you have debug-specific configuration in the implementation.

A couple of solutions are available:

  • switch back to #if DEBUG
  • Use Priority attribute as suggested here
  • Move implementation into a method not marked as [TestMethod] into a new method marked with¬†Conditional(“DEBUG”) and call it from the [TestMethod]

None of these are ideal, so the obvious solutions is to stop unit testing or maybe even testing in general.

Cheers!