When getting start with a new language the first thing that I do to get familiar with the new to me syntax is solve FizzBuzz (https://en.wikipedia.org/wiki/Fizz_buzz) in that language. This small activity sends me through the gauntlet of setting up a new project, finding and setting up a unit testing framework, getting the test project to find and execute the code, and finally solving FizzBuzz using a new language. Creating a unit testing project for .NET Core on the Mac proved a little difficult so I thought I’d share my process in a blog post.

Most of these steps will be done from the terminal and I will be using Visual Studio Code view project files.

Open your terminal and navigate to where you would like to create your project

First we are going to create our FizzBuzz directory

mkdir FizzBuzz

Navigate into that directory and create our Dotnet Solution file

cd FizzBuzz/

dotnet new sln

Create the FizzBuzzService project that will handle solving FizzBuzz

mkdir FizzBuzz.Service

cd FizzBuzz.Service/

dotnet new classlib

Navigate back to the Solution file and add a reference to the FizzBuzzService project

cd ..

dotnet sln add FizzBuzz.Service/FizzBuzz.Service.csproj

For a unit testing library we are going to use NUnit. To make setting it up easier we are going to install an NUnit Dotnet Template (this template will remain on your computer so you won’t have to do this again if you create a new project)

dotnet new -i NUnit3.DotNetNew.Template

Create the NUnit test project

mkdir FizzBuzz.Test

cd FizzBuzz.Test/

dotnet new nunit

Add a reference to the FizzBuzz Service

dotnet add reference ../FizzBuzz.Service/FizzBuzz.Service.csproj

Navigate back to the Solution file and add a reference to the FizzBuzzTest project

cd ..

dotnet sln add FizzBuzz.Test/FizzBuzz.Test.csproj

You can now open the project in Visual Studio Code

code .

Back in the terminal navigate to the test folder to run the tests

cd FizzBuzz.Test

dotnet test

You should see one of one tests passing

In your UnitTest1.cs file write your first test

Currently your test can not find the FizzBuzzService class which is fine because it does not exist yet

In the class1.cs file in the FizzBuzz.Service folder rename the class to FizzBuzzService and create the Translate() method

All that is left is to add a reference to the FizzBuzzService in the UnitTest1.cs file with

using FizzBuzz.Service;

From the FizzBuzz.Test folder run dotnet test and you should now have one failing test

In your Translate method return “1” instead of “” and run

dotnet test

Your dotnet core project using Junit is now setup with its one test passing

Share This