Question

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?

Was it helpful?

Solution

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?

OTHER TIPS

You wouldn't want to handle the caller context from your code; particularly if you are trying to test the logic within it. If, however, you want to execute some function with different parameters (for example) depending on whether you are executing it in application or test context, then you could either use some base type or interface as the parameter, or tag the method with an attribute and check the attribute using reflection. The directives are to be used to determine what code should be compiled and you should only use them if you are certain that's what you are intending.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top