Unit testing a .NET Standard 1.6 library

blgrnboy picture blgrnboy · Dec 27, 2016 · Viewed 9.9k times · Source

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).

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

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.

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?

Answer

Brett Mathe picture Brett Mathe · Mar 25, 2017

I found the issue posted on xUnit's GitHub page here: https://github.com/xunit/xunit/issues/1032

As Brad Wilson explains, NETStandard libraries must be tested with either a dotnet core library or a full .Net Framework library.

In my case I made my unit test library a full "classic desktop" library and Test Explorer was able to run my tests.