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

路易索

我对.NET的核心部分图书馆哪些目标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"
         ]
      }
   }
}
内特·巴贝蒂尼

根据xUnit .NET Core文档

您可以同时定位这两个目标net4xxnetcoreapp只需将两个框架一起添加到project.json文件中即可。当您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中运行测试时,收到与您类似的错误消息。我认为这是一个错误,因为dotnet test在命令行上工作正常。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在使用 xunit 和 .net core 3.1 进行单元测试时获取记录器

使用 xunit 和 moq 进行 net core api 控制器单元测试

使用 xUnit 和 Moq 在 .NET 5 中对 FluentEmail 进行单元测试

如果我必须同时针对Dot Net Core 3.0和.Net Framework 4.7.2,我应该在库中使用哪个.Net标准版本。

是否应针对.NET标准库而不是.NET Core的框架进行单元测试项目?

ASP.Net MVC:使用ValidationAttribute和IClientValidatable时如何编写单元测试代码

如何使用.Net Core和Coverlet查看友好的单元测试报告?

使用Moq .net Core进行单元测试文件上传

使用xUnit测试.Net Core库

如何确定一个项目正在使用哪个.net单元测试框架?

如何识别 Visual Studio、.NET 项目中使用的单元测试框架?

如何针对使用matplotlib的代码编写单元测试?

在.net core和.net框架中使用.net标准包

使用.NET Core使用xunit生成结果报告测试

如何使用Xunit对ASP.net中的[System.Web.Http.Authorize]过滤器进行单元测试

使用Moq和xUnit对服务进行单元测试

如何在 ASP.NET Core 中使用 mysql 进行单元测试、actionresult

如何在 .NET Core 3.1 中对使用 ExecuteSqlRawAsync 调用存储过程的函数进行单元测试?

如何使用Moq对.NET Core 3.1中的LoggerMessage.Define()进行单元测试?

如何在ASP.NET Core中使用ILogger进行单元测试

使用.NET Core MSTest运行单元测试:“未找到以下TestContainer ...”

使用JWT声明对.NET Core Web API控制器进行单元测试

使用ASP.NET Core在单元测试中模拟POST请求

如何使用依赖注入.net Core 2进行单元测试

Net Core:如何使用数据库行简化单元测试存储库和服务

Net Core:使用Xunit对工作单元模式和DBContext执行依赖注入

使用Asp.Net Web Api和客户筛选器进行单元测试

使用 XUnit 和 AspNetCore.TestHost 测试 .net core web api 控制器文件上传

如何为使用AutoMapper和依赖注入的.net core 2.0服务编写xUnit测试?