My first time doing unit testing by NUnit

Around 2 days ago, I have just tried to use NUnit. NUnit is software used to do unit testing during software development in C# ASP.NET environment. At first, it took quite a while (around 1-2 hours) just to figure out, install and learn how to use this software.

This is my first time using unit testing tool because in my previous job, a lack of documentation on system workflow always prevented me to create well defined test cases and its expected results. Here, the new developer has done excellent job on documenting the workflow and made my job is easier (I am using version 2.6.1 on Visual Studio 2010 on Windows 8, btw).

And then come first obstacle after installation. I encountered error alert box popped up when I try to load my DLL project file into newly created NUnit project. The error said: “This assembly was not build by any known testing framework“. After minutes of browsing on stackoverflow and couples of forum, it turned out that I need to go to the Tools -> Settings -> IDE Support -> Visual Studio and check the option: “Enable Visual Studio support” as indicated in figure below:

NUnit - Enable Visual Studio support

After that, you can load the DLL file to NUnit project. However, at this moment, I confused because it turned out the graphical appearance for my project is quite different from the sample project in the bundled NUnit software. The sample project has a look of apparently some classes together with its methods and a set of test cases to be executed upon clicking ‘Run’ button at the main interface:

NUnit - Sample Project

In contrast, my DLL project did not have tick and cross icons and no public methods which were defined inside those DLL project were visible from this NUnit GUI screen. It turned out that we need to incorporate NUnit library (nunit.framework.dll) located inside bin folder of NUnit program into our Visual Studio project. You can right click and choose ‘Add reference…’ in Solution Explorer window and browse that DLL file inside NUnit installation path.

After that you can test your methods which are contained inside code behind file. First, just add NUnit.Framework namespace and then use [TestCase] statement above the methods which you want to test as shown below:

NUnit inside C# code

After that, I compiled my class library project and then I reopened the NUnit project and viola, the test is working now:

NUnit testing

Advertisements

About edwin2026

I am currently Indonesian expatriate working in Singapore, graduated from Nanyang Technological University with major in Computer Science in 2009. I am also working as software engineer, developing and maintaining systems for Singapore civil service. I write this blog to share to readers about my life principle on various aspects, like religion, politics, business, relationship, and technology. I am interested in alternative worldviews because I found that many things taught to us by establishments are not true and harmful. My dream is to become self-sufficient in food and energy. Hopefully, someday I can have my own fruit garden and my own power plant and able to sell my electricity to power company. I hope readers enjoy my blog.
Image | This entry was posted in Work and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s