I was able to reproduce the issue you describe, and it does work as expected if I add the PropertyGroup to the application .csproj file.
This is because DefineConstants only applies to the current project and does not affect other projects. That is, you define UNIT_TEST in the .csproj file of the Tests project, but this is not automatically passed to the WPF App project. Preprocessing directives (such as #if !UNIT_TEST) are effective at compile time, and DefineConstants only applies to the project in which it is defined (i.e., your Tests project). The WPF App project is unaware of the existence of the UNIT_TEST constant, so the code is not excluded.
You can also refer to the description in this document:
If you're setting a property somewhere and not getting the expected result, consider where and how the property is changed or used in all the files imported by your project, including imports that are added implicitly when you're using the attribute.