I am using VS2022 Community Edition with xUnit to TDD a .NET 8 / .NET 4.8 WPF application. My goal is to disable a specific line of code in the app during test runs.
I added the following to the Tests project’s .csproj
file:
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>UNIT_TEST</DefineConstants>
</PropertyGroup>
Then I wrapped the code to be skipped as follows:
#if !UNIT_TEST
Logger.Log($"EditorViewModel() instance: {GetHashCode()} : Text' {Text}'", "EditorViewModel");
#endif
However, the code still executes during testing. I expected to see the UNIT_TEST symbol in the app’s compilation output, but it isn’t present. Interestingly, if I add the same PropertyGroup to the app’s own .csproj
file, it works as expected.
I suspect this is due to how conditional compilation symbols are applied only at the project level. Is this a bug with VS2022, or is there another explanation?
UPDATE:
Thanks to everyone for their input. I've learned a valuable lesson about TDD—I'm a total noob. I’m now looking into using MOQ as well.
I also considered a theoretical approach: since xUnit (or any test harness) compiles the app separately, it could potentially create a temporary copy of the app's .csproj
file with an inserted UNIT_TEST define constant. As I’m already working on dynamic assembly generation and project file manipulation, this might be a direction to explore in building a custom testing harness.