.NET Standard 1.6 库的单元测试 [英] Unit testing a .NET Standard 1.6 library

查看:35
本文介绍了.NET Standard 1.6 库的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到有关如何对 .NET Standard 1.6 类库(可以从 .NET Core 项目中引用)进行单元测试的最新文档.

I am having trouble finding up to date documentation on how to unit test a .NET Standard 1.6 class library (which can be referenced from a .NET Core project).

这是我的 project.json 在我的库中的样子:

Here is what my project.json looks like for my library:

{
  "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 团队正在推动的.

Now the left over task is to be able to create some sort of a project that can do the unit testing. The goal is to use xUnit since it seems that this is what the .NET Core team is pushing.

我继续创建了另一个 .NET Portable 库项目,该项目的 project.json 如下所示:

I went ahead and created another .NET Portable library project that has a project.json that looks like this:

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

    }
  }
}

该项目中我的测试类如下所示:

My test class within that project looks like this:

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);
        }
    }
}

当我继续构建该项目时,测试资源管理器没有看到我的任何测试.

When I go ahead and build that project, the Test Explorer is not seeing any of my tests.

如何创建能够测试此库的单元测试?

How do I go about creating a unit test that's able to test this library?

推荐答案

我目前有一个使用 xunit 2.1.0 和 dotnet-test-xunit 2.2.0-preview2-build1029 的工作项目.

I currently have a working project using xunit 2.1.0 and dotnet-test-xunit 2.2.0-preview2-build1029.

这是我用于单元测试项目的project.json:

This is my project.json for the unit test project:

{
  "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 测试资源管理器.

This works both on the command line (via dotnet test) and in the Visual Studio 2015 Test Explorer.

我认为 dotnet-test-xunit 已被弃用,但我不确定.在 project.json 消失后,上述所有内容都可能会发生变化,但今天有效.

I think that dotnet-test-xunit is being deprecated, but I'm not sure. All of the above will likely change after project.json goes away, but this works today.

这篇关于.NET Standard 1.6 库的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆