Lesson 1.7: Setting Up Unit Test Project

Testing is an important part of any project or command-line app. Spectre.Console.Cli is designed very well to support unit testing of commands. Unit tests are isolated tests meant to test just the code and logic within a component, or a command in the case of CLI apps. Using interfaces for dependent components and dependency injection (which we set up in our last two lessons) makes our app easier to test. In this lesson, we are going to set up a test project, connect it to our Lesson 1.6 console application, and learn about mock objects.

Reminder: you can find the source for this lesson in our Azure DevOps repo: Cli.Lesson1.6.UnitTests – Repos (azure.com).

Setup xUnit Test Project

In this lesson, we need to setup a separate test project in our solution. And we will use the code from Lesson 1.6 as the basis for the tests that we build.

There are many unit testing tools for .NET. One of the most popular is xUnit, so we will use that for our test projects. It integrates well with Visual Studio and supports the .NET versions that we need. While the mechanics of the tests are using xUnit, the concepts behind the tests and the set of tests created are useful on any unit testing platform. MsTest and NUnit are also popular testing frameworks for .NET.

Let’s create our xUnit test project in Visual Studio:

  1. Ensure our solution is open in Visual Studio (tutorials.sln). The solution should contain separate projects for the 6 lessons that we have built so far.
  2. Right-click the solution root to get the context menu.
  3. Then select ‘Add > New Project’ to launch the new project dialog.
Fig 1 – New Project Dialog
  1. Search for xUnit in the new project dialog.
  2. Select the ‘xUnit Test Project (.NET Core)’ in the list and then press the ‘Next’ button.
Fig 2 – Configure New Project
  1. Name the project: ‘Cli.Lesson1.6.UnitTests’. We typically use this naming convention. The test project has ‘.UnitTests’ appended to the name of the project that it is testing. That way even with many projects in a solution, we know which tests are for which code projects.
  2. Then click the ‘Create’ button.
  3. After a few seconds, it will create that test project with a single test class in it.
  4. We won’t need that test class file, so let’s delete it from the project.

We now have our xUnit test project, which will hold all of our tests. If we look at the project’s ‘Dependencies’ node in the Solution Explorer and launch the NuGet manager, we will see that it comes with two package references: xunit and xunit.runner.visualstudio. These packages are what adds xUnit support to this project.

Re-target Project to .NET 5

The test project actually still targets .NET Core v3.1. As of writing this lesson, the default was not yet moved to .NET 5 for new projects, so we need to re-target this test project to .NET 5 (like we did with the other code projects in this chapter).

  1. In the solution explorer, right click on the Cli.Lesson1.6.UnitTests project to open the context menu.
  2. In the menu, click the ‘Properties’ item.
  3. That will load the ‘Project Properties’ window to the ‘Application’ tab.
Fig 3 – UnitTest Project Properties
  1. Then change the ‘Target framework’ selection to ‘.NET 5.0’.
  2. With that change in place, let’s save the file.

Add Project Reference

Before we can start writing tests for the code in Cli.Lesson1.6 project, we need to add a reference to it in our test project.

  1. In the solution explorer under the Cli.Lesson1.6.UnitTests project, right click on the ‘Dependencies’ node.
  2. In the context menu, click the ‘Add Project Reference’ item.
Fig 4 – Add Project Reference
  1. In the ‘Reference Manager’ dialog, check the ‘Cli.Lesson1.6’ project.
  2. Finally, click the ‘OK’ button to add this reference to our test project. Now we can reference types and build tests for classes in that project.

Preparing for Mocking Objects

Our test project configuration is complete, but before writing tests, we need to investigate mock objects and Mock frameworks.

Mock objects are simulated objects that mimic the behavior of real system objects in controlled ways, typically used to test software. A developer typically creates a mock object to test the behavior of some other object, so that they may return expected data or desired behavior for the test… for example if the method under test should return a particular number or flag… or if the mocked object’s method call should throw an exception to mimic error conditions.

It is possible to create mock classes by hand to match the real dependencies in your application. But luckily, there are Mock frameworks that can do some of this repetitive work for you. They make extensive use of interfaces and virtual methods to provide mock behavior without requiring us to provide full implementations of those interfaces or base classes. We will see exactly how that works in our command unit tests.

Like unit test frameworks, there are several Mock frameworks for .NET test projects. We will use the Moq framework. It’s the largest such tool for .NET projects.

Let’s install the Moq package into our project:

  1. In the solution explorer under the Cli.Lesson1.6.UnitTests project, right click on the ‘Dependencies’ node.
  2. In the context menu, click the ‘Manage NuGet Packages’ item.
Fig 5 – NuGet Manager for Moq
  1. In the ‘Browse’ tab, search for ‘Moq’.
  2. Then select the ‘Moq’ item in the list view.
  3. At the far right, click the ‘Install’ button to install this package into our test project.
  4. Accept any license or dependency windows that come up during the installation process.

After installing Moq, the ‘Installed’ tab in the NuGet Manager should have the following packages:

Fig 5 – NuGet Manager Installed Tab

Finally we have completed all of the pre-requisites for our test project. We can build our project, but nothing interesting happens, since there are no tests defined in the project. But at least we know we are in a good state. There were a lot of tools and configuration to get setup in this lesson, but now we can focus on just writing and understanding our first command test in the next lesson.

One thought on “Lesson 1.7: Setting Up Unit Test Project

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s