Scott Hanselman

Command Line: Using dotnet watch test for continuous testing with .NET Core 1.0 and XUnit.net

March 27, 2017 Comment on this post [13] Posted in DotNetCore | Open Source
Sponsored By

I've installed .NET Core 1.0 on my machine. Let's see if I can get a class library and tests running and compiling automatically using only the command line. (Yes, some of you are freaked out by my (and other folks') appreciation of a nice, terse command line. Don't worry. You can do all this with a mouse if you want. I'm just enjoying the CLI.

NOTE: This is considerably updated from the project.json version in 2016.

First, I installed from http://dot.net/core. This should all work on Windows, Mac, or Linux.

C:\> md testexample & cd testexample

C:\testexample> dotnet new sln
Content generation time: 33.0582 ms
The template "Solution File" created successfully.

C:\testexample> dotnet new classlib -n mylibrary -o mylibrary
Content generation time: 40.5442 ms
The template "Class library" created successfully.

C:\testexample> dotnet new xunit -n mytests -o mytests
Content generation time: 87.5115 ms
The template "xUnit Test Project" created successfully.

C:\testexample> dotnet sln add mylibrary\mylibrary.csproj
Project `mylibrary\mylibrary.csproj` added to the solution.

C:\testexample> dotnet sln add mytests\mytests.csproj
Project `mytests\mytests.csproj` added to the solution.

C:\testexample> cd mytests

C:\testexample\mytests> dotnet add reference ..\mylibrary\mylibrary.csproj
Reference `..\mylibrary\mylibrary.csproj` added to the project.

C:\testexample\mytests> cd ..

C:\testexample> dotnet restore
Restoring packages for C:\Users\scott\Desktop\testexample\mytests\mytests.csproj...
Restoring packages for C:\Users\scott\Desktop\testexample\mylibrary\mylibrary.csproj...
Restore completed in 586.73 ms for C:\Users\scott\Desktop\testexample\mylibrary\mylibrary.csproj.
Installing System.Diagnostics.TextWriterTraceListener 4.0.0.
...SNIP...
Installing Microsoft.NET.Test.Sdk 15.0.0.
Installing xunit.runner.visualstudio 2.2.0.
Installing xunit 2.2.0.
Generating MSBuild file C:\Users\scott\Desktop\testexample\mytests\obj\mytests.csproj.nuget.g.props.
Generating MSBuild file C:\Users\scott\Desktop\testexample\mytests\obj\mytests.csproj.nuget.g.targets.
Writing lock file to disk. Path: C:\Users\scott\Desktop\testexample\mytests\obj\project.assets.json
Installed:
16 package(s) to C:\Users\scott\Desktop\testexample\mytests\mytests.csproj

C:\testexample> cd mytests & dotnet test

Build started, please wait...
Build completed.

Test run for C:\testexample\mytests\bin\Debug\netcoreapp1.1\mytests.dll(.NETCoreApp,Version=v1.1)
Microsoft (R) Test Execution Command Line Tool Version 15.0.0.0
Copyright (c) Microsoft Corporation. All rights reserved.

Starting test execution, please wait...
[xUnit.net 00:00:00.5539676] Discovering: mytests
[xUnit.net 00:00:00.6867799] Discovered: mytests
[xUnit.net 00:00:00.7341661] Starting: mytests
[xUnit.net 00:00:00.8691063] Finished: mytests

Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 1.8329 Seconds

Of course, I'm testing nothing yet but pretend there's a test in the tests.cs and something it's testing (that's why I added a reference) in the library.cs, OK?

Now I want to have my project build and tests run automatically as I make changes to the code. I can't "dotnet add tool" yet so I'll add this line to my test's project file:

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

Like this:

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

Then I just dotnet restore to bring in the tool.

In order to start the tests, I don't write dotnet test, I run "dotnet watch test." The main command is watch, and then WATCH calls TEST. You can also dotnet watch run, etc.

NOTE: There's a color bug using only cmd.exe so on "DOS" you'll see some ANSI chars. That should be fixed in a minor release soon - the PR is in and waiting. On bash or PowerShell things look fin.

In this screenshot, you can see as I make changes to my test and hit save, the DotNetWatcher Tool sees the change and restarts my app, recompiles, and re-runs the tests.

Test Run Successful

All this was done from the command line. I made a solution file, made a library project and a test project, made the test project reference the library, then built and ran the tests. If I could add the tool from the command line I wouldn't have had to manually touch the project file at all.

Again, to be sure, all this is stuff you can (and do) do in Visual Studio manually all the time. But I'll race you anytime. ;)


Sponsor: Check out JetBrains Rider: a new cross-platform .NET IDE. Edit, refactor, test, build and debug ASP.NET, .NET Framework, .NET Core, or Unity applications. Learn more and get access to early builds!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service
March 27, 2017 9:33
Hi Scott,

Thanks for this article.
What about code coverage for .Net core projects ?

March 27, 2017 9:40
Cool, so this is a kind of poor man's live unit testing, no need for VS Enterprise then!
March 27, 2017 9:56
@matt you can also consider NCrunch (not pricey and beats Live Unit Testing easily)
March 27, 2017 10:12
Hi, to see the result after changes in code one also needs to execute "dotnet watch run".
March 27, 2017 11:11
@Miha
+1 for NCrunch.
There's so many good things going on with it.
The NCrunch Grid node is also very useful for using another machine's CPU or software.
March 27, 2017 12:01
I've installed .NET Core 1.0 on my machine. Let's see if I can get a class library and tests running and compiling automatically using only the command line. (Yes, some of you are freaked out by my (and other folks') appreciation of a nice, terse command line. Don't worry. You can do all this with a mouse if you want. I'm just enjoying the CLI.


typo found, missing ) - from years of programming, i would of suspected you would of been on the ball when it comes to opening and closing brackets. :)
March 27, 2017 14:58
Hi Scott,

thanks for posting all these testing related blog posts. I'd really love to see a blog post about CI+CD for ASP.NET projects in terms of a self-hosted solution (I do have some projects that are either closed source or where I'm not allowed to use services like AppVeyor). I tried to use Jenkins and tried to use TFS for this but I just gave up after a couple of hours because I had multiple problem and they just didn't work out of the box. Since containers are becoming more popular, I'd love to see a Docker solution for CI+CD that I can simply run in Azure. Unfortunately, I don't have the time to do this myself. I'd love to hear your thoughts about this.

With regards,
Matthias
March 27, 2017 16:32
Hi Scott,

Thank you for your articles on .NET Core.
I followed the .NET Core course on MVA. Really appreciated.

Keep up the good work, for the brave new Microsoft!
March 27, 2017 22:02
I'm using a "dotnet new angular" instead of the class library but I can't add it as a reference because the xunit project is netcoreapp1.0 instead of netcoreapp1.1

If I change the xunit project to target netcoreapp1.1 - none of my tests are discovered.

Any ideas braintrust?
March 28, 2017 0:20
Thanks for the MVA tutorials. I followed them and got up and running with the basics on .NET Core. It's really awesome to see the command line support. I come from a Linux background where cli was very much integrated into my development workflow. I love to see the same happening here. Feels home.


C:\Code\CoreMVC>dotnet watch run



Love it!
March 28, 2017 4:45
Hello Scott,

I don't suppose you have any insight into using code coverage tools, like Sonarqube, with .NET Core? My understanding is code coverage can't run because there aren't debug/symbol files for .NET core apps?
March 28, 2017 15:52
Scott, good thing you installed the xunit packages before installing the watch tool. The other way around does not work. :/

https://github.com/NuGet/Home/issues/4771

:punch-in-sad-panda-face:
March 28, 2017 18:38
Minor typo:

On bash or PowerShell things look fin.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.