I'm using VS2010, I have the following method call:
[Conditional("DEBUG")]
public void VerboseLogging() { }
public void DoSomething() {
VerboseLogging();
Foo();
Bar();
}
Then I have a unit test for the DoSomething
method which checks that it emits proper logging.
[Conditional("DEBUG"), TestMethod()]
public void EnsureVerboseLog() {
DoSomething();
VerifyVerboseLoggingCalled(); // <-- fail in release builds since VerboseLogging() calls get eliminated.
}
It seems that MSTest only sees TestMethod
and executes it (generating failed test) even though I've marked it with Conditional("DEBUG")
and compile it in release mode.
So, is there a way to exclude certain tests depending on compilation constant other than #if
?
The ConditionalAttribute
does not affect whether or not a method is compiled into an application. It controls whether or not calls to the method are compiled into the application.
There is no call to EnsureVerboseLog
in this sample. MSTest just sees a method in the assemlby with the TestMethod
attribute and correctly executes it. In order to prevent MSTest from running the method you will need to do one of the following
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With