使用.NET Core和xUnit同时针对多个框架进行单元测试代码

9
我在.NET Core上有一些库,目标是net45netstandard1.6,我想对它们进行单元测试。我执行了dotnet new -t xunittest,创建了一个新的测试项目,目标是netcoreapp1.0,以便只测试.NET Core代码。
我试图将其编译为net45的目标,但是测试发现时出现了一系列错误。我的问题是:是否有一种方法可以使用单个测试项目测试针对两个(甚至更多)框架的代码?或者我应该为每个目标创建一个测试项目? 编辑:这里是我的project.json 和我收到的消息:
{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable"
  },
  "dependencies": {
    "xunit": "2.1.0"
  },
  "testRunner": "xunit",
  "frameworks": {
    "net45": {
      "frameworkAssemblies": {
        "System.Runtime": "4.0.0.0"
      }
    },
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        },
        "System.Runtime.Serialization.Primitives": "4.1.1",
        "dotnet-test-xunit": "2.2.0-preview2-build1029"
      },
      "imports": [
        "dotnet5.4",
        "portable-net451+win8"
      ]
    }
  }
}

以下是项目编译后出现的错误:

dotnet-test Error: 0 : [ReportingChannel]: Waiting for message failed System.IO.IOException: Unable to read data om the transport connection: An established connection was aborted by the software in your host machine. ---> stem.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.IO.Stream.ReadByte()
   at System.IO.BinaryReader.ReadByte()
   at System.IO.BinaryReader.Read7BitEncodedInt()
   at System.IO.BinaryReader.ReadString()
   at Microsoft.DotNet.Tools.Test.ReportingChannel.ReadMessages()
>dotnet-test Error: 0 : Unhandled Exception: System.IO.IOException: Unable to read data from the transport nnection: An established connection was aborted by the software in your host machine. ---> stem.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.IO.Stream.ReadByte()
   at System.IO.BinaryReader.ReadByte()
   at System.IO.BinaryReader.Read7BitEncodedInt()
   at System.IO.BinaryReader.ReadString()
   at Microsoft.DotNet.Tools.Test.ReportingChannel.ReadMessages()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object ate)
[ReportingChannel]: Error sending System.IO.IOException: Unable to write data to the transport connection: Cannot cess a disposed object.
Object name: 'System.Net.Sockets.Socket'.. ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.
   at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, cketError& errorCode)
   at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.IO.BinaryWriter.Write7BitEncodedInt(Int32 value)
   at System.IO.BinaryWriter.Write(String value)
   at Microsoft.DotNet.Tools.Test.ReportingChannel.Send(Message message)
dotnet-test Error: 0 : System.IO.IOException: Unable to write data to the transport connection: Cannot access a sposed object.
Object name: 'System.Net.Sockets.Socket'.. ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.
   at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, cketError& errorCode)
   at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.IO.BinaryWriter.Write7BitEncodedInt(Int32 value)
   at System.IO.BinaryWriter.Write(String value)
   at Microsoft.DotNet.Tools.Test.ReportingChannel.Send(Message message)
   at Microsoft.DotNet.Tools.Test.ReportingChannel.SendError(String error)
   at Microsoft.DotNet.Tools.Test.ReportingChannel.SendError(Exception ex)
   at Microsoft.DotNet.Tools.Test.DesignTimeRunner.HandleDesignTimeMessages(ProjectContext projectContext, tnetTestParams dotnetTestParams)
   at Microsoft.DotNet.Tools.Test.DesignTimeRunner.DoRunTests(ProjectContext projectContext, DotnetTestParams tnetTestParams)
   at Microsoft.DotNet.Tools.Test.BaseDotnetTestRunner.RunTests(ProjectContext projectContext, DotnetTestParams tnetTestParams, BuildWorkspace workspace)
   at Microsoft.DotNet.Tools.Test.TestCommand.DoRun(String[] args)

并以此行作为最终结果。
========== Discover test finished: 0 found (0:00:03.0417342) ==========

虽然我知道我只有一个测试(至少目前如此)。

但是,如果我删除net 45的引用,则所有工作都像魅力一样,这是我的工作project.json

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

你遇到了哪些错误?你能否更详细地描述一下你的操作,以便我们能够重现这些错误? - svick
@svick 我刚刚编辑了我的问题,并添加了错误和“project.json”数据。希望这有所帮助。 - Luiso
2个回答

6
根据xUnit .NET Core文档,您可以通过在project.json文件中同时添加两个框架来同时针对net4xxnetcoreapp。当您使用多个框架条目运行dotnet test时,系统会依次运行所有框架测试。这并不意味着您可以拥有单个测试项目引用和测试不同平台的项目(例如.NET 4.5项目和.NET Core项目)。但是,这确实意味着您可以测试针对相同平台集的项目。例如,我有一个类库,它针对.NET 4.5.1和.NET Core两个平台。
{
  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },

  "frameworks": {
    "netstandard1.6": {},
    "net451":  {} 
  },
  "version": "1.0.0-*"
}

我可以在我的测试项目中针对net451netcoreapp1.0进行目标设置,并引用这个类库:
{
  "buildOptions": {
    "debugType": "portable"
  },
  "dependencies": {
    "xunit": "2.2.0-beta2-build3300",
    "dotnet-test-xunit": "2.2.0-preview2-build1029",
    "NetStandardClassLibrary": {
      "target": "project"
    }
  },
  "frameworks": {
    "net451": {},
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": [
        "dotnet5.4",
        "portable-net451+win8"
      ]
    }
  },
  "testRunner": "xunit",
  "version": "1.0.0-*"
}

执行 dotnet test 命令时,测试将会运行两次(每个平台目标运行一次):

λ dotnet test
Project NetStandardClassLibrary (.NETStandard,Version=v1.6) was previously compiled. Skipping compilation.
Project XunitBothFrameworks (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
xUnit.net .NET CLI test runner (64-bit .NET Core win81-x64)
  Discovering: XunitBothFrameworks
  Discovered:  XunitBothFrameworks
  Starting:    XunitBothFrameworks
  Finished:    XunitBothFrameworks
=== TEST EXECUTION SUMMARY ===
   XunitBothFrameworks  Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 0.244s
Project NetStandardClassLibrary (.NETFramework,Version=v4.5.1) was previously compiled. Skipping compilation.
Project XunitBothFrameworks (.NETFramework,Version=v4.5.1) was previously compiled. Skipping compilation.
xUnit.net .NET CLI test runner (64-bit Desktop .NET win81-x64)
  Discovering: XunitBothFrameworks
  Discovered:  XunitBothFrameworks
  Starting:    XunitBothFrameworks
  Finished:    XunitBothFrameworks
=== TEST EXECUTION SUMMARY ===
   XunitBothFrameworks  Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 0.201s
SUMMARY: Total: 2 targets, Passed: 2, Failed: 0.

当我尝试在 Visual Studio 中运行测试时,我得到了一个与你相似的错误信息。我认为这是一个 bug,因为在命令行上运行dotnet test时一切正常。

1
对我来说,下面的project.json很好用,我不必从cmd运行测试。
{
  "version": "0.1.0-*",
  "dependencies": {
    "Moq": "4.5.22",
    "xunit": "2.2.0-beta2-build3300",
    "dotnet-test-xunit": "2.2.0-preview2-build1029",
    "IntegraPay.Domain": { "version": "1.0.0-*","target": "project" },
    "Integrapay.RegistrationApplication": { "version": "", "target": "project" }
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "net451"
      ],
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      }
    }
  },
  "testRunner": "xunit"
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接