Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write tests for debug and release configurations

I'm writing a library, and behaviour of it differs based on mode if it's debug or release. I want to write a unit-tests TestDebugBehaviour and TestReleaseBehaviour. Is it possible to setup tests to run in debug/release mode?

like image 269
Alex Zhukovskiy Avatar asked Oct 19 '22 14:10

Alex Zhukovskiy


1 Answers

I think you should be able to do this with preprocessor directives. I am using xunit in my examples below; you will probably have to decorate your test method with a different attribute.

This test should only execute in debug mode.

#if DEBUG

[Fact]
public void ThisIsATestThatWillOnlyRunInDebugMode()
{
    throw new Exception("I am running in debug mode.");
}

#endif

This test does not exactly run in release mode specifically, it just runs in any mode except debug mode but that is usually good enough.

#if !DEBUG

[Fact]
public void ThisIsATestThatWillNotRunInDebugMode()
{
    throw new Exception("I am running in in something other than debug mode.");
}

#endif
like image 130
Jason Boyd Avatar answered Oct 22 '22 03:10

Jason Boyd