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

查看:81
本文介绍了单元测试.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 的样子:

{
  "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可移植库项目,该项目具有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测试)和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天全站免登陆