Exclude code when running in a test #2530
-
In my xunit project I'm accessing a static class of my application. In this static class there are many properties which are irrelevant for the test but they're getting initialized and xunit fails. I don't want to rewrite everything only for getting xunit to work. Are there any kind of preprocessor directives I can use to simply exclude these properties when running in xunit only? Something like: #if !xunit
internal static readonly BitmapImage ErrorImage = new(new Uri("pack://application:,,,/Images/Error.jpg"));
#endif |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
We don't emit any compiler directives during builds (and it's outside the scope of what we do). Assuming you're building the code twice (once for production and once for testing), you could do this yourself by defining this only during the test builds. If you're not building it twice, then I'd suggest you start doing so, because the only way to get this kind of compilation change is by building the code twice. Alternatively, you could leverage |
Beta Was this translation helpful? Give feedback.
We don't emit any compiler directives during builds (and it's outside the scope of what we do).
Assuming you're building the code twice (once for production and once for testing), you could do this yourself by defining this only during the test builds. If you're not building it twice, then I'd suggest you start doing so, because the only way to get this kind of compilation change is by building the code twice.
Alternatively, you could leverage
Lazy<T>
to stop running all your static startup code until the first time the value in question is needed.