DEV Community

Yuan-Hsi Lee
Yuan-Hsi Lee

Posted on • Updated on

Unit test in C# without IDE

This lab, we’re working on unit test and CI.

About unit test

I’ve heard about unit test and did some in internship. However, I never got a chance to really understand what I’m supposed to write in unit tests. This is a good chance for me to study about unit test.

My link checker project uses C#, therefore, I searched the unit test in dotnet environment. There are several tools such as XUnit, NUnit. However, most of the documentations and tutorial are using Visual Studio and rely on its functionalities. In my case, my OS is Linux Mint which can’t install this IDE. After trying all the unit test tools that I can find, NUnit works perfect for me. Here are the steps for using NUnit with IDE:

1. Create a test directory

Move the original code files and its project file (.csproj)that run the application to an independent directory (not in the root), and create a new directory for test files.

For example, I moved the application's project file and relevant .cs to GoodLinkOrBadLink directory, and created a GoodLinkOrBadLink.Tests directory for placing my unit tests.
Alt Text

2. Create unit test project in test directory

cd to your test directory and use this command to create a .csproj file and a .cs file. The default file name of .cs is UnitTest, I changed it to Tests.cs.

dotnet new nunit
Enter fullscreen mode Exit fullscreen mode

3. Add reference to the test project file

In the test project file, add the application file path as reference. Below is an example in my test project file. The file path in the Include points to my application's project file.

    <ProjectReference Include="..\GoodLinkOrBadLink\OSD600.GoodLinkOrBadLink.csproj" />
Enter fullscreen mode Exit fullscreen mode

4. Modify UnitTest1.cs (or whatever test file name you have)

In step 2, a .cs file is generated with the command dotnet new nunit. By default, there is a line of C# directive using NUnit.Framework; for us to use the NUnit testing framework. I modified the class name and include my application's namespace as a C# reference.

By adding [Test] code block, we can add the unit test that we want to test our functions. For example,

public void Version_ValidOptions_ReturnTrue()
    string isVersionOpt_1 = "--v";
    string isVersionOpt_2 = "--version";
    string isVersionOpt_3 = "/v";

    var underTestT_1 = CLIUsage.Version(isVersionOpt_1);
    var underTestT_2 = CLIUsage.Version(isVersionOpt_2);
    var underTestT_3 = CLIUsage.Version(isVersionOpt_3);


Enter fullscreen mode Exit fullscreen mode

I'm using arrange, act, assert pattern to write my unit tests. I'm very new to write unit test, therefore, I will not explain how to write a unit test; I followed this documentation to write my unit tests, it is beginner-friendly.

5. Try to run the test!

Open terminal, move to the test directory and run this command to run the test project:

dotnet test
Enter fullscreen mode Exit fullscreen mode

If your test units are all passed, you'll see something like this,
Alt Text
However, if there is any test that didn't pass, you'll see something like the below picture with the number of failed tests with the error message and stack trace.
Alt Text

Just like how we debug in a normal bug; use the error message and stack trace to find the issue.

Create a GitHub Action as CI

After having unit tests in your repo, we can include the tests to ensure the new PR pass all the test before we merge it.

GitHub has its own CI service named Action. I followed its documentation which is very straightforward. You might need to make some changes after what I did in my dotnet.yml file.

Writing unit test is critical, however, this is actually the first time that I start a unit test from scratch. I'm now more confident to write testing and will study some good practice to improve my skill.

Thank you for reading my blog!

Top comments (0)