Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use #IF directive to detect unit test

I have this specific call in my code and I found the config needs to be different when running the full app as to when I'm testing using an integration unit test.

Is there a way to check for a call from a unit test using the #if directive?

#if TestMethod
...do this config
#else
...do this config
#endif

Maybe someway to detect an attribute on the unit test function?

like image 701
Yogurt The Wise Avatar asked Nov 25 '11 19:11

Yogurt The Wise


1 Answers

No, because either the code exists in the built binary or it doesn't. The decision is made entirely at compile-time, so there's nothing you can do at execution time to reintroduce the "missing" code. You'd have to dynamically load one of two different binaries, and run the tests that way.

Do you have to use conditional compilation, instead of deciding which route to take at execution time?

like image 54
Jon Skeet Avatar answered Oct 05 '22 01:10

Jon Skeet