views:

403

answers:

4

Are there any good way to use the Conditional-attribute in the context of testing?

My thoughts were that if you can do this:

[Conditional("Debug")]
public void DebugMethod()
{
    //...
}

Maybe you could have some use for: (?)

[Conditional("Test")]
public void TestableMethod()
{
    //...
}
+4  A: 

I don't see a use when there's a better alternative: Test Projects.

Use NUnit or MSTest to achieve this functionality in a more graceful way.

Mehrdad Afshari
Details please. This was just a brainstorm-thing. I just threw it out there, I have no idea.
Seb Nilsson
There are testing frameworks that handle this automatically. They provide some attributes that you'll use to decorate your classes. This might explain better: http://www.nunit.org/index.php
Mehrdad Afshari
+1  A: 

I'd accept Mehrdad's answer - I just wanted to give more context on when you might use these attributes:

Things like [Conditional] are more generally used to control things like logging/tracing, or interactions with an executing debugger; where it makes sense for the calls to be in the middle of your regular code, but which you might not want in certain builds (and #if... etc is just so ugly and easy to forget).

Marc Gravell
+1  A: 

If the test code is not part of the product it should not be in the product code base. I have seen projects trying to include unit tests in the same project as the tested objects, and using #if statements to include them only in debug builds only to regret it later.

One obvious problem would be that the application project gets a reference to the unit testing framework. Even though you may not need to ship that framework as part of your product (if you can guarantee that no code in the release build will reference it), it still smells a bit funny to me.

Let test code be test code and production code be production code, and let the production code have no clue about it.

Fredrik Mörk
+1  A: 

The other problem with this is that you may want to run your unit tests on your release builds. (We certainly do).

Simon P Stevens