1

I have some testing code using condition compilation like below:

public class MyFixture : IDisposable
{
    public MyFixture()
    {
        #if UseMySqlForTesting 
             ... do A
        #else
             ... do B
        #endif
    }

and also some related files if UseMySqlForTesting is true:

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <IsPackable>false</IsPackable>
    <UseMySqlForTesting>true</UseMySqlForTesting>
  </PropertyGroup>

  ...
  <!-- copy the files if we use mysql, otherwise, don't copy them -->
  <ItemGroup Condition="$(UseMySqlForTesting)">
    <None Update="MySql.Test.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="other.configuration.files.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

Now I'd like to use dotnet test to test the project.

Question:

Is there an approach like this:

dotnet test /p:UseMySqlForTesting=false
dotnet test /p:UseMySqlForTesting=true    

by which it can test the #if or #else branch automatically and also copy the related files if required?

itminus
  • 23,772
  • 2
  • 53
  • 88
  • 2
    Those variables get set depending on your build configuration, so I'd think you'd want to set up two build configs and then test them accordingly. – Victor Wilson Mar 06 '20 at 05:06

0 Answers0