When teaching .NET Core I do a lot of "dotnet new" Hello World demos to folks who've never seen it before. That has it's place, but I also wanted to show how easy it is to get setup with Unit Testing on .NET Core.

For this blog post I'm going to use the command line so you know there's nothing hidden, but you can also use Visual Studio or Visual Studio Code, of course. I'll start the command prompt then briefly move to Code.

Starting from an empty folder, I'll make a SomeApp folder and a SomeTests folder.

C:\example\someapp> dotnet new

C:\example\someapp> md ..\sometests && cd ..\sometests

C:\example\sometests> dotnet new -t xunittest

At this point I've got a HelloWorld app and a basic test but the two aren't related - They aren't attached and nothing real is being tested.

Tests are run with dotnet test, not dotnet run. Tests are libraries and don't have an entry point, so dotnet run isn't what you want.

c:\example>dotnet test SomeTests

Project SomeTests (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.

xUnit.net .NET CLI test runner (64-bit win10-x64)

Discovering: SomeTests

Discovered: SomeTests

Starting: SomeTests

Finished: SomeTests

=== TEST EXECUTION SUMMARY ===

SomeTests Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 0.197s

SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.

I'll open my test project's project.json and add a reference to my other project.

I'll make a little thing to test in my App.

And add some tests.

Because the Test app references the other app/library, I can just make changes and run "dotnet test" from the command line. It will build both dependencies and run the tests all at once.

Here's the full output inclding both build and test.

c:\example> dotnet test SomeTests

Project SomeApp (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified

Compiling SomeApp for .NETCoreApp,Version=v1.0



Compilation succeeded.

0 Warning(s)

0 Error(s)



Time elapsed 00:00:00.9814887

Project SomeTests (.NETCoreApp,Version=v1.0) will be compiled because dependencies changed

Compiling SomeTests for .NETCoreApp,Version=v1.0



Compilation succeeded.

0 Warning(s)

0 Error(s)



Time elapsed 00:00:01.0266293





xUnit.net .NET CLI test runner (64-bit win10-x64)

Discovering: SomeTests

Discovered: SomeTests

Starting: SomeTests

Tests.Tests.TwoAndThreeIsFive [FAIL]

Assert.Equal() Failure

Expected: 4

Actual: 5

Stack Trace:

c:\Users\scott\Desktop\testtest\SomeTests\Tests.cs(20,0): at Tests.Tests.TwoAndThreeIsFive()

Finished: SomeTests

=== TEST EXECUTION SUMMARY ===

SomeTests Total: 2, Errors: 0, Failed: 1, Skipped: 0, Time: 0.177s

SUMMARY: Total: 1 targets, Passed: 0, Failed: 1.

Oops, I made a mistake. I'll fix that test and run "dotnet test" again.

c:\example> dotnet test SomeTests

xUnit.net .NET CLI test runner (64-bit .NET Core win10-x64)

Discovering: SomeTests

Discovered: SomeTests

Starting: SomeTests

Finished: SomeTests

=== TEST EXECUTION SUMMARY ===

SomeTests Total: 2, Errors: 0, Failed: 0, Skipped: 0, Time: 0.145s

SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.

I can keep changing code and running "dotnet test" but that's tedious. I'll add dotnet watch as a tool in my Test project's project.json.

Then I'll go back and rather than typing "dotnet test" I'll type "dotnet watch test."

c:\example> dotnet watch test

[DotNetWatcher] info: Running dotnet with the following arguments: test

[DotNetWatcher] info: dotnet process id: 14064

Project SomeApp (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.

Project SomeTests (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified

Compiling SomeTests for .NETCoreApp,Version=v1.0

Compilation succeeded.

0 Warning(s)

0 Error(s)

Time elapsed 00:00:01.1479348



xUnit.net .NET CLI test runner (64-bit .NET Core win10-x64)

Discovering: SomeTests

Discovered: SomeTests

Starting: SomeTests

Finished: SomeTests

=== TEST EXECUTION SUMMARY ===

SomeTests Total: 2, Errors: 0, Failed: 0, Skipped: 0, Time: 0.146s

SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.

[DotNetWatcher] info: dotnet exit code: 0

[DotNetWatcher] info: Waiting for a file to change before restarting dotnet...

Now if I make a change to either the Tests or the projects under test it will automatically recompile and run the tests!

[DotNetWatcher] info: File changed: c:\example\SomeApp\Program.cs

[DotNetWatcher] info: Running dotnet with the following arguments: test

[DotNetWatcher] info: dotnet process id: 5492

Project SomeApp (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified

Compiling SomeApp for .NETCoreApp,Version=v1.0

I'm able to do all of this with any text editor and a command prompt.

How do YOU test?

Sponsor: Do you deploy the same application multiple times for each of your end customers? The team at Octopus have taken the pain out of multi-tenant deployments. Check out their latest 3.4 release!