I know I could do something like this:
dotnet test myproject.csproj /p:IsTest=true
and in myproject.csproj:
<PropertyGroup>
...
<DefineConstants Condition=" '$(IsTest)' == 'true' ">$(DefineConstants);TEST</DefineConstants>
</PropertyGroup>
And this will allow me to selectively put in or out some portion of codes that should or shouldn't be executed/included when I only want to run a test/unit test. (by put any codes behind #if TEST ... #endif or #if !TEST ... #endif)
I want to know, can I also do something like this when I run a test via visual studio built-in test explorer?
I solved the problem by using build configuration.
And then add symbol manually for each project that requires it.
So, create a new build profile (configuration) specifically for unit test and customize what you need directly inside each projects. When you want to run a test via visual studio, you change to this new build config before you hit the run test button.