ASP.NET Core 1.0’s DNX and MSTest

Intro

DNX is .NET’s new cross platform Execution Environment that was developed next to ASP.NET core 1.0 (formerly known as ASP.NET 5)[1]. I came across multiple sources (here for instance [2]) that claimed you can’t write tests for code based on DNX with MSTest. And although I prefer xUnit I felt bad for the MSTest community, so I decided to spend a little time on this and found that MSTest works perfectly fine! You can check out the code on my GitHub [3].

Simple DNX project

I’ve made this example without Visual Studio, just to show better what DNX is. So open your favourite file explorer and make sure at the location of your choice the folders are added as follows:

DnxAndMsTestFolders
Figure 1: Folder structure

Make at the root folder “DnxAndMsTest” a “global.json” with the following JSON in it via your favourite text editor:

global.json
Figure 2: Global.json

Side note: It’s not required, but I’ve used a “src” folder and a “test” folder for this example to keep the project clean. Via the “projects” property we can tell DNX which folders need to be included. And we can set the minimal required DNX version, which is set on “1.0.0-rc1-update1” for this example.

Then in the “SomeDnxProject” folder add a “project.json”, a “Program.cs” file and a “MessageMaker.cs” file.

Open them in your favourite text editor and add the following JSON for the ” project.json” file:

project.json
figure 3: project.json

Add the following code to the “Program.cs” file:

using System;

namespace SomeDnxProject
{
    public class Program
    {
        public static void Main(string[] args)
        {
			var messageMaker = new MessageMaker();
			Console.WriteLine(messageMaker.GetMessage());
        }
    }
}

And the following code to the “MessageMaker.cs” file:

using System;

namespace SomeDnxProject
{
	public class MessageMaker {
		public string GetMessage(){
			return "Hello from DNX!";
		}
	}
}

Side note: The “project.json” is required, because DNX uses this file for all its configurations. The “public static void Main” method in “Program.cs” file is required because DNX uses this as its entry point to run the application. “MessageMaker.cs” simply contains a method which we can simply test with MSTest later on.

Then in the “SomeDnxProject.MsTest” folder add the files “project.json”, “Program.cs” and “MessageMakerTests.cs”.

The “project.json” contains the json:

project.test.json
Figure 4: project.json for test project

In the “Program.cs” file you add:

using System;

namespace SomeDnxProject.MsTest
{
    public class Program
    {
        public static void Main(string[] args)
        {
			//Dummy Main method so DNX stops complaining.
        }
    }
}

Then add to the “MessageMakerTests.cs” the following code:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using SomeDnxProject;

namespace SomeDnxProject.MSTest
{
	[TestClass]
	public class MessageMakerTests
	{
		public MessageMakerTests()
		{
		}

		[TestMethod]
		public void GetMessage_GivenNone_ExpectedDefault()
		{
			var expectedMessage = "Hello from DNX!";
			var messageMaker = new MessageMaker();
			var message = messageMaker.GetMessage();
			Assert.AreEqual(expectedMessage, message);
		}

	}
}

Go back to the root of the project “DnxAndMsTest” in a console and type “DNU restore” to create the “project.lock.json” files which DNX needs to run. Then navigate to “SomeDnxProject” folder and type “DNX run” to test everything. You should see the following:

DnxAndMsTestRun
Figure 5: DNX run

If not, then you need to check if your installation of DNX is correct and if you followed the above steps correctly. But if you see the above result you can now navigate to the “SomeDnxProject.MsTest” folder and type “DNX test” and see the following result:

DnxAndMsTestPassed
Figure 6: DNX passed

MSTest.Runner.Dnx

The key component is the assembly “MSTest.Runner.Dnx” which can be acquired via NuGet [4]. It is available since December 2015, so it is understandable that is has been missed by others. Especially because with the release of ASP.NET 5 it seemed xUnit was endorsed. But this assembly makes sure your MSTest tests get discovered, also in Visual Studio in the Test Exlorer by the way. I’ve tried it in an ASP.NET Core 1.0 project.

Conclusion

I hope I can make some MSTest users happy. You can work on the cutting edge and still use your beloved MSTest if you want. Make sure you check out my GitHub [3] for the necessary files and if any MSTest user is really happy now, let me know!

Links

  1. DNX overview: https://docs.asp.net/en/latest/dnx/overview.html
  2. Unit testing ASP.NET Core 1.0: http://www.centare.com/asp-net-core-1-0-unit-testing/
  3. Code on GitHub: https://github.com/DannyvanderKraan/DnxAndMsTest
  4. MSTest.Runner.Dnx: https://www.nuget.org/packages/MSTest.Runner.Dnx/1.0.0-rc1
Advertisements

About Danny

Bachelor in Commercial ICT MCTS Winforms .NET 2.0 MCTS ASP.NET 3.5 PSM I
This entry was posted in .NET programming, ASP.NET Core 1.0, DNX, MSTest and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s