Continuously Running Tests in DotNet Core

Continuous test runners are a life-saver if you practice TDD. By watching files for changes, the runner is able to exexecute all of your tests for you as you edit files. This allows you to stay in your development flow and know if a change you made broke some tests.

Continuous test runners exist for a variety of languages, DotNet Core being no exception. In order to start using the continuous test runner you need to execute the following command in an emtpy directory:

dotnet new xunit

This will create a new XUnit DotNet Core project. If you list the directory you’ll see something similar to the following:

Currently our project has no test, let’s add one now in order to see the continuous test runner in action. Open up UnitTest1.cs file and add the following test:

public void assert_true_is_true()
  Assert.Equal(true, true);

To run our new test we execute the following command from the project root directory:

dotnet test

This produces an about similar to:

Great, we’ve got test execution. However, we have to execute the tests manually. In order to remedy that we first install a package entitled Microsoft.DotNet.Watcher.Tools with the following command:

dotnet add package Microsoft.DotNet.Watcher.Tools && dotnet restore

Unfortunatley, we need to change our .csproj file before this will work. Open that file and find the following line:

<PackageReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />

We need to change the PackageReference text to read DotNetCliToolReference. See the docs for more information about DotNetCliToolReference. After editing, the line should read as follows:

<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />

We should now be able to run the continuous test runner:

dotnet watch test

VoilĂ , we now have a DotNet Core continuous test runner! Using a continuous test runner can shorten your feedback loop by automating the execution of your tests as you change your code.

304 Words

2017-09-17 06:05 +0000