单元测试 .NET Standard 1.6 库
Unit testing a .NET Standard 1.6 library
我无法找到有关如何对 .NET Standard 1.6 class 库(可以从 .NET Core 项目引用)进行单元测试的最新文档。
这是我的 project.json
图书馆的样子:
{
"supports": {},
"dependencies": {
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"NETStandard.Library": "1.6.0",
"Portable.BouncyCastle": "1.8.1.2"
},
"frameworks": {
"netstandard1.6": {}
}
}
现在剩下的任务是能够创建某种可以进行单元测试的项目。目标是使用 xUnit,因为这似乎是 .NET Core 团队正在推动的。
我继续创建了另一个 .NET 可移植库项目,它有一个 project.json 如下所示:
{
"supports": {},
"dependencies": {
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"NETStandard.Library": "1.6.0",
"xunit": "2.2.0-beta4-build3444",
"xunit.runner.visualstudio": "2.1.0"
},
"frameworks": {
"netstandard1.6": {
}
}
}
我在该项目中的测试 class 如下所示:
using USB.EnterpriseAutomation.Security.DotNetCore;
using Xunit;
namespace Security.DotNetCore.Test
{
public class AesEncryptionHelperTests
{
[Fact]
public void AesEncryptDecrypt()
{
var input = "Hello world!";
var encrypted = AesEncryptionHelper.AesEncrypt(input);
var decrypted = AesEncryptionHelper.AesDecrypt(encrypted);
Assert.Equal(input, decrypted);
}
}
}
当我继续构建该项目时,测试资源管理器没有看到我的任何测试。
我如何着手创建能够测试此库的单元测试?
我目前有一个使用 xunit 2.1.0 和 dotnet-test-xunit 2.2.0-preview2-build1029 的工作项目。
这是我的project.json
单元测试项目:
{
"dependencies": {
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"MyProject.Library": {
"target": "project",
},
"xunit": "2.1.0"
},
"description": "Unit tests",
"frameworks": {
"netcoreapp1.0": {
"imports": "dotnet"
}
},
"testRunner": "xunit"
}
这在命令行(通过 dotnet test
)和 Visual Studio 2015 Test Explorer 中都有效。
我认为 dotnet-test-xunit
已被弃用,但我不确定。在 project.json 消失后,上述所有内容都可能会发生变化,但这在今天有效。
我在 xUnit 的 GitHub 页面上发现了这个问题:https://github.com/xunit/xunit/issues/1032
正如 Brad Wilson 所解释的,NETStandard 库必须使用 dotnet 核心库或完整的 .Net Framework 库进行测试。
在我的例子中,我将我的单元测试库制作成一个完整的 "classic desktop" 库,并且 Test Explorer 能够 运行 我的测试。
我无法找到有关如何对 .NET Standard 1.6 class 库(可以从 .NET Core 项目引用)进行单元测试的最新文档。
这是我的 project.json
图书馆的样子:
{
"supports": {},
"dependencies": {
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"NETStandard.Library": "1.6.0",
"Portable.BouncyCastle": "1.8.1.2"
},
"frameworks": {
"netstandard1.6": {}
}
}
现在剩下的任务是能够创建某种可以进行单元测试的项目。目标是使用 xUnit,因为这似乎是 .NET Core 团队正在推动的。
我继续创建了另一个 .NET 可移植库项目,它有一个 project.json 如下所示:
{
"supports": {},
"dependencies": {
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"NETStandard.Library": "1.6.0",
"xunit": "2.2.0-beta4-build3444",
"xunit.runner.visualstudio": "2.1.0"
},
"frameworks": {
"netstandard1.6": {
}
}
}
我在该项目中的测试 class 如下所示:
using USB.EnterpriseAutomation.Security.DotNetCore;
using Xunit;
namespace Security.DotNetCore.Test
{
public class AesEncryptionHelperTests
{
[Fact]
public void AesEncryptDecrypt()
{
var input = "Hello world!";
var encrypted = AesEncryptionHelper.AesEncrypt(input);
var decrypted = AesEncryptionHelper.AesDecrypt(encrypted);
Assert.Equal(input, decrypted);
}
}
}
当我继续构建该项目时,测试资源管理器没有看到我的任何测试。
我如何着手创建能够测试此库的单元测试?
我目前有一个使用 xunit 2.1.0 和 dotnet-test-xunit 2.2.0-preview2-build1029 的工作项目。
这是我的project.json
单元测试项目:
{
"dependencies": {
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"MyProject.Library": {
"target": "project",
},
"xunit": "2.1.0"
},
"description": "Unit tests",
"frameworks": {
"netcoreapp1.0": {
"imports": "dotnet"
}
},
"testRunner": "xunit"
}
这在命令行(通过 dotnet test
)和 Visual Studio 2015 Test Explorer 中都有效。
我认为 dotnet-test-xunit
已被弃用,但我不确定。在 project.json 消失后,上述所有内容都可能会发生变化,但这在今天有效。
我在 xUnit 的 GitHub 页面上发现了这个问题:https://github.com/xunit/xunit/issues/1032
正如 Brad Wilson 所解释的,NETStandard 库必须使用 dotnet 核心库或完整的 .Net Framework 库进行测试。
在我的例子中,我将我的单元测试库制作成一个完整的 "classic desktop" 库,并且 Test Explorer 能够 运行 我的测试。