Shouldly
Search…
Getting Started
Here are some guides to setup a Shouldly project.

Setup a project with Dotnet CLI

In this guide, we will set up a project with Shouldly unit tests.
We won't use Visual Studio, we will build our project bare bones with Dotnet CLI and a text editor.

Prerequisites

  • You need to install a text editor (Notepad++, VSCode, etc...).

Creating the main program

The project structure will be as follows:
1
/project-name
2
project-name.sln
3
/program
4
...
5
/test
Copied!
  1. 1.
    Create your root directory, name it your project name.
  2. 2.
    Open a terminal in your root directory.
  3. 3.
    Run dotnet new sln.
  4. 4.
    Go into program folder and run dotnet new classlib.
  5. 5.
    Rename Class1.cs to Program.cs.
  6. 6.
    Add the following code to Program.cs:
1
using System;
2
3
public static class Program
4
{
5
public static string TestWorks()
6
{
7
return "Works";
8
}
9
}
Copied!
Go to the root folder and run dotnet sln add program/program.csproj, in order to add program to your solution.

Creating unit tests

In this part we will create the unit tests for the main program.
  1. 1.
    Move to test folder and run dotnet new nunit (you can use others unit tests frameworks but in this guide we will stick to nunit).
  2. 2.
    Add a reference to the main program by running dotnet add reference ../program/program.csproj
  3. 3.
    In the root folder add the tests to the solution by running dotnet sln add test/test.csproj.

Adding Shouldly

Now comes the important part, when we actually add Shouldly.
  1. 1.
    Go to the test folder and run dotnet add package Shouldly, to add Shouldly as a NuGet Package.
  2. 2.
    Add the following code to UnitTest1.cs:
1
using NUnit.Framework;
2
using Shouldly;
3
4
public class Tests
5
{
6
[Test]
7
public void Test1()
8
{
9
Program.TestWorks().ShouldBe("Works");
10
}
11
}
Copied!

Testing

To test your project, in your root folder run dotnet test.
And there you go, you have a basic project with unit tests using Shouldly.

Summary

We created a classlib project using Dotnet CLI Tools, then added unit tests and added Shouldly as a NuGet package to be able to use Shouldly in our tests.
Last modified 1yr ago