开发者

Conditions for tests in Visual Studio and .NET

Is it possible to do this in some way ?

[Condi开发者_开发技巧tional("TEST")]
...

or

#if TEST

#end if

The reason is that I would like to encapsulate some test logic in a libray, without having to extract the logic to it's own test libray.

thanks!


Yes, it is possible.

Create a build configuration called TEST and

[Conditional("TEST")]

on your methods.

To create a build configuration in Visual Studio go to "build" menu -> Configuration manager. Under "Active solution configuration:" select "new...". Give a name (TEST in your case) and you would probably want to set Debug in "Copy settings from:".

This would also allow you to use:

#if TEST
...
#end if

However using #if hides the section from the compiler, which in my experience is the source of much pain when switching between build configurations, due to refactored code which break other configurations.

EDIT

@Aaronaught mentions that the conditional attribute will not work. I think the naive solution is using #if. In that case you might want to do something like this:

#if TEST
    [TestMethod()]
#endif

before your test method to avoid hiding more than just the TestMethod() attribute from the compiler.


It sounds like you want to use Preprocessor Directives (available in C#, possibly other languages). Specifically, the use of #define in source code and the /define compiler option, along with #if statements. You didn't state which version of .NET you are using, but the MSDN indicates it goes all the way back to .NET 1.1, at least with Visual Studio 2003.


Simply create a new build configuration as written by @steenhulthin and add the compilation symbol TEST.

The name of the build configurations doesn't matter, but you have to set the conditional compilation symbols for this configaration to include TEST (project settings, page "build" IIRC). You may also define the compilation symbol in every file you want to use it, instead of the project settings. (#define TEST)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜