How do you test a .NET standard project? Do you need frameworks like nUnit or xUnit? What type of project do you need to write tests? After reading this tutorial, you are able to create a Visual Studio Solution containing a .NET standard class library including a .NET core test project without the use of any test framework.

What project type do you need to test a .NET standard project?

new project

using System;

namespace Lib
{
public class Class1
{
public string SayHello() => "Hello!";
}
}

Testing a .NET Standard library

.NET Standard libraries are tested with unit test projects of type .NET Framework or .NET Core projects. In this tutorial you will use a .NET Core project

add project

nuget

Note: you can also add a .NET Core unit test project in Visual Studio. This will add the nuget packages automatically. For this tutorial, I think it is useful to show that a unit test project is a regular class library plus some added packages

reference

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Lib.Tests
{
[TestClass]
public class TestClass1
{
[TestMethod]
public void Test1()
{
    var cl = new Class1();
    Assert.AreEqual(cl.SayHello(), "Hell!");
}
}
}

The test will fail:

fail test

Assert.AreEqual(cl.SayHello(), "Hell!");

to

Assert.AreEqual(cl.SayHello(), "Hello!");

This time the test should succeed:

success

Written by Loek van den Ouweland on 2018-02-10.
Questions regarding this artice? You can send them to the address below.