In this chapter we will look into how to write classes and unit tests in C# using x-unit with .Net Core.
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
BootcampFunctions project - create a class for each Bootcamp function.BootcampFunctions.Test project to test the Bootcamp classes.Use the
dotnet new --listcommand to see all the available templates. You can learn more about thedotnetproject template commands here - https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new-list
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 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:
totalPhoneBill functions equivalent class name TotalPhoneBill and create it in a file name called TotalPhoneBill.cs.BootcampFunctions.For example:
namespace BootcampFunctions {
class TotalPhoneBill {
public float total(string param) {
// return the result.
}
}
}
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}";
}
}
}
To run the tests you can use this command assuming you are in the UnitTestYourBootcampInCSharp folder:
dotnet test ./BootcampFunctions.Test/
Create a .gitignore file and add the entries below:
**/bin/*
**/obj/Debug/**
Create repository in GitHub named TestYourBootcampInCSharp. Push your code to it.
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.ymlfile.
After setting this up you should soon have a workflow with a green tick next to it under Actions.
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:
Re-run jobs,Create status badge,README.md file in your project root.See a starter project here.