@manhnguyenv

Welcome to my blog!

NUnit in VS2017

July 13, 2017 10:45

Testing .NET Core with NUnit in Visual Studio 2017

The switch from the project.json format to the new csproj based format for .NET Core and .NET Standard projects also changed the API for test adapters. Because of this, NUnit’s dotnet-test-nunit adapterstopped working and we had to update our existing Visual Studio test adapter to support .NET Core. Last night I released the first alpha release of the test adapter to NuGet enabling testing of .NET Core and .NET Standard projects using Visual Studio 2017, Visual Studio Code, TFS Build, and, the dotnet test CLI command.

Creating a Test Project

The following instructions are for Visual Studio, but the principles apply to all development environments.

In the solution that you want to test, Add | New Project… and add a Visual C# | .NET Core | Class Library (.NET Core).

Add .NET Core Class Library

I am testing a .NET Standard library, but I am using a .NET Core class library for testing. Testing requires a platform to test on, so your test assembly must target a specific platform. I selected .NET Core, but I could also target .NET 4.5 or even multi-target the test assembly. Be aware that multi-targeted test projects are barely supported by Visual Studio and your results may vary. Hopefully we will see improvements in the tooling soon.

Adding NuGet References

First, you will want to add a project reference to the project you are testing, then add NuGet references to the test framework NUnit 3.6.1, to the test runner NUnit3TestAdapter 3.8.0-alpha1 and to the test SDK Microsoft.NET.Test.Sdk 15.0.0. Add the references either using the Manage NuGet Packages… user interface, or by editing the project file.

Add NuGet References to Test Project

Your csproj file should end up looking similar to this. Notice how much cleaner the new format is.

<project Sdk="Microsoft.NET.Sdk">

  <propertygroup>
    <targetframework>netcoreapp1.1</targetframework>
  </propertygroup>

  <itemgroup>
    <packagereference Include="Microsoft.NET.Test.Sdk" Version="15.0.0"></packagereference>
    <packagereference Include="NUnit" Version="3.6.1"></packagereference>
    <packagereference Include="NUnit3TestAdapter" Version="3.8.0-alpha1"></packagereference>
  </itemgroup>

  <itemgroup>
    <projectreference Include="..\NetStandardLibrary\NetStandardLibrary.csproj"></projectreference>
  </itemgroup>

  <itemgroup>
    <service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}"></service>
  </itemgroup>

</project>

From here, write your unit tests as you would for any project.

Running Tests in Visual Studio

Running tests in Visual Studio hasn’t changed. See my previous post on running NUnit tests in Visual Studio if you have questions.

Running Unit Tests in Visual Studio

A few things to note,

  • If you multi-target your tests, only one target will run in Visual Studio, I believe the first
  • Code Coverage is not supported for .NET Core yet, that will be available in a future update to Visual Studio
  • Live Unit Testing is also not available for .NET Core yet

Running Tests from the Command Line

Adding the NUnit 3 Test Adapter to your project will also allow you to use the dotnet test CLI. Here is an example run with the unit tests targeting both .NET 4.5 and .NET Core 1.1.

C:\src\Spikes\NetStandardLibrary
λ dotnet test .\NetStandardLibrary.Tests\NetStandardLibrary.Tests.csproj
Build started, please wait...
Build completed.

Test run for C:\src\Spikes\NetStandardLibrary\NetStandardLibrary.Tests\bin\Debug\net45\NetStandardLibrary.Tests.dll(.NETFramework,Version=v4.5)
Microsoft (R) Test Execution Command Line Tool Version 15.0.0.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
NUnit Adapter 3.8.0.0: Test execution started
Running all tests in C:\src\Spikes\NetStandardLibrary\NetStandardLibrary.Tests\bin\Debug\net45\NetStandardLibrary.Tests.dll
NUnit3TestExecutor converted 18 of 18 NUnit test cases
NUnit Adapter 3.8.0.0: Test execution complete

Total tests: 18. Passed: 18. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 0.9096 Seconds


Test run for C:\src\Spikes\NetStandardLibrary\NetStandardLibrary.Tests\bin\Debug\netcoreapp1.1\NetStandardLibrary.Tests.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...
NUnit Adapter 3.8.0.0: Test execution started
Running all tests in C:\src\Spikes\NetStandardLibrary\NetStandardLibrary.Tests\bin\Debug\netcoreapp1.1\NetStandardLibrary.Tests.dll
NUnit3TestExecutor converted 18 of 18 NUnit test cases
NUnit Adapter 3.8.0.0: Test execution complete

Total tests: 18. Passed: 18. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 1.1661 Seconds

The tests run for both targets and that you get the output from the dotnet test command, not the normal coloured NUnit output that you would get from the NUnit Console. Hopefully we will be releasing an updated console runner in the near future.

Refer: http://www.alteridem.net/2017/05/04/test-net-core-nunit-vs2017/

Introduction to Unit Tests with NUnit

You have a new project and you want to add unit tests, where do you start?  This tutorial walks you through adding an NUnit test project, writing your first tests and running them in Visual Studio.

This walk-through assumes you already have a project that you want to test. I am starting here with a project that contains a simple class that contains an extension method that converts an enum value to a friendly string by splitting the enum name at capitals. For example, System.Windows.Forms.Keys.BrowserBack would become “Browser Back”.

The source code for this tutorial is on GitHub.

Add a Unit Test Project

It is best practice to keep your unit tests separate from your production code, so we will add a new project that contains our tests. It is also common to name your test project the same as the project it is testing and appending .Tests. My main project is IntroToNUnit, so I will add IntroToNUnit.Tests.

  1. Right click on your solution in Visual Studio
  2. Click on Add | New project…
  3. Click on Visual C# | Windows and click on Class Library.
  4. Enter the name of your test project and click OK.

Add new unit test project

Add a Reference to NUnit

Before you can write unit tests, you need to add a reference to a test framework, in this case NUnit.

  1. Right click on your unit test project
  2. Click on Manage NuGet packages…
  3. Click on Browse
  4. Select NUnit and click the Install button. At the time of this writing, NUnit is number three in the list. If that changes, use the search bar to find it.

Add reference to NUnit

Add a Reference to Your Project

In order to test the code in your main project, you need to add a reference to it in your test project.

  1. Right click on your test project
  2. Click on Add | Reference…
  3. Select Projects | Solution
  4. Add a checkmark on your main project.
  5. Click OK.

Add Project Reference

Write Unit Tests

Another common convention is to name the test class the same as the class being tested with Tests appended. The class I want to test is called EnumToStringConverter.cs, so I rename Class1.csto EnumToStringConverterTests.cs.

To this class, I add the [TestFixture] attribute. You also need to add the using NUnit.Framework. My first test is going to make sure that the converter splits multiple words correctly, so I add a test enum and the first unit test. Add this point, my code looks like this.

using NUnit.Framework;

namespace IntroToNUnit.Tests
{
    public enum TestTypes
    {
        None,
        UnitTesting,
        IntegrationTesting,
        FlyByTheSeatOfYourPantsTesting
    }

    [TestFixture]
    public class EnumToStringConverterTests
    {
        [Test]
        public void CanConvertEnumIntoMultipleWords()
        {
        }
    }
}

Notice that my test method is public and returns void and that it has a [Test] attribute to identify it as a test. You can have more complicated test methods which I will cover in future posts.

Arrange, Act, Assert

Now it is time to write the first unit test. The most common structure is known as Arrange, Act, Assert. What this means is that you group the code in your test method into setting up for the test (Arrange), then you perform the action that you want to test (Act), then you check the results (Assert). Sometimes you will find that the arrange and act steps are combined for simpler tests as in this case. That is fine.

[Test]
public void CanConvertEnumIntoMultipleWords()
{
    // Arrange/Act
    var actual = TestTypes.UnitTesting.ToFriendlyString();

    // Assert
    Assert.That(actual, Is.EqualTo("Unit Testing"));
}

In this test, I have executed the code that I want to test, then I asserted what I expected the value to be. The Assert class is provided by NUnit and allows check many conditions in your tests. In this case I am only checking that the string returned is equal to what I expected. I will cover more types of NUnit Asserts in a later post.

NUnit has two assert syntaxes. In the example code, I used the Constraint Model or sometimes the Fluent Syntax. It is called the fluent syntax because it reads very much like English and because you can continue to append extra conditions. For example, I could have also checked for null with,

Assert.That(actual, Is.Not.Null.And.EqualTo("Unit Testing"));

NUnit also has a classic model/syntax that you are likely to see in other tutorials. The above Assert could also be written as,

Assert.NotNull(actual);
Assert.AreEqual("Unit Testing", actual);

Some people prefer the classic syntax, but I recommend that you use the fluent syntax. New features in NUnit are always added to the fluent syntax and only rarely get added to the classic syntax.

Run the Unit Tests in Visual Studio

To run your unit tests in Visual Studio, you first need to install the NUnit 3 Test Adapter. To do so,

  1. In the main menu, click Tools | Extensions and Updates…
  2. Click on Online | Visual Studio Gallery,
  3. Search for NUnit and install NUnit 3 Test Adapter
  4. After you install, click the button at the bottom to Restart Visual Studio

Add NUnit Visual Studio Adapter

Back in Visual Studio with your solution open, you need to open the Test Explorer Window. To do this, in the main menu, click Test | Windows | Test Explorer.

The window that opens will list your unit tests and allow you to run or debug them. Click Run All to run your tests. Successful tests will go green and failed tests will be red. You can see the results of any test by clicking on it and viewing the results at the bottom of the window. In a future post, I will cover other ways to run your tests and how to use the Test Adapter in Visual Studio in more detail.

Test Explorer

Congratulations, you have your first unit test.

Refer: http://www.alteridem.net/2016/10/03/nunit-unit-tests/

Run NUnit tests in dotnet core

 

Update 3: With NUnit3TestAdapter v3.8.0-alpha1 it is possible now to run the tests using dotnet test command. You just need to have these dependencies in your test project:

<PackageReference Include="nunit" Version="3.7.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0-*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.*" />

You can try it out!

Update 2: Visual Studio 2017 and the move from project.json to csproj made the dotnet-test-nunit test adapter obsolete, so we needed to release another updated adapter to run .NET Core tests. Please see Testing .NET Core with NUnit in Visual Studio 2017 if you are using VS2017 and the new .NET Core tooling. See the update below if you are using project.json.

Update: NUnit now has support for dotnet test, so you no longer have to use NUnitLite. See testing .NET Core RC2 and ASP.NET Core RC2 using NUnit 3.

Categories