DotNetIntroduction

Unit testing in C# introduction

In this chapter we will look into how to write classes and unit tests in C# using x-unit with .Net Core.

Setup

Create a folder called UnitTestYourBootcampInCSharp in your projects folder.

mkdir UnitTestYourBootcampInCSharp

Change into the folder and setup two dotnet core projects BootcampFunctions and BootcampFunctions.Test. Use the dotnet new command to setup the projects.

cd UnitTestYourBootcampInCSharp

Create a classlib project:

dotnet new classlib -o BootcampFunctions

And one xunit dotnet core folder:

dotnet new classlib -o BootcampFunctions.Test

Use the dotnet new --list command to see all the available templates. You can learn more about the dotnet project template commands here - https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new-list

Add a reference

Add a reference to the BootcampFunctions from the BootcampFunctions.Test project by adding and entry in the BootcampFunctions.Test.csproj file. This will allow you to reference classes in the BootcampFunctions project in your tests.

Copy the ProjectReference tag below and put it below the last PackageReference tag in the ItemGroup tag.

     <ProjectReference Include="../BootcampFunctions/BootcampFunctions.csproj" />

Create the classes

Create C# classes for each of the logic of these codeX Bootcamp functions at http://bootcamp.projectcodex.co

The aim is for you to convert you JavaScript knowledge into C#. Converted each of the functions below into a class. Each class should have one method. You can use constructors or static methods where you see fit.

Create classes for these functions:

Note:

For example:

namespace BootcampFunctions {
  class TotalPhoneBill {
      public float total(string param) {
        // return the result.
      }
  }
}

Create some Tests

Create tests that at looks like this in the BootcampFunctions.Test project.

Note the using BootcampFunctions statement to reference the Bootcamp functions you created.

using System;
using Xunit;
using BootcampFunctions;

namespace BootcampFunctions.Test
{
    public class GreetTest
    {
        [Fact]
        public void ShouldBeAbleToGreetJo()
        {            
            Assert.Equal("Hello, Jo", Greet.Execute("Jo"));
        }

        [Fact]
        public void ShouldBeAbleToGreetLihle()
        {
            Assert.Equal("Hello, Lihle", Greet.Execute("Lihle"));
        }
    }
}

The class that is being tested here lloks like this:

using System;

namespace BootcampFunctions
{
    public class Greet
    {
        public static string Execute(String name) {
            return $"Hello, {name}";
        }
    }
}

Run the tests

To run the tests you can use this command assuming you are in the UnitTestYourBootcampInCSharp folder:

dotnet test ./BootcampFunctions.Test/

Git setup

Create a .gitignore file and add the entries below:

**/bin/*
**/obj/Debug/**

Create repository in GitHub named TestYourBootcampInCSharp. Push your code to it.

CI using GitHub Actions

Once you committed your code to GitHub setup CI by using GitHub Actions - click on Actions in your GitHub repository.

Select : .NET By GitHub Actions Build and test a .NET or ASP.NET Core project. - it should be the first option top left.

Click on: Setup this workflow

We will tweak the default config a bit.

Remove the Build & Restore dependencies entries.

And change the Test entry to look like this

- name: Test
      run: dotnet test ./BootcampFunctions.Test/

Once done your setup should look this:

name: .NET

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0.x
    - name: Test
      run: dotnet test ./BootcampFunctions.Test/

The above config will be saved in the ./gitub/workflows/dotnet.yml file.

After setting this up you should soon have a workflow with a green tick next to it under Actions.

Add a GitHub Actions badge

A GitHub Actions Badge will show the status of your Unit Tests - green if they are all passing and green if they are failing.

To create a GitHub Actions Badge:

Starter project

See a starter project here.