C# 对.NET标准1.6库进行单元测试

C# 对.NET标准1.6库进行单元测试,c#,.net,.net-core,xunit.net,project.json,C#,.net,.net Core,Xunit.net,Project.json,我很难找到关于如何对.NET标准1.6类库(可以从.NET核心项目中引用)进行单元测试的最新文档 以下是我的库的project.json外观: { "supports": {}, "dependencies": { "Microsoft.NETCore.Portable.Compatibility": "1.0.1", "NETStandard.Library": "1.6.0", "Portable.BouncyCastle": "1.8.1.2" },

我很难找到关于如何对.NET标准1.6类库(可以从.NET核心项目中引用)进行单元测试的最新文档

以下是我的库的
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核心团队正在推动的

我继续创建了另一个.NET Portable library项目,它有一个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": {

    }
  }
}
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);
        }
    }
}
我在该项目中的测试类如下所示:

{
  "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": {

    }
  }
}
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测试资源管理器中都有效


我认为,
dotnettestxunit
正在被弃用,但我不确定。在project.json消失后,上述所有内容都可能会发生变化,但这在今天起作用。

我在xUnit的GitHub页面上发现了这个问题:

正如Brad Wilson所解释的,NETStandard库必须使用dotnet核心库或完整的.Net Framework库进行测试


在我的例子中,我使我的单元测试库成为一个完整的“经典桌面”库,并且测试资源管理器能够运行我的测试。

Visual Studio 2017 RC确实需要一个不同的设置,我昨天试过了。@LexLi很高兴知道。我确信升级时我必须再次解决所有这些问题。:)我在这里附上了这些信息,这个链接(用于SDK预览3)不会永远有效,但是微软应该在将来的某个地方有一个更新版本。有人在VS 2017 RTM中发现了这一点吗?当我尝试测试NetStandard库时,找不到我的测试。我参考的是xunit的2.2版和xunit.runner.visualstudio。