{"id":17145,"date":"2016-05-30T13:43:40","date_gmt":"2016-05-30T20:43:40","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/?p=17145"},"modified":"2019-02-14T17:33:59","modified_gmt":"2019-02-15T01:33:59","slug":"announcing-mstest-framework-support-for-net-core-rc2-asp-net-core-rc2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/announcing-mstest-framework-support-for-net-core-rc2-asp-net-core-rc2\/","title":{"rendered":"Announcing MSTest Framework support for .NET Core RC2 \/ ASP.NET Core RC2"},"content":{"rendered":"<p>.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 <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/announcing-net-core-rc2\/\">.NET team blog<\/a> and the <a href=\"https:\/\/blogs.msdn.microsoft.com\/webdev\/2016\/05\/16\/announcing-asp-net-core-rc2\/\">.NET Web Development team blog<\/a>.<\/p>\n<p>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.<\/p>\n<p>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:<\/p>\n<ol>\n<li>Installing the SDK<\/li>\n<li>Creating a class library project<\/li>\n<li>Adding references for MSTest<\/li>\n<li>Writing the tests<\/li>\n<li>Running the tests from Visual Studio<\/li>\n<li>Running the tests from the console<\/li>\n<li>Targetting desktop .NET<\/li>\n<\/ol>\n<h3>Installing the SDK<\/h3>\n<p>Install the Visual Studio official MSI installer from <a href=\"https:\/\/www.microsoft.com\/net\/core\">https:\/\/www.microsoft.com\/net\/core<\/a><\/p>\n<h3>Creating a class library project<\/h3>\n<p>Create a .NET Core Class Library application. Open Visual Studio, and choose File | New | Project:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/05\/img13.jpg\"><img decoding=\"async\" class=\"alignnone wp-image-17195 size-full\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2016\/05\/img13.jpg\" alt=\"img1\" width=\"955\" height=\"667\" \/><\/a><\/p>\n<h3>Adding references for MSTest<\/h3>\n<p>From nuget.org, install the MSTest.TestFramework package (shown below).<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/05\/img23.jpg\"><img decoding=\"async\" class=\"alignnone wp-image-17205 size-full\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2016\/05\/img23.jpg\" alt=\"img2\" width=\"665\" height=\"92\" \/><\/a><\/p>\n<p>Now, install the runner \u2013 look for the dotnet-test-mstest package, and install it:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/05\/img3-new.jpg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-17405\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2016\/05\/img3-new.jpg\" alt=\"img3-new\" width=\"674\" height=\"82\" \/><\/a><\/p>\n<p><em><span style=\"color: #000000;font-family: Calibri\">[<strong>Editor\u2019s note<\/strong>: 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.]<\/span><\/em><\/p>\n<p>Open the <code>project.json<\/code> file in the solution. You will already see the packages you just installed mentioned under &#8220;dependencies&#8221;. We need to add the &#8220;testRunner&#8221; property and set that to &#8220;mstest&#8221;. To make it easier, just replace the content of the <code>project.json<\/code> file with the following:<\/p>\n<pre><code>{\n  <span style=\"color: #3366ff\">\"version\"<\/span>: <span style=\"color: #ff0000\">\"1.0.0-*\"<\/span>,\n\n  <span style=\"color: #3366ff\">\"testRunner\"<\/span>: <span style=\"color: #ff0000\">\"mstest\"<\/span>,\n\n  <span style=\"color: #3366ff\">\"dependencies\"<\/span>: {\n    <span style=\"color: #3366ff\">\"dotnet-test-mstest\"<\/span>: <span style=\"color: #ff0000\">\"1.0.1-preview\"<\/span>,\n    <span style=\"color: #3366ff\">\"MSTest.TestFramework\"<\/span>: <span style=\"color: #ff0000\">\"1.0.0-preview\"<\/span>\n  },\n\n  <span style=\"color: #3366ff\">\"frameworks\"<\/span>: {\n    <span style=\"color: #3366ff\">\"netcoreapp1.0\"<\/span>: {\n      <span style=\"color: #3366ff\">\"imports\"<\/span>: [\n        <span style=\"color: #ff0000\">\"dnxcore50\"<\/span>,\n        <span style=\"color: #ff0000\">\"portable-net45+win8\"<\/span>\n      ],\n  \n      <span style=\"color: #3366ff\">\"dependencies\"<\/span>: {\n        <span style=\"color: #3366ff\">\"Microsoft.NETCore.App\"<\/span>: {\n          <span style=\"color: #3366ff\">\"version\"<\/span>: <span style=\"color: #ff0000\">\"1.0.0-rc2-3002702\"<\/span>,\n          <span style=\"color: #3366ff\">\"type\"<\/span>: <span style=\"color: #ff0000\">\"platform\"<\/span>\n        }\n      }\n    }\n  }\n}\n<\/code><\/pre>\n<p>Notice that the class library project we created is getting marked as an application (<code>netcoreapp1.0<\/code>). That is because when using .NET CLI for testing, unit test projects are actually an application, not a class library. That application\u2019s <em>Main<\/em> method is provided by the runner.<\/p>\n<h3>Writing the tests<\/h3>\n<p>Visual Studio would have automatically created a file named <code>Class1.cs<\/code>. Open that file and replace its content with the following:<\/p>\n<pre><code><span style=\"color: #0000ff\">using<\/span> Microsoft.VisualStudio.TestTools.UnitTesting;\n\n<span style=\"color: #0000ff\">namespace<\/span> SampleNetCoreUnitTests\n{\n  [<span style=\"color: #33cccc\">TestClass<\/span>]\n  <span style=\"color: #0000ff\">public class<\/span> <span style=\"color: #33cccc\">TestClass<\/span>\n  {\n    [<span style=\"color: #33cccc\">TestMethod<\/span>]\n    <span style=\"color: #0000ff\">public void<\/span> TestMethodPassing()\n    {\n      <span style=\"color: #33cccc\">Assert<\/span>.IsTrue(<span style=\"color: #0000ff\">true<\/span>);\n    }\n\n    [<span style=\"color: #33cccc\">TestMethod<\/span>]\n    <span style=\"color: #0000ff\">public void<\/span> TestMethodFailing()\n    {\n      <span style=\"color: #33cccc\">Assert<\/span>.IsTrue(<span style=\"color: #0000ff\">false<\/span>);\n    }\n  }\n}\n<\/code><\/pre>\n<h3>Running the tests from Visual Studio<\/h3>\n<p>Open the Test Explorer window (Test | Windows | Test Explorer in Visual Studio).\nBuild the solution, and you should see the tests as follows:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/05\/img41.jpg\"><img decoding=\"async\" class=\"alignnone wp-image-17225 size-full\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2016\/05\/img41.jpg\" alt=\"img4\" width=\"331\" height=\"198\" \/><\/a><\/p>\n<p>Click on &#8220;Run All&#8221; to run the tests.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/05\/img51.jpg\"><img decoding=\"async\" class=\"alignnone wp-image-17235 size-full\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2016\/05\/img51.jpg\" alt=\"img5\" width=\"331\" height=\"402\" \/><\/a><\/p>\n<h3>Running tests from the console<\/h3>\n<p>Open a command prompt and navigate to the folder containing the solution. Type <code>dotnet test<\/code> to run the .NET CLI test runner:<\/p>\n<pre><span style=\"color: #000000\"><code>D:SamplesdotNetCoreTestssrcdotNetCoreTests&gt;dotnet test\nProject dotNetCoreTests (.NETFramework,Version=v4.5.1) was previously compiled. Skipping compilation.\nDiscovering Tests ...\nExecuting Tests ...\nPassed   TestMethodPassing\nFailed   TestMethodFailing\nError Message:\n   Assert.IsTrue failed.\nStack Trace:\n   at SampleNetCoreUnitTests.TestClass.TestMethodFailing() in D:SamplesdotNetCoreTestssrcdotNetCoreTestsClass1.cs:line 17\n============ Test Run Summary ============\nTotal tests: 2. Passed: 1. Failed: 1. Skipped: 0\nTest Run Failed.\nSUMMARY: Total: 1 targets, Passed: 1, Failed: 0.\n<\/code><\/span><\/pre>\n<p>The tests are discovered and executed as expected.<\/p>\n<h3>Targeting the desktop .NET<\/h3>\n<p>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 <code>project.json<\/code> to use this frameworks section instead:<\/p>\n<pre><code><span style=\"color: #3366ff\">\"frameworks\"<\/span>: {\n  <span style=\"color: #3366ff\">\"net451\"<\/span>: { }\n}\n<\/code><\/pre>\n<h3>Summary<\/h3>\n<p>There, it is as simple as that &#8211; MSTest support for .NET Core 1.0 RC2 and ASP.NET Core 1.0 RC2, fully integrated with Visual Studio.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>.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 [&hellip;]<\/p>\n","protected":false},"author":765,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[226,1,252],"tags":[],"class_list":["post-17145","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ci","category-devops","category-testing"],"acf":[],"blog_post_summary":"<p>.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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/17145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/765"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=17145"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/17145\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=17145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=17145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=17145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}