F# Xunit with .NET Core 2.0, VsCode and Ionide – Run all tests with keyboard shortcut

A while back I wrote a post about F# unit testing in vscode with ionide

With the release of .NET Core 2.0 this week, I was keen to explore it and, in the process, I found a really nice work flow for running Xunit tests in VsCode. Using a VsCode task and the .NET core CLI, I can simply run all Xunit tests with one simple keyboard shortcut. There’s a few steps to get it set up but really its very quick to set up and, once you do it once, its quicker for any subsequent solutions that you use it on.

To show this in action, I went through a set up of a new solution using .NET Core 2.0 today and documented all my steps below. Hopefully this helps others to get a nice work flow set up for their F# .NET Core 2.0 solutions.

This was done on a Windows 10 OS but the steps should be easily transferable to other platforms.

Firstly, my solution is called XunitTestDemo and the root directory that I am working from is also called XunitTestDemo.

So, create a new directory called XunitTestDemo and go to that directory in the command line.
Run the following commands.

dotnet new classlib -lang f# -o TestDemo.Core
dotnet new xunit -lang f# -o TestDemo.Tests
cd TestDemo.Tests
dotnet add reference ..\TestDemo.Core\TestDemo.Core.fsproj
code ..\

These set up a test project called TestDemo.Tests and a project for housing production code called TestDemo.Core. We also have now set up a reference from TestDemo.Tests to TestDemo.Core and opened VsCode in our XunitTestDemo directory.

In Vs Code, follow the steps below to create a new directory called .vscode

Next go into TestDemo.Core.fsproj. I found a slight issue with .NET Core 2.0 classlib projects in F# which means that the FsAutoComplete doesn’t work properly.This isn’t an issue with Ionide but, rather, other upstream services that it uses.
You can get more information about this here
To work around this, I had to change the TargetFramework in TestDemo.Core.fsproj to be netcoreapp2.0 as in the steps below.


With this done, lets add an F# module that we can later write a test for. Go into Library.fs and replace its contents with:

module TestDemo.Core.Calculator

let add x y = x + y

Now run the following in the command line from the XunitTestDemo\TestDemo.Core directory.

\TestDemo.Core>dotnet restore
dotnet build

And the same from the XunitTestDemo\TestDemo.Tests directory.

\TestDemo.Tests>dotnet restore
dotnet build

Back in VsCode reload the window


Now lets write a test that will fail. Go into Tests.fs in the TestDemo.Tests project and replace its contents with below:

module TestDemo.Tests.CalculatorTest
open System
open Xunit
open TestDemo.Core

[]
let ``test add`` () =
    Assert.Equal(5, Calculator.add 3 10)

Now we need to set up our VsCode test task. So, create a file called tasks.json in the .vscode folder that you created earlier.

Add the below content to it:

{
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "Tests",
            "command": "dotnet",
            "type": "shell",
            "args": [
                "test"
            ],
            "presentation": {
                "reveal": "silent"
            },
            "problemMatcher": "$msCompile",
            "group": {
                "kind": "test",
                "isDefault": true
            }
        }
    ]
}

This task calls out to .NET Core 2.0 CLI to run our tests. The above configuration also makes it the default testing task for our solution. (“isDefault”: true)

To make things easier, lets create a keyboard shortcut to run the default testing task. So go into Keyboard Shortcuts preferences:

Click into keybindings.json and add the following custom key binding.

.....
{   "key": "ctrl+shift+r",              
    "command": "workbench.action.tasks.test" 
}
...

Save everything and now ctrl+shift+r will run the default testing task which we have configured to be our testing task earlier which will run all tests in our solution.

In order for the .NET CORE 2.0 CLI to look for and run all our xunit tests including future ones that you may add, we need to create a Solution and add our two projects to it. This is done with the following commands, again from the XunitTestDemo directory on the command line.

XunitTestDemo>dotnet new sln
XunitTestDemo>dotnet sln XunitTestDemo.sln add TestDemo.Core\TestDemo.Core.fsproj
XunitTestDemo>dotnet sln XunitTestDemo.sln add TestDemo.Tests\TestDemo.Tests.fsproj

Back in VsCode, reload the window again as before from the command palette, ctrl+shift+p


Open up the integrated terminal that will show our test results.

Now go back into Tests.fs and hit ctrl+shift+r to run all our tests. This command can actually be run from anywhere within the VsCode instance that is open for our solution.

Our test will be discovered and run and we can see the results in the integrated terminal.

Lets fix our broken failing test so it is asserting the correct result – not the correct TDD approach but it serves as an example.

 Assert.Equal(13, Calculator.add 3 10)

Then without doing any building our reloading of the window or anything, we can hit ctrl+shift+r again and see our successful test run in the integrated terminal.

We are now set up for a nice work flow to run our tests with ease as we make changes.

The source code for this demo is available on my Github here

Leave a Reply

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