ASP.NET 5: Running tests on K and Visual Studio

Using new ASP.NET tooling it is also very easy to run tests on command line. We don’t need Visual Studio to be installed on machine where we want to run tests and we can use this fancy K-stuff to make tests run. This post will show you how to write and run tests using new Visual Studio and K.

Unit tests project

To keep demo simple I am using simple pointless tests. One of these succeeds because it does nothing and the other one fails with exception. Tests are shown on the image below.

ASP.NET MVC 6: Sample tests

Notice that our tests project has reference to xUnit K-Runner. This runner is used by K-stuff at next step.

Running tests

We can run tests from Visual Studio. If some test fails with exception – and we have one failing test – then debugger stops at this point and we can investigate the exception thrown by test.

ASP.NET MVC 6: Test fails in Visual Studio

Take a look also at output window on image above. You can see that Visual Studio is using K Language Runtime to run tests. It will keep command line window open while tests are running but I think it will be removed when new Visual Studio gets to RTM.

Configuration of tests project

To find out how K understands what to do with tests let’s open project.json file in tests project.

ASP.NET MVC 6: Tests project configuration

Take a look at “commands” section. There is command called “test” defined and the value of command is the name of package that contains class that is able to do something with our project when K asks it. In our case this class is K runner for xUnit.

Running tests on command line

Next let’s try to run tests on command line without any involvement by Visual Studio. All we have to do is to open command prompt, move to our tests project folder and run the following command:

k test

Here’s the command line out put for our tests.

ASP.NET MVC 6: Running tests on K

Similar way we can run our web applications from command line using lightweight web server coming with ASP.NET vNext. We don’t need IIS or IIS Express or any other hardcore web server installed on machine where we want to run our web stuff.

Wrapping up

New K-tools support besides multi-platform targeting also command line environment very well. In ideal we don’t need Visual Studio or MSBuild to be installed to machines where we want to run our tests. Of course, it’s possible that there are more complex tests or tests on components that are not ready for use in isolation from Visual Studio. But the current trend is to give us lightweight and simple to use tools that have no complex dependencies to programs and components that make it hard for us to set up special tests environment.

Leave a Reply

Your email address will not be published. Required fields are marked *