首页 > 解决方案 > 如何在 .NET Core 中传递预处理器条件

问题描述

我有一些使用条件编译的测试代码,如下所示:

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

UseMySqlForTesting如果为真,还有一些相关文件:

  <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>

现在我想dotnet test用来测试项目。

问题:

有没有这样的方法:

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

它可以通过它自动测试#ifor#else分支并在需要时复制相关文件?

标签: c#.net-corexunit

解决方案


推荐阅读