Announcing MSTest Framework support for .NET Core RC2 / ASP.NET Core RC2

Pratap Lakshman

.NET Core RC2 and ASP.NET Core RC2 released just a couple of weeks back. They feature the introduction of the .NET CLI, major changes to the .NET Core SDK (formerly called DNX), the rebranding of ASP.NET 5 to ASP.NET Core, and more. You can read about these on the .NET team blog and the .NET Web Development team blog.

We are now pleased to announce MSTest framework support for these releases! The framework and its allied packages are available on NuGet now. This is a preview release, and we are looking for your feedback to make this a robust rollout for RTM.

In this post, we show you how to write and run your first set of MSTest based tests for this release. Here are the steps:

  1. Installing the SDK
  2. Creating a class library project
  3. Adding references for MSTest
  4. Writing the tests
  5. Running the tests from Visual Studio
  6. Running the tests from the console
  7. Targetting desktop .NET

Installing the SDK

Install the Visual Studio official MSI installer from https://www.microsoft.com/net/core

Creating a class library project

Create a .NET Core Class Library application. Open Visual Studio, and choose File | New | Project:

img1

Adding references for MSTest

From nuget.org, install the MSTest.TestFramework package (shown below).

img2

Now, install the runner – look for the dotnet-test-mstest package, and install it:

img3-new

[Editor’s note: Thank you @IanGriffiths and @BenHysell for reporting an issue with the package we published earlier. We have since uploaded a fixed version, as shown in the updated image above.]

Open the project.json file in the solution. You will already see the packages you just installed mentioned under “dependencies”. We need to add the “testRunner” property and set that to “mstest”. To make it easier, just replace the content of the project.json file with the following:

{
  "version": "1.0.0-*",

  "testRunner": "mstest",

  "dependencies": {
    "dotnet-test-mstest": "1.0.1-preview",
    "MSTest.TestFramework": "1.0.0-preview"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dnxcore50",
        "portable-net45+win8"
      ],
  
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.0.0-rc2-3002702",
          "type": "platform"
        }
      }
    }
  }
}

Notice that the class library project we created is getting marked as an application (netcoreapp1.0). That is because when using .NET CLI for testing, unit test projects are actually an application, not a class library. That application’s Main method is provided by the runner.

Writing the tests

Visual Studio would have automatically created a file named Class1.cs. Open that file and replace its content with the following:

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SampleNetCoreUnitTests
{
  [TestClass]
  public class TestClass
  {
    [TestMethod]
    public void TestMethodPassing()
    {
      Assert.IsTrue(true);
    }

    [TestMethod]
    public void TestMethodFailing()
    {
      Assert.IsTrue(false);
    }
  }
}

Running the tests from Visual Studio

Open the Test Explorer window (Test | Windows | Test Explorer in Visual Studio). Build the solution, and you should see the tests as follows:

img4

Click on “Run All” to run the tests.

img5

Running tests from the console

Open a command prompt and navigate to the folder containing the solution. Type dotnet test to run the .NET CLI test runner:

D:SamplesdotNetCoreTestssrcdotNetCoreTests>dotnet test
Project dotNetCoreTests (.NETFramework,Version=v4.5.1) was previously compiled. Skipping compilation.
Discovering Tests ...
Executing Tests ...
Passed   TestMethodPassing
Failed   TestMethodFailing
Error Message:
   Assert.IsTrue failed.
Stack Trace:
   at SampleNetCoreUnitTests.TestClass.TestMethodFailing() in D:SamplesdotNetCoreTestssrcdotNetCoreTestsClass1.cs:line 17
============ Test Run Summary ============
Total tests: 2. Passed: 1. Failed: 1. Skipped: 0
Test Run Failed.
SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.

The tests are discovered and executed as expected.

Targeting the desktop .NET

In addition to .NET Core, the .NET CLI runner can run tests targeting desktop .NET (minimum version 4.5.1) as well. To target desktop .NET, update the project.json to use this frameworks section instead:

"frameworks": {
  "net451": { }
}

Summary

There, it is as simple as that – MSTest support for .NET Core 1.0 RC2 and ASP.NET Core 1.0 RC2, fully integrated with Visual Studio.

0 comments

Discussion is closed.

Feedback usabilla icon