Running NUnit test depending on a condition
-
07-04-2021 - |
문제
I'm writing a project using Win7 x64
. Some part of my tests requires using SQLServer CE which only represents support for x86. I'm using Visual Studio 2010 Express
and I gotta change platform target for my projects manually editing *.cproj files to run, for example, schema export test (NHibernate)
. Howcome I run a part of my tests depending on a platform target.
Thanks!
해결책
I don't know if there's a built-in mechanism in NUnit to handle this scenario, but at the very least you can use preprocessor directives.
For instance, create a "Debug x86" solution configuration, targeting x86. Then define the DEBUG_X86 conditional compilation symbol (in the properties of the project). Finally, surround your unit test with preprocessor directives:
#if DEBUG_X86
[Test]
public void Test()
{
// This test will only run when compiled with Debug x86
}
#endif
Edit: Actually, you don't even have to create a new solution configuration, as it's possible to define the conditional symbols depending on the platform (https://stackoverflow.com/a/1313450/869621). So define a WIN32 compilation symbol, and surround your test with it:
#if WIN32
[Test]
public void Test()
{
// This test will only run when compiled for x86
}
#endif