MSTest is a command line utility from Microsoft that executes unit tests created in Visual Studio 2005, 2008, 2010 and is still available for backwards compatibility with older versions in Visual Studio 2012 and 2013. Microsoft recommends running the tests in VSTest.Console in later versions.
MSTest V2 is now supported on .NET Core 1.0 RTM. API changes in .NET Core 1.0 leading up to RTM (see #7754), were blocking users of the MSTest V2 packages we had released for .NET Core 1.0 RC2. Not any more (see #10713), we are pleased to note.
This continues to be a preview release, and comes with following changes:
- dotnet-test-mstest is built against .NET Core 1.0 RTM.
- Deprecated support for netstandardapp1.5 moniker as announced here.
- Fixes for bugs reported by you:
- “the test suite returns an exit code of zero even when there are failing tests” (from a comment in our earlier post)
- “AssemblyCleanup throwing Exception in ASP.NET Core Project Targeting .NET 4.5.1” (from a comment on the forum)
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:Adding references for MSTest
From nuget.org, install the MSTest.TestFramework package (shown below).
Now, install the runner – look for the updated dotnet-test-mstest package, and install it:
Open the project.json file in the solution. You will
already see the packages you just installed mentioned under
“dependencies”. 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 (and note the differences in version numbers from our earlier post):{
"version": "1.0.0-*",
"testRunner": "mstest",
"dependencies": {
"dotnet-test-mstest": "1.1.1-preview",
"MSTest.TestFramework": "1.0.1-preview"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dnxcore50",
"portable-net45+win8"
],
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
}
}
}
}
}
Writing the tests
Visual Studio would have automatically created a file namedClass1.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);
}
}
}
Testing using the Visual Studio IDE
Open the Test Explorer window (Test | Windows | Test Explorer in Visual Studio).Build the solution, and you should see the tests as follows:
Click on “Run All” to run the tests.

Testing using the .NET CLI
Open a command prompt and navigate to the folder containing the solution, and typedotnet test to execute the tests:
Use the dotnet test --list command to discover tests.
Use the dotnet test --test to execute specific tests.
Testing using vstest.console.exe
The venerable vstest.console.exe may be used as well to run tests. Open a Developer Command Prompt, and just point vstest to the project.json file using the built-in Vsix based adapter to execute the tests:vstest.console.exe project.json /UseVsixExtensions:true
Use vstest.console.exe project.json /UseVsixExtensions:true /logger:trx to generate reports too.
Testing using VSTS
To execute the tests in VSTS CI, check in the code and create a build definition with the following steps:- a Command Line step that invokes the
dotnet restore(i.e. “Tool” set todotnetand “Arguments” set torestore) - a Visual Studio Build step with the default settings
- a Visual Studio Test step, with “TestAssembly” set to
**\project.json, and “Other console options” set to/UseVsixExtensions:true /logger:trx
Build completed, tests run (with one of the tests failing as expected), and test results published!






No comments:
Post a Comment