我在.NET核心上有一些库,它们的目标是net45和netstandard1.6,我想对它们进行单元测试。我做了dotnet new -t xunittest,创建了一个针对netcoreapp1.0的新测试项目,以便它只测试.NET核心代码。
我还试图将它编译为同时针对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"
]
}
}
}发布于 2016-08-04 20:02:55
根据xUnit .NET核心文档的说法
您可以简单地通过将两个框架添加到您的
net4xx文件中来实现netcoreapp和project.json的目标。当您使用多个框架条目运行dotnet test时,系统将一个接一个地运行所有框架测试。
这并不意味着您可以拥有针对不同平台(如.NET 4.5项目和.NET核心项目)的单个测试项目引用和测试项目。但这确实意味着您可以测试针对同一组平台的项目。
例如,我有一个类库,它同时针对.NET 4.5.1和.NET Core:
{
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {},
"net451": {}
},
"version": "1.0.0-*"
}我可以在我的测试项目中针对net451和netcoreapp1.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中运行测试时,我收到了与您类似的错误消息。我认为这是一个bug,因为命令行上的dotnet test工作得很好。
发布于 2016-09-30 08:16:34
对我来说,下面的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"
}https://stackoverflow.com/questions/38708034
复制相似问题