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

标签 .net unit-testing .net-core xunit.net

我在 .NET Core 上有一些面向 net45 的库和 netstandard1.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"
         ]
      }
   }
}

最佳答案

根据xUnit .NET Core docs ,

You can target both net4xx and netcoreapp simply by adding both frameworks together in your project.json file. When you run dotnet test with multiple framework entries, the system will run all your framework tests, one after another.



这并不意味着您可以拥有针对不同平台(如 .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 中运行测试时,我收到了与您类似的错误消息。我认为这是一个错误,因为 dotnet test在命令行上工作正常。

关于.net - 使用 .NET Core 和 xUnit 同时针对多个框架的单元测试代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38708034/

相关文章:

c# - 事件设计指南

iphone - Xcode 中的单元测试,它是否运行该应用程序?

c# - 在 C# 中使用动态绑定(bind)代替严格类型值的扩展方法

c# - 从批处理文件构建 .net 解决方案

c# -/平台 :anycpu32bitpreferred is not a valid setting for option/target:library or/target:module

c# - Microsoft.Extensions.Logging自定义实现,如何获取调用方方法名?

android - 如何验证方法是否在模拟类的回调中运行?

JavaScript——单元测试子任务

c# - 是否可以在执行代码之前加载服务器端 Blazor 页面 HTML?

c# - .NET Core 6 MVC AddRazorRuntimeCompilation 破坏全局使用