NUnit Testing
Published: 2023-11-12 02:16:29

## Testing Framework

This week I added a testing framework to my text to html coverter project. I decided to go with [NUnit](https://nunit.org/). NUnit initially ported from JUnit and it is an open source unit-testing framework for all .Net languages. I decided to go with this framework because I use C# for my project and NUnit is integrated with Visual Studio and Visual Studio Code which makes it easily accessible for me.

## Setup

The setup was very easy. It was done by simply adding the NUnit Test Project to my solution, which can be seen in the screen shots below: 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7dxurdq0bi01q0j4a1dw.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dgn95kdtisiyoivhpzof.png)

After the framework was added, I began to familiarize myself with the steps required to create tests for my program. I learned about the Assert class to check the results of my test actions, the Setup() method to call functions before each test function is called and TearDown() method to call functions after each test function is called, which I used to clean up what was done in Setup(). I also learned that each test function can be divided into 3 parts to make things organized, which are:

- **Arrange:** This is where I prepared variables that I was going to pass to the function I was testing.
- **Act:** This is where I called the function I was testing.
- **Assert:** This is where I mostly used Assert.IsTrue() and Assert.DoesNotThrow() functions to decide if the function call passed or failed depending on its objective.

## Testing & Results

To begin I started with the most basic function in my program which handles directory creation based on a path parameter. I tested it with a valid directory string and an empty string. The valid directory string test passed and the empty string test failed. So, I had to go and add a try-catch block to that function so that if an empty string was passed as an argument it'd handle the error properly. Then, I decided to add test cases for the core of my program which was in charge of converting a text or a markdown file to html. I wrote around 15 test cases for it with different scenarios like: checking if an html file was created after converting from text/markdown, checking the content of the converted html file to make sure paragraphs, language, horizontal rule, headings, links, and style-sheet were correctly included based on the input files. All the tests for the core of the program passed except for 1, which was converting a "#" markdown identifier to h1 html tag. The issue was that after converting the Heading1 from markdown to h1 in html, the text for the h1 was being repeated in the body. So, I made changes to my code-base to address this issue.

## What I learned

This was my first time using a testing framework, and I learned about the value that testing provides, specially when there is a big project with contributors, and a code base that is changing all the time. I learned that when creating a test function I can divide up the logic into: Arrange, Act, and Assert sections to make it easier. Now that I understand how helpful testing can be, I will continue to use it for my projects.