使用 xUnit 处理 netstandard1.6

Handle netstandard1.6 with xUnit

我想为 netstandard1.6 库使用测试框架。我尝试关注并编辑 Getting started with xUnit.net (.NET Core / ASP.NET Core) 但没有成功。按照 xUnit 的教程在 VS 2015 Update 3 RTM 上使用 dotnetcore lib 和我的 project.json 文件重现错误。

project.json :

{
  "version": "1.0.0-*",
  "testRunner": "xunit",
  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "xunit": "2.2.0-beta2-build3300",
    "dotnet-test-xunit": "2.2.0-preview2-build1029"
  },
  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  },
  "runtimes": {
    "win10-arm": {},
    "win10-arm-aot": {},
    "win10-x86": {},
    "win10-x86-aot": {},
    "win10-x64": {},
    "win10-x64-aot": {}
  }
}

错误:

Severity    Code    Description
Error       NU1002  The dependency dotnet-test-xunit 2.2.0-preview2-build1029 does not support framework .NETStandard,Version=v1.0

我可以降级到 dotnet-test-xunit 2.2.0-preview2-build1029 支持的 .netstandard 版本吗?是否有任何已知的解决方法可以将 xUnit 与它一起使用?

由于我对 project.json 和 dotnetcore 有点陌生,我可能会错过一些有用的东西。

  1. 检查 xunit 依赖项的可用版本。我认为 2.2.0 已经是最终版了。

  2. xunit 项目需要是 netcoreapp1.0 而不是 netstandard。

查看他们的网页了解详情。

我建议使用以下版本(这与 asp.net 核心存储库相同,如 Logging:

"dotnet-test-xunit": "1.0.0-*",
"xunit": "2.1.0"

.NET CLI 工具 (dotnet) 支持创建测试项目:

testproj $ dotnet new -t xunittest
Created new C# project in /home/bartonjs/dotnet/testproj.
testproj $ cat project.json

产生:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable"
  },
  "dependencies": {
    "System.Runtime.Serialization.Primitives": "4.1.1",
    "xunit": "2.1.0",
    "dotnet-test-xunit": "1.0.0-rc2-192208-24"
  },
  "testRunner": "xunit",
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": [
        "dotnet5.4",
        "portable-net451+win8"
      ]
    }
  }
}

这些版本可能会更成功。

这对我有用。似乎现有的 xunit 版本还不支持 netstandard 1.6 库。尝试将您的项目 json 更改为 xunit site 中提供的样子。这还假设您创建了一个 .net 核心库项目

{
  "version": "1.0.0-*",
  "testRunner": "xunit",
  "dependencies": {
    "xunit": "2.2.0-beta2-build3300",
    "dotnet-test-xunit": "2.2.0-preview2-build1029"
  },
  "frameworks": {
    "netcoreapp1.0": {
       "dependencies": {
          "Microsoft.NETCore.App": {
             "type": "platform",
             "version": "1.0.0"
           }
         }
      }
   }
}

您可以导入 netcoreapp1.0 TFM 以说服工具依赖项与目标框架兼容:

{ 
  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "xunit": "2.2.0-beta4-build3444",
    "dotnet-test-xunit": "2.2.0-preview2-build1029" 
  },
  "frameworks": {
    "netstandard1.6": {
      "imports": [ "netcoreapp1.0" ]
    }
  }
}

您可以在下面链接的 NuGet 文档中找到 table 个目标框架名字对象 (TFM),包括 table 个 已弃用的框架 ,其中包括dnxcore50(替换为netcoreapp1.0):

https://docs.nuget.org/ndocs/schema/target-frameworks